Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 1542/refactor EVENTS into hooks #1585

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
9079255
refactor LocationsModel in two diferent hooks
rebecarubio Oct 12, 2023
d4a9f72
implement useEventLocations and useEventLocationMutations hooks to re…
rebecarubio Oct 12, 2023
1bb46be
fix lint errors
rebecarubio Oct 12, 2023
4600806
refactor getAllEvents, getParallelEvents and getRelatedEvents in thei…
rebecarubio Oct 12, 2023
580d389
refactor duplicateEvent in its own hook and implement changes
rebecarubio Oct 12, 2023
6ffc816
refactor getData for event to useEventData hook and implement changes
rebecarubio Oct 12, 2023
bcdb535
refactor removeContact and setContact in its own hook
rebecarubio Oct 12, 2023
01e6554
refactor event mutations in its own hook and implement changes
rebecarubio Oct 12, 2023
d1b3253
refactor event state in its own hook and implemen changes
rebecarubio Oct 12, 2023
e555c8c
update EventState reference and imports
rebecarubio Oct 12, 2023
c062cdb
refactor deleteEvents and updateEvents in its own hook and implement …
rebecarubio Oct 12, 2023
b7e754c
refactor getting event participants data in its own hook useEventPart…
rebecarubio Oct 12, 2023
29f7fce
refactor in a hook the participants status info
rebecarubio Oct 12, 2023
24abcae
refactor addTypes and getEventTypes in its own hook and implement cha…
rebecarubio Oct 12, 2023
f3cfb50
refactor event participants mutations functions in its own hook and i…
rebecarubio Oct 12, 2023
d78df7d
remove repo and models folders and unused imports and related code
rebecarubio Oct 12, 2023
6fa3dee
implement useEventParticipantsData and useParticipantStatus hooks in …
rebecarubio Oct 12, 2023
4ab1e42
remove and sort imports
rebecarubio Oct 12, 2023
7bcf320
remove eventId parameter from useEventMutation hook and add missing t…
rebecarubio Oct 13, 2023
75ce83a
add declaration of ZetkinLocationPatchBody in useEventLocationMutatio…
rebecarubio Oct 13, 2023
e076716
update import reference for ZetkinEventPostBody
rebecarubio Oct 13, 2023
3552341
implement useEventMutations hook instead of Model in CampaignActionBu…
rebecarubio Oct 13, 2023
03d2231
sort imports
rebecarubio Oct 13, 2023
e0ac68a
restore EventRepo until another features are refactored
rebecarubio Oct 13, 2023
00c966d
Remove querys and implement backendApi calls in getServerSideProps fu…
rebecarubio Oct 13, 2023
ae314e2
remove unused fetching event functions
rebecarubio Oct 13, 2023
b9d7011
remove postEvent fetching function and dead code
rebecarubio Oct 13, 2023
51c7625
fix test by mocking call to org/1/actions
rebecarubio Oct 13, 2023
d617095
create a separate hook for createEvent action
rebecarubio Oct 16, 2023
294cb34
remove unnecessary function getDuplicatePostBody and add it inline
rebecarubio Oct 16, 2023
67e774e
rename hook useEventParticipantsData per useEventParticipants
rebecarubio Oct 16, 2023
3cbdf05
refactor useEventMutations to recieve eventId instead of functions
rebecarubio Oct 16, 2023
8ca592e
use new useCreateEvent instead of useEventMutations
rebecarubio Oct 16, 2023
5844478
rename useEventData to useEvent
rebecarubio Oct 16, 2023
11e4906
include event url when duplicating event
rebecarubio Oct 16, 2023
aeb6de7
rename useEventLocation to useEventLocations for consistency
rebecarubio Oct 16, 2023
2132714
rename useEventsMutations by useBulkEventMutations
rebecarubio Oct 16, 2023
87ccf89
remove hook useEventData hook because it is renamed to useEvent
rebecarubio Oct 16, 2023
be9e5bf
remove useEventsMutations file because it's renamed
rebecarubio Oct 16, 2023
878c6ff
remove unnused ZUISpeedDial and TaskList
rebecarubio Oct 20, 2023
7c3d0f0
get allEvents from useEventsFromDateRange in useParallelEvents hook i…
rebecarubio Oct 20, 2023
157f264
get events from useEventsFromDateRAnge instead of using useAllEvents …
rebecarubio Oct 20, 2023
0a579c9
use loadIfNecessary in useEvent hook
rebecarubio Oct 20, 2023
35991d4
move addTypes to useEventMutations hook and implement changes
rebecarubio Oct 20, 2023
a493560
add new function in eventParticipantMutations called setParticipantSt…
rebecarubio Oct 20, 2023
50a14a6
implement changes from useEventType in eventFilterPane
rebecarubio Oct 20, 2023
8afb684
get orgId from event object instead of url route if the component was…
rebecarubio Oct 20, 2023
5f42424
move addType to it's own hook useCreateType, implement it and remove …
rebecarubio Oct 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ test.describe('Campaigns list page ', () => {
});

test('shows list of campaigns ', async ({ page, appUri, moxy }) => {
moxy.setZetkinApiMock('/orgs/1/actions', 'get', []);
moxy.setZetkinApiMock('/orgs/1/campaigns', 'get', [
ReferendumSignatures,
WelcomeNewMembers,
Expand Down
11 changes: 5 additions & 6 deletions src/features/calendar/components/CalendarDayView/Day/Event.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import NextLink from 'next/link';
import { Box, Link, Typography } from '@mui/material';
import { People, PlaceOutlined, Schedule } from '@mui/icons-material';

import EventDataModel from 'features/events/models/EventDataModel';
import EventSelectionCheckBox from 'features/events/components/EventSelectionCheckBox';
import EventWarningIcons from 'features/events/components/EventWarningIcons';
import getEventState from 'features/events/utils/getEventState';
Expand All @@ -14,14 +13,10 @@ import StatusDot from 'features/events/components/EventPopper/StatusDot';
import theme from 'theme';
import { truncateOnMiddle } from 'utils/stringUtils';
import { useMessages } from 'core/i18n';
import useModel from 'core/useModel';
import { ZetkinEvent } from 'utils/types/zetkin';

const Event = ({ event }: { event: ZetkinEvent }) => {
const messages = useMessages(messageIds);
const model = useModel(
(env) => new EventDataModel(env, event.organization.id, event.id)
);

const needsParticipants =
event.num_participants_required > event.num_participants_available;
Expand Down Expand Up @@ -118,7 +113,11 @@ const Event = ({ event }: { event: ZetkinEvent }) => {
</Box>
{/* Icons */}
<Box alignItems="center" display="flex" gap={1}>
<EventWarningIcons compact model={model} />
<EventWarningIcons
compact
eventId={event.id}
orgId={event.organization.id}
/>
<People color={needsParticipants ? 'error' : 'secondary'} />
<Typography color={needsParticipants ? 'error' : 'secondary'}>
{event.num_participants_available}/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { Box, Button } from '@mui/material';

import CheckboxFilterList from './CheckboxFilterList';
import EventInputFilter from './EventInputFilter';
import EventTypesModel from 'features/events/models/EventTypesModel';
import messageIds from 'features/calendar/l10n/messageIds';
import PaneHeader from 'utils/panes/PaneHeader';
import useModel from 'core/useModel';
import useEventTypes from 'features/events/hooks/useEventTypes';
import { ZetkinActivity } from 'utils/types/zetkin';
import ZUIFuture from 'zui/ZUIFuture';
import {
Expand All @@ -15,6 +12,7 @@ import {
filterUpdated,
STATE_FILTER_OPTIONS,
} from 'features/events/store';
import { Box, Button } from '@mui/material';
import { Msg, useMessages } from 'core/i18n';
import { useAppDispatch, useAppSelector } from 'core/hooks';

Expand All @@ -25,7 +23,7 @@ const EventFilterPane = ({ orgId }: EventFilterPaneProps) => {
const dispatch = useAppDispatch();
const messages = useMessages(messageIds);
const state = useAppSelector((state) => state.events.filters);
const typesModel = useModel((env) => new EventTypesModel(env, orgId));
const eventTypes = useEventTypes(orgId);

const disableReset =
state.selectedActions.length === 0 &&
Expand Down Expand Up @@ -106,7 +104,7 @@ const EventFilterPane = ({ orgId }: EventFilterPaneProps) => {
selectedValues={state.selectedStates}
title={messages.eventFilter.filterOptions.stateFilters.title()}
/>
<ZUIFuture future={typesModel.getTypes()}>
<ZUIFuture future={eventTypes}>
{(data) => {
return (
<CheckboxFilterList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { scrollToEarliestEvent } from './utils';
import theme from 'theme';
import useWeekCalendarEvents from 'features/calendar/hooks/useWeekCalendarEvents';
import { ZetkinEvent } from 'utils/types/zetkin';
import { ZetkinEventPostBody } from 'features/events/repo/EventsRepo';
import { ZetkinEventPostBody } from 'features/events/hooks/useEventMutations';
import { useAppDispatch, useEnv, useNumericRouteParams } from 'core/hooks';

dayjs.extend(isoWeek);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EventOutlined } from '@mui/icons-material';
import { FC } from 'react';

import { EventState } from 'features/events/models/EventDataModel';
import { EventState } from 'features/events/hooks/useEventState';
import { fieldsToPresent } from './utils';
import getEventState from 'features/events/utils/getEventState';
import messageIds from 'features/calendar/l10n/messageIds';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FC } from 'react';

import calendarMessageIds from 'features/calendar/l10n/messageIds';
import eventMessageIds from 'features/events/l10n/messageIds';
import { EventState } from 'features/events/models/EventDataModel';
import { EventState } from 'features/events/hooks/useEventState';
import { fieldsToPresent } from './utils';
import getEventState from 'features/events/utils/getEventState';
import MultiLocationIcon from 'zui/icons/MultiLocation';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ScheduleOutlined } from '@mui/icons-material';

import calendarMessageIds from 'features/calendar/l10n/messageIds';
import eventMessageIds from 'features/events/l10n/messageIds';
import { EventState } from 'features/events/models/EventDataModel';
import { EventState } from 'features/events/hooks/useEventState';
import getEventState from 'features/events/utils/getEventState';
import LocationLabel from 'features/events/components/LocationLabel';
import { removeOffset } from 'utils/dateUtils';
Expand Down
2 changes: 1 addition & 1 deletion src/features/calendar/components/EventCluster/Single.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FC } from 'react';

import calendarMessageIds from 'features/calendar/l10n/messageIds';
import eventMessageIds from 'features/events/l10n/messageIds';
import { EventState } from 'features/events/models/EventDataModel';
import { EventState } from 'features/events/hooks/useEventState';
import { fieldsToPresent } from './utils';
import getEventState from 'features/events/utils/getEventState';
import LocationLabel from 'features/events/components/LocationLabel';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import {
} from '@mui/icons-material';

import { CLUSTER_TYPE } from 'features/campaigns/hooks/useClusteredActivities';
import EventDataModel from 'features/events/models/EventDataModel';
import EventWarningIcons from 'features/events/components/EventWarningIcons';
import getEventUrl from 'features/events/utils/getEventUrl';
import messageIds from 'features/events/l10n/messageIds';
import OverviewListItem from './OverviewListItem';
import { removeOffset } from 'utils/dateUtils';
import { useEventPopper } from 'features/events/components/EventPopper/EventPopperProvider';
import { useMessages } from 'core/i18n';
import useModel from 'core/useModel';
import { ZetkinEvent } from 'utils/types/zetkin';
import ZUIIconLabelRow from 'zui/ZUIIconLabelRow';
import ZUITimeSpan from 'zui/ZUITimeSpan';
Expand All @@ -30,9 +28,6 @@ const EventOverviewListItem: FC<EventOverviewListItemProps> = ({
focusDate,
}) => {
const { openEventPopper } = useEventPopper();
const model = useModel(
(env) => new EventDataModel(env, event.organization.id, event.id)
);
const messages = useMessages(messageIds);

return (
Expand All @@ -46,7 +41,13 @@ const EventOverviewListItem: FC<EventOverviewListItemProps> = ({
}
focusDate={focusDate}
href={getEventUrl(event)}
meta={<EventWarningIcons compact model={model} />}
meta={
<EventWarningIcons
compact
eventId={event.id}
orgId={event.organization.id}
/>
}
onClick={(x: number, y: number) => {
openEventPopper(
{ events: [event], kind: CLUSTER_TYPE.SINGLE },
Expand Down
22 changes: 10 additions & 12 deletions src/features/campaigns/components/CampaignActionButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ import ZUIEllipsisMenu from 'zui/ZUIEllipsisMenu';
import ZUISnackbarContext from 'zui/ZUISnackbarContext';
import { Msg, useMessages } from 'core/i18n';

import EventDataModel from 'features/events/models/EventDataModel';
import messageIds from '../l10n/messageIds';
import useCreateEvent from 'features/events/hooks/useCreateEvent';
import { useNumericRouteParams } from 'core/hooks';

enum CAMPAIGN_MENU_ITEMS {
EDIT_CAMPAIGN = 'editCampaign',
Expand All @@ -46,7 +47,7 @@ const CampaignActionButtons: React.FunctionComponent<
const messages = useMessages(messageIds);
const queryClient = useQueryClient();
const router = useRouter();
const { orgId } = router.query;
const { orgId } = useNumericRouteParams();
// Dialogs
const { showSnackbar } = useContext(ZUISnackbarContext);
const { showConfirmDialog } = useContext(ZUIConfirmDialogContext);
Expand All @@ -56,18 +57,15 @@ const CampaignActionButtons: React.FunctionComponent<
const closeCreateTaskDialog = () => setCreateTaskDialogOpen(false);

const model = useModel(
(env) => new CampaignDataModel(env, parseInt(orgId as string), campaign.id)
);
const eventModel = useModel(
(env) => new EventDataModel(env, parseInt(orgId as string), campaign.id)
(env) => new CampaignDataModel(env, orgId, campaign.id)
);

const { createEvent } = useCreateEvent(orgId);

// Mutations
const patchCampaignMutation = useMutation(
patchCampaign(orgId as string, campaign.id)
);
const patchCampaignMutation = useMutation(patchCampaign(orgId, campaign.id));
const deleteCampaignMutation = useMutation(
deleteCampaign(orgId as string, campaign.id)
deleteCampaign(orgId, campaign.id)
);

// Event Handlers
Expand All @@ -82,7 +80,7 @@ const CampaignActionButtons: React.FunctionComponent<
onError: () =>
showSnackbar('error', messages.form.deleteCampaign.error()),
onSuccess: () => {
router.push(`/organize/${orgId as string}/projects`);
router.push(`/organize/${orgId}/projects`);
},
});
};
Expand All @@ -96,7 +94,7 @@ const CampaignActionButtons: React.FunctionComponent<

const defaultEnd = new Date(defaultStart.getTime() + 60 * 60 * 1000);

eventModel.createEvent({
createEvent({
activity_id: null,
campaign_id: campaign.id,
end_time: defaultEnd.toISOString(),
Expand Down
31 changes: 16 additions & 15 deletions src/features/events/components/AddPersonButton.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import messageIds from '../l10n/messageIds';
import PersonAddIcon from '@mui/icons-material/PersonAdd';
import useEventParticipants from '../hooks/useEventParticipants';
import useEventParticipantsMutations from '../hooks/useEventParticipantsMutations';
import { useState } from 'react';
import { Box, IconButton, Popover } from '@mui/material';
import { EmojiPeople, People } from '@mui/icons-material';

import EventDataModel from 'features/events/models/EventDataModel';
import messageIds from '../l10n/messageIds';
import ZUIFutures from 'zui/ZUIFutures';
import { MUIOnlyPersonSelect as ZUIPersonSelect } from 'zui/ZUIPersonSelect';
import { Box, IconButton, Popover } from '@mui/material';
import { EmojiPeople, People } from '@mui/icons-material';
import { Msg, useMessages } from 'core/i18n';

interface AddPersonButtonProps {
model: EventDataModel;
orgId: number;
eventId: number;
}

const AddPersonButton = ({ model }: AddPersonButtonProps) => {
const AddPersonButton = ({ orgId, eventId }: AddPersonButtonProps) => {
const [anchorEl, setAnchorEl] = useState<Element | null>(null);

const messages = useMessages(messageIds);

const handleSelectedPerson = (personId: number) => {
model.addParticipant(personId);
};
const { addParticipant } = useEventParticipantsMutations(orgId, eventId);
const { participantsFuture, respondentsFuture } = useEventParticipants(
orgId,
eventId
);

return (
<>
Expand Down Expand Up @@ -60,8 +61,8 @@ const AddPersonButton = ({ model }: AddPersonButtonProps) => {
<Box mt={1} p={2}>
<ZUIFutures
futures={{
participants: model.getParticipants(),
respondents: model.getRespondents(),
participants: participantsFuture,
respondents: respondentsFuture,
}}
>
{({ data: { participants, respondents } }) => {
Expand Down Expand Up @@ -117,7 +118,7 @@ const AddPersonButton = ({ model }: AddPersonButtonProps) => {
}}
name="person"
onChange={(person) => {
handleSelectedPerson(person.id);
addParticipant(person.id);
}}
placeholder={messages.addPerson.addPlaceholder()}
selectedPerson={null}
Expand Down
21 changes: 11 additions & 10 deletions src/features/events/components/EventActionButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import {
} from '@mui/icons-material';
import React, { useContext } from 'react';

import EventDataModel from '../models/EventDataModel';
import messageIds from '../l10n/messageIds';
import useDuplicateEvent from '../hooks/useDuplicateEvent';
import useEventMutations from '../hooks/useEventMutations';
import { useMessages } from 'core/i18n';
import useModel from 'core/useModel';
import { ZetkinEvent } from 'utils/types/zetkin';
import { ZUIConfirmDialogContext } from 'zui/ZUIConfirmDialogProvider';
import ZUIDatePicker from 'zui/ZUIDatePicker';
Expand All @@ -28,34 +28,35 @@ const EventActionButtons: React.FunctionComponent<EventActionButtonsProps> = ({
const orgId = event.organization.id;
const { showConfirmDialog } = useContext(ZUIConfirmDialogContext);
const router = useRouter();

const model = useModel((env) => new EventDataModel(env, orgId, event.id));
const { cancelEvent, deleteEvent, restoreEvent, setPublished } =
useEventMutations(orgId, event.id);
const { duplicateEvent } = useDuplicateEvent(orgId, event.id);

const published =
!!event.published && new Date(event.published) <= new Date();

const handlePublish = () => {
model.setPublished(new Date().toISOString());
setPublished(new Date().toISOString());
};

const handleUnpublish = () => {
model.setPublished(null);
setPublished(null);
};
const handleChangeDate = (date: string | null) => {
model.setPublished(date);
setPublished(date);
};

const handleDelete = () => {
model.deleteEvent();
deleteEvent();
router.push(`/organize/${orgId}/projects/${event.campaign?.id || ''} `);
};

const handleDuplicate = () => {
model.duplicateEvent();
duplicateEvent();
};

const handleCancel = () => {
event.cancelled ? model.restoreEvent() : model.cancel();
event.cancelled ? restoreEvent() : cancelEvent();
};

return (
Expand Down
Loading
Loading