Skip to content

Commit

Permalink
refactor(web): create utility saga to use during someday event conver…
Browse files Browse the repository at this point in the history
…sion
  • Loading branch information
tyler-dane committed Jan 2, 2025
1 parent 0057c1b commit a809445
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
11 changes: 10 additions & 1 deletion packages/web/src/ducks/events/sagas/saga.util.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { schema } from "normalizr";
import { put } from "redux-saga/effects";
import { put, select } from "redux-saga/effects";
import { normalize } from "normalizr";
import { Schema_Event } from "@core/types/event.types";
import { Schema_GridEvent } from "@web/common/types/web.event.types";
import { RootState } from "@web/store";

import { getSomedayEventsSlice } from "../slices/someday.slice";
import { getWeekEventsSlice } from "../slices/week.slice";
import { eventsEntitiesSlice } from "../slices/event.slice";
import { selectEventById } from "../selectors/event.selectors";

export function* getEventById(_id: string) {
const currEvent = (yield select((state: RootState) =>
selectEventById(state, _id)
)) as Schema_Event;
return currEvent;
}

export function* insertOptimisticEvent(
event: Schema_GridEvent,
Expand Down
34 changes: 19 additions & 15 deletions packages/web/src/ducks/events/sagas/somday.sagas.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,45 @@
import { AxiosResponse } from "axios";
import { normalize } from "normalizr";
import { call, put, select } from "redux-saga/effects";
import { call, put } from "redux-saga/effects";
import { Schema_Event } from "@core/types/event.types";
import { Payload_NormalizedAsyncAction } from "@web/common/types/entity.types";
import {
replaceIdWithOptimisticId,
handleError,
} from "@web/common/utils/event.util";
import { RootState } from "@web/store";
import { Schema_GridEvent } from "@web/common/types/web.event.types";
import { removeSomedayProperties } from "@web/common/utils/grid.util";

import { Action_Someday_Reorder } from "../slices/someday.slice.types";
import { EventApi } from "../event.api";
import {
Action_ConvertSomedayEvent,
Response_GetEventsSaga,
Action_DeleteEvent,
Action_GetEvents,
Response_GetEventsSuccess,
} from "../event.types";
import { selectEventById } from "../selectors/event.selectors";
import { eventsEntitiesSlice } from "../slices/event.slice";
import { getSomedayEventsSlice } from "../slices/someday.slice";
import {
insertOptimisticEvent,
replaceOptimisticId,
normalizedEventsSchema,
getEventById,
} from "./saga.util";

export function* convertSomedayEvent({ payload }: Action_ConvertSomedayEvent) {
const { _id, updatedFields } = payload;
const optimisticId: string | null = null;

try {
//get grid event from store
const currEvent = (yield select((state: RootState) =>
selectEventById(state, _id)
)) as Response_GetEventsSaga;
const gridEvent = { ...currEvent, ...updatedFields };
// remove extra props before sending to DB
delete gridEvent.order;
delete gridEvent.recurrence;
const gridEvent = yield* _assembleGridEvent(_id, updatedFields);

//get optimisitcGridEvent
//create optimisitcGridEvent
const optimisticGridEvent = replaceIdWithOptimisticId(gridEvent);
yield put(getSomedayEventsSlice.actions.remove({ _id }));
yield* insertOptimisticEvent(optimisticGridEvent, false);

// call API
// create real event
const response = (yield call(
EventApi.edit,
_id,
Expand All @@ -56,7 +49,7 @@ export function* convertSomedayEvent({ payload }: Action_ConvertSomedayEvent) {

const convertedEvent = response.data;

// replace ids
// cleanup replace ids
yield* replaceOptimisticId(
optimisticGridEvent._id,
convertedEvent._id as string,
Expand Down Expand Up @@ -116,3 +109,14 @@ export function* reorderSomedayEvents({ payload }: Action_Someday_Reorder) {
handleError(error as Error);
}
}

function* _assembleGridEvent(
_id: string,
updatedFields: Partial<Schema_Event>
) {
const currEvent = yield* getEventById(_id);

const _gridEvent = { ...currEvent, ...updatedFields };
const gridEvent = removeSomedayProperties(_gridEvent);
return gridEvent as Schema_GridEvent;
}

0 comments on commit a809445

Please sign in to comment.