Skip to content

Commit

Permalink
chore: simplify optimistic event creation
Browse files Browse the repository at this point in the history
  • Loading branch information
tyler-dane committed Jan 1, 2025
1 parent 6c4b5f3 commit 56b83f8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 28 deletions.
14 changes: 1 addition & 13 deletions packages/web/src/common/utils/event.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export const prepEvtBeforeSubmit = (draft: Schema_GridEvent) => {
return event;
};

export const createOptimisticEvent = (
export const replaceIdWithOptimisticId = (
event: Schema_Event
): Schema_OptimisticEvent => {
const _event: Schema_OptimisticEvent = {
Expand All @@ -223,15 +223,3 @@ 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;
};
3 changes: 2 additions & 1 deletion packages/web/src/ducks/events/event.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Schema_Event,
} from "@core/types/event.types";
import { CompassApi } from "@web/common/apis/compass.api";
import { AxiosPromise } from "axios";

const EventApi = {
create: (event: Schema_Event) => {
Expand All @@ -17,7 +18,7 @@ const EventApi = {
_id: string,
event: Schema_Event,
params: { applyTo?: Categories_Recur }
) => {
): AxiosPromise<Schema_Event> => {
if (params?.applyTo) {
return CompassApi.put(`/event/${_id}?applyTo=${params.applyTo}`, event);
}
Expand Down
36 changes: 22 additions & 14 deletions packages/web/src/ducks/events/sagas/event.sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { EventApi } from "@web/ducks/events/event.api";
import { selectEventById } from "@web/ducks/events/selectors/event.selectors";
import { selectPaginatedEventsBySectionType } from "@web/ducks/events/selectors/util.selectors";
import {
createOptimisticEvent,
createOptimisticGridEvent,
replaceIdWithOptimisticId,
handleError,
} from "@web/common/utils/event.util";
import { Schema_GridEvent } from "@web/common/types/web.event.types";
import { ID_OPTIMISTIC_PREFIX } from "@web/common/constants/web.constants";
import { AxiosResponse } from "axios";

import {
createEventSlice,
Expand Down Expand Up @@ -48,28 +48,36 @@ import {

function* convertSomedayEvent({ payload }: Action_ConvertSomedayEvent) {
const { _id, updatedFields } = payload;
let optimisticId: string | null = null;
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;

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

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

const res = yield call(EventApi.edit, _id, gridEvent);
const convertedEvent = res.data as Schema_Event;
// call API
const response = (yield call(
EventApi.edit,
_id,
gridEvent,
{}
)) as AxiosResponse<Schema_Event>;

const convertedEvent = response.data;

// replace ids
yield* replaceOptimisticId(
optimisticId,
optimisticGridEvent._id,
convertedEvent._id as string,
false
);
Expand Down Expand Up @@ -115,7 +123,7 @@ function* convertTimedEvent({ payload }: Action_ConvertTimedEvent) {
}

function* createEvent({ payload }: Action_CreateEvent) {
const event = createOptimisticEvent(payload);
const event = replaceIdWithOptimisticId(payload);
const optimisticId = event._id;

try {
Expand Down

0 comments on commit 56b83f8

Please sign in to comment.