Skip to content

Commit

Permalink
chore(web): add createOptimisticGridEvent function to cleanup saga
Browse files Browse the repository at this point in the history
splitting things out of the convertSomedayEvent saga will make the saga more readable
  • Loading branch information
tyler-dane committed Dec 31, 2024
1 parent c650498 commit c435313
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
19 changes: 16 additions & 3 deletions packages/web/src/common/utils/event.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import { YEAR_MONTH_DAY_COMPACT_FORMAT } from "@core/constants/date.constants";
import { Categories_Event, Schema_Event } from "@core/types/event.types";
import { Origin, Priorities } from "@core/constants/core.constants";
import { Status } from "@core/errors/status.codes";
import { Response_GetEventsSaga } from "@web/ducks/events/event.types";

import {
Schema_GridEvent,
Schema_OptimisticEvent,
Schema_SomedayEventsColumn,
} from "../types/web.event.types";
import { removeGridFields } from "./grid.util";
import { removeGridProperties } from "./grid.util";
import {
COLUMN_WEEK,
COLUMN_MONTH,
Expand Down Expand Up @@ -192,7 +193,7 @@ export const prepEvtAfterDraftDrop = (
};

export const prepEvtBeforeConvertToSomeday = (draft: Schema_GridEvent) => {
const event = removeGridFields(draft);
const event = removeGridProperties(draft);

if (event.recurrence) {
delete event.recurrence;
Expand All @@ -202,7 +203,7 @@ export const prepEvtBeforeConvertToSomeday = (draft: Schema_GridEvent) => {
};

export const prepEvtBeforeSubmit = (draft: Schema_GridEvent) => {
const _event = removeGridFields({ ...draft });
const _event = removeGridProperties({ ...draft });

const event = {
..._event,
Expand All @@ -222,3 +223,15 @@ export const createOptimisticEvent = (

return _event;
};

export const createOptimisticGridEvent = (
currentEvent: Response_GetEventsSaga,
updatedFields: Schema_GridEvent
) => {
const gridEvent = { ...currentEvent, ...updatedFields };
delete gridEvent.order;
delete gridEvent.recurrence;

const optimisticGridEvent = createOptimisticEvent(gridEvent);
return optimisticGridEvent;
};
11 changes: 8 additions & 3 deletions packages/web/src/common/utils/grid.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,17 +488,22 @@ const normalizeDayNums = (days: number[]) => {
});
};

export const removeGridFields = (event: Schema_GridEvent): Schema_Event => {
export const removeGridProperties = (event: Schema_GridEvent): Schema_Event => {
const {
isEditing,
importanceIndex,
isOpen,
row,
siblingsCount,
...eventWithoutGridFields
...eventWithoutGridProps
} = event;

return eventWithoutGridFields;
return eventWithoutGridProps;
};

export const removeSomedayProperties = (event: Schema_Event): Schema_Event => {
const { order, recurrence, ...eventWithoutSomedayProps } = event;
return eventWithoutSomedayProps;
};

export const widthMinusPadding = (width: number) => {
Expand Down
12 changes: 6 additions & 6 deletions packages/web/src/ducks/events/sagas/event.sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { selectEventById } from "@web/ducks/events/selectors/event.selectors";
import { selectPaginatedEventsBySectionType } from "@web/ducks/events/selectors/util.selectors";
import {
createOptimisticEvent,
createOptimisticGridEvent,
handleError,
} from "@web/common/utils/event.util";
import { Schema_GridEvent } from "@web/common/types/web.event.types";
Expand Down Expand Up @@ -54,14 +55,13 @@ function* convertSomedayEvent({ payload }: Action_ConvertSomedayEvent) {
selectEventById(state, _id)
)) as Response_GetEventsSaga;

const gridEvent = { ...currEvent, ...updatedFields };
delete gridEvent.order;
delete gridEvent.recurrence;

const optimisticGridEvent = createOptimisticEvent(gridEvent);
// Optimistically convert the event
const optimisticGridEvent = createOptimisticGridEvent(
currEvent,
updatedFields
);
optimisticId = optimisticGridEvent._id;

// Optimistically convert the event
yield put(getSomedayEventsSlice.actions.remove({ _id }));
yield* insertOptimisticEvent(optimisticGridEvent, false);

Expand Down

0 comments on commit c435313

Please sign in to comment.