@@ -17,6 +17,7 @@ import dayjs, { Dayjs } from "@core/util/date/dayjs";
1717import { getUserId } from "@web/auth/auth.util" ;
1818import { isEventDirty } from "@web/common/parsers/dirty.parser" ;
1919import { EventParser } from "@web/common/parsers/event.parser" ;
20+ import { EventInViewParser } from "@web/common/parsers/view.parser" ;
2021import { PartialMouseEvent } from "@web/common/types/util.types" ;
2122import {
2223 Schema_GridEvent ,
@@ -27,6 +28,7 @@ import {
2728 replaceIdWithOptimisticId ,
2829} from "@web/common/utils/event.util" ;
2930import { getX } from "@web/common/utils/grid.util" ;
31+ import { Payload_EditEvent } from "@web/ducks/events/event.types" ;
3032import {
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