Skip to content

Commit b867c33

Browse files
committed
✨ Feat: Add drag and drop (DnD) state management for sidebar events
1 parent 281fed4 commit b867c33

File tree

7 files changed

+31
-19
lines changed

7 files changed

+31
-19
lines changed

packages/web/src/ducks/events/selectors/draft.selectors.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ export const selectDraftStatus = (state: RootState) =>
99
export const selectIsDrafting = (state: RootState) =>
1010
state.events.draft.status?.isDrafting;
1111

12+
export const selectIsDNDing = (state: RootState) =>
13+
state.events.draft.status?.activity === "dnd";
14+
1215
export const selectIsDraftingExisting = (state: RootState) =>
1316
state.events.draft.event?._id !== undefined;
1417

packages/web/src/ducks/events/slices/draft.slice.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ export const draftSlice = createSlice({
4747
};
4848
},
4949

50+
startDnd: (state) => {
51+
state.status = {
52+
...state.status,
53+
activity: "dnd",
54+
isDrafting: true,
55+
};
56+
},
57+
5058
startDragging: (state, action) => {
5159
const { event } = action.payload;
5260
state.event = event;

packages/web/src/ducks/events/slices/draft.slice.types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ export interface Action_DraftEvent extends Action {
77
}
88
export type Activity_DraftEvent =
99
| "createShortcut"
10-
| "eventRightClick"
10+
| "dnd"
1111
| "dragging"
12+
| "eventRightClick"
1213
| "gridClick"
1314
| "resizing"
1415
| "sidebarClick";

packages/web/src/views/Calendar/components/Draft/hooks/sidebar/useMousePosition.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
import { Coordinates } from "@web/common/types/util.types";
88

99
export const useMousePosition = (
10-
isDragging: boolean,
10+
isDNDing: boolean,
1111
isFormOpen: boolean,
1212
measurements: Measurements_Grid,
1313
) => {
@@ -40,14 +40,14 @@ export const useMousePosition = (
4040
setMouseCoords({ x, y });
4141
};
4242

43-
if (!isDragging || isFormOpen) return;
43+
if (!isDNDing) return;
4444

4545
window.addEventListener("mousemove", handleMouseMove);
4646

4747
return () => {
4848
window.removeEventListener("mousemove", handleMouseMove);
4949
};
50-
}, [allDayRow?.bottom, allDayRow?.top, isDragging, isFormOpen]);
50+
}, [allDayRow?.bottom, allDayRow?.top, isDNDing, isFormOpen]);
5151

5252
return { isOverAllDayRow, isOverGrid, isOverMainGrid, mouseCoords };
5353
};

packages/web/src/views/Calendar/components/Draft/hooks/sidebar/useSidebarState.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import { Schema_Event } from "@core/types/event.types";
33
import { COLUMN_MONTH, COLUMN_WEEK } from "@web/common/constants/web.constants";
44
import { Measurements_Grid } from "@web/views/Calendar/hooks/grid/useGridLayout";
55
import { useAppSelector } from "@web/store/store.hooks";
6-
import { selectDraftStatus } from "@web/ducks/events/selectors/draft.selectors";
6+
import {
7+
selectDraftStatus,
8+
selectIsDNDing,
9+
} from "@web/ducks/events/selectors/draft.selectors";
710
import { selectCategorizedEvents } from "@web/ducks/events/selectors/someday.selectors";
811

912
import { useMousePosition } from "./useMousePosition";
@@ -17,6 +20,7 @@ export const useSidebarState = (measurements: Measurements_Grid) => {
1720
}, [categorizedEvents]);
1821

1922
const { eventType: draftType } = useAppSelector(selectDraftStatus);
23+
const isDNDing = useAppSelector(selectIsDNDing);
2024

2125
const [draft, setDraft] = useState<Schema_Event | null>(null);
2226
const [isDrafting, setIsDrafting] = useState(false);
@@ -25,7 +29,7 @@ export const useSidebarState = (measurements: Measurements_Grid) => {
2529

2630
const isDragging = isDrafting && draft !== null;
2731
const { isOverAllDayRow, isOverGrid, isOverMainGrid, mouseCoords } =
28-
useMousePosition(isDragging, isSomedayFormOpen, measurements);
32+
useMousePosition(isDNDing, isSomedayFormOpen, measurements);
2933

3034
const somedayWeekIds = somedayEvents.columns[COLUMN_WEEK]
3135
.eventIds as string[];
@@ -36,8 +40,7 @@ export const useSidebarState = (measurements: Measurements_Grid) => {
3640
const isDraftingNew =
3741
isDrafting && !isDraftingExisting && !somedayIds.includes(draft?._id);
3842

39-
const shouldPreviewOnGrid =
40-
draft !== null && isOverGrid && !isSomedayFormOpen;
43+
const shouldPreviewOnGrid = isDNDing && isOverGrid;
4144

4245
return {
4346
draft,

packages/web/src/views/Calendar/components/Draft/hooks/sidebar/useSidebarUtil.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
import { DropResult } from "@hello-pangea/dnd";
1414
import { ID_SOMEDAY_DRAFT } from "@web/common/constants/web.constants";
1515
import { DropResult_ReactDND } from "@web/common/types/dnd.types";
16-
import { Schema_GridEvent } from "@web/common/types/web.event.types";
1716
import {
1817
prepEvtAfterDraftDrop,
1918
assembleDefaultEvent,
@@ -187,6 +186,7 @@ export const useSidebarUtil = (
187186
const onDraft = (event: Schema_Event, category: Categories_Event) => {
188187
state.setIsDrafting(true);
189188
state.setDraft(event);
189+
setIsSomedayFormOpen(true);
190190

191191
dispatch(
192192
draftSlice.actions.start({
@@ -244,25 +244,22 @@ export const useSidebarUtil = (
244244
close();
245245
};
246246

247-
const onDragStart = (props: { draggableId: string }) => {
247+
const onDragStart = async (props: { draggableId: string }) => {
248248
const existingEvent = state.somedayEvents.events[props.draggableId];
249249
const isExisting = existingEvent !== undefined;
250250

251-
let _draft: Schema_GridEvent;
251+
dispatch(draftSlice.actions.startDnd());
252+
252253
if (isExisting) {
253-
_draft = {
254-
...existingEvent,
255-
isOpen: false,
256-
};
254+
state.setDraft(existingEvent);
257255
} else {
258256
console.log("REMINDER: update for monthly");
259-
const defaultSomeday = assembleDefaultEvent(
257+
const defaultSomeday = await assembleDefaultEvent(
260258
Categories_Event.SOMEDAY_WEEK,
261259
);
262-
_draft = { ...defaultSomeday, isOpen: false };
260+
state.setDraft(defaultSomeday);
263261
}
264262

265-
state.setDraft(_draft);
266263
state.setIsDrafting(true);
267264
};
268265

packages/web/src/views/Calendar/components/Sidebar/SomedayTab/SomedayEvents/SomedayEvents.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export const SomedayEvents: FC<Props> = ({
6565

6666
return (
6767
<DragDropContext onDragEnd={util.onDragEnd} onDragStart={util.onDragStart}>
68-
{state.shouldPreviewOnGrid && (
68+
{state.shouldPreviewOnGrid && state.draft && (
6969
<GridEventPreview
7070
dateCalcs={dateCalcs}
7171
dayIndex={dayIndex}

0 commit comments

Comments
 (0)