Skip to content

Commit c7e980f

Browse files
committed
refactor(web): use EventParser for event submission processing
1 parent 47a431a commit c7e980f

File tree

3 files changed

+73
-42
lines changed

3 files changed

+73
-42
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Schema_WebEvent } from "../types/web.event.types";
2+
import {
3+
prepEvtBeforeSubmit,
4+
prepSomedayEventBeforeSubmit,
5+
} from "../utils/event.util";
6+
7+
export class EventParser {
8+
private readonly event: Schema_WebEvent;
9+
10+
constructor(event: Schema_WebEvent) {
11+
this.event = event;
12+
}
13+
14+
public parse() {
15+
console.log("this.event", this.event);
16+
if (this.event.isSomeday) {
17+
return prepSomedayEventBeforeSubmit(this.event, this.event.user);
18+
}
19+
return prepEvtBeforeSubmit(this.event, this.event.user);
20+
}
21+
}

packages/web/src/common/utils/event.util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ export const prepEvtBeforeSubmit = (draft: Schema_WebEvent, userId: string) => {
225225
};
226226

227227
export const prepSomedayEventBeforeSubmit = (
228-
draft: Schema_Event | Schema_GridEvent,
228+
draft: Schema_WebEvent,
229229
userId: string,
230230
) => {
231231
const _event: Omit<Schema_WebEvent, "recurrence"> = {

packages/web/src/views/Calendar/components/Draft/hooks/actions/useDraftActions.ts

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import dayjs, { Dayjs } from "@core/util/date/dayjs";
1717
import { getUserId } from "@web/auth/auth.util";
1818
import { isEventDirty } from "@web/common/parsers/dirty.parser";
1919
import { EventParser } from "@web/common/parsers/event.parser";
20+
import { EventInViewParser } from "@web/common/parsers/view.parser";
2021
import { PartialMouseEvent } from "@web/common/types/util.types";
2122
import {
2223
Schema_GridEvent,
@@ -27,6 +28,7 @@ import {
2728
replaceIdWithOptimisticId,
2829
} from "@web/common/utils/event.util";
2930
import { getX } from "@web/common/utils/grid.util";
31+
import { Payload_EditEvent } from "@web/ducks/events/event.types";
3032
import {
3133
selectDraft,
3234
selectDraftStatus,
@@ -256,7 +258,46 @@ export const useDraftActions = (
256258
[reduxDraft, isFormOpenBeforeDragging],
257259
);
258260

259-
// const determineEditActions = useCallback((draft: Schema_WebEvent) => {}, []);
261+
const getEditSlicePayload = useCallback(
262+
(
263+
event: Schema_WebEvent,
264+
applyTo: RecurringEventUpdateScope,
265+
): Payload_EditEvent => {
266+
const viewParser = new EventInViewParser(
267+
event,
268+
weekProps.component.startOfView,
269+
weekProps.component.endOfView,
270+
);
271+
const shouldRemove = viewParser.isEventOutsideView();
272+
273+
const payload = { _id: event._id, event, shouldRemove, applyTo };
274+
return payload;
275+
},
276+
[],
277+
);
278+
279+
const shouldAddToView = useCallback(
280+
(event: Schema_WebEvent) => {
281+
const viewParser = new EventInViewParser(
282+
event,
283+
weekProps.component.startOfView,
284+
weekProps.component.endOfView,
285+
);
286+
const lastNavSource = weekProps.util.getLastNavigationSource();
287+
const idsInView = currentWeekEvents?.data ?? [];
288+
const shouldAddToView = viewParser.shouldAddToViewAfterDragToEdge(
289+
lastNavSource,
290+
idsInView,
291+
);
292+
return shouldAddToView;
293+
},
294+
[
295+
weekProps.component.startOfView,
296+
weekProps.component.endOfView,
297+
weekProps.util,
298+
currentWeekEvents?.data,
299+
],
300+
);
260301

261302
const submit = useCallback(
262303
async (
@@ -282,50 +323,19 @@ export const useDraftActions = (
282323
return;
283324
}
284325
case "UPDATE": {
285-
const event = new EventParser(draft).parse();
286-
// const userId = await getUserId();
287-
288-
const { startOfView, endOfView } = weekProps.component;
289326
const isExisting =
290327
draft._id && !draft._id.startsWith(ID_OPTIMISTIC_PREFIX);
291328

292329
if (isExisting) {
293-
const isDateWithinView = (date: string) =>
294-
dayjs(date).isBetween(startOfView, endOfView, null, "[]");
295-
296-
const isStartDateInView = isDateWithinView(event.startDate);
297-
const isEndDateInView = isDateWithinView(event.endDate);
298-
const doesEventSpanView =
299-
dayjs(event.startDate).isBefore(startOfView) &&
300-
dayjs(event.endDate).isAfter(endOfView);
301-
302-
const isEventCompletelyOutsideView =
303-
!isStartDateInView && !isEndDateInView && !doesEventSpanView;
304-
305-
const shouldRemove = isEventCompletelyOutsideView;
306-
307-
const payload = { _id: event._id, event, shouldRemove, applyTo };
330+
// const userId = await getUserId();
331+
const event = new EventParser(draft).parse();
332+
const payload = getEditSlicePayload(event, applyTo);
308333
dispatch(
309334
editEventSlice.actions.request(payload as unknown as void),
310335
);
311336

312-
// If this was a drag-to-edge navigation and event moved to current week, ensure it's visible
313-
const lastNavigationSource =
314-
weekProps.util.getLastNavigationSource();
315-
const isDragToEdgeNavigation =
316-
lastNavigationSource === "drag-to-edge";
317-
const wasEventMovedToCurrentWeek =
318-
!shouldRemove &&
319-
(isStartDateInView || isEndDateInView || doesEventSpanView);
320-
321-
if (isDragToEdgeNavigation && wasEventMovedToCurrentWeek) {
322-
// Only insert if the event is not already in the current week's event list
323-
const isEventAlreadyInWeek = currentWeekEvents?.data.includes(
324-
event._id!,
325-
);
326-
if (!isEventAlreadyInWeek) {
327-
dispatch(getWeekEventsSlice.actions.insert(event._id!));
328-
}
337+
if (shouldAddToView(event)) {
338+
dispatch(getWeekEventsSlice.actions.insert(event._id!));
329339
}
330340
}
331341

@@ -341,13 +351,13 @@ export const useDraftActions = (
341351
}
342352
},
343353
[
344-
isFormOpenBeforeDragging,
345-
weekProps,
346-
currentWeekEvents,
347-
dispatch,
348354
determineSubmitAction,
349355
discard,
356+
dispatch,
357+
getEditSlicePayload,
358+
isFormOpenBeforeDragging,
350359
openForm,
360+
shouldAddToView,
351361
],
352362
);
353363

0 commit comments

Comments
 (0)