Skip to content

Commit df20a6f

Browse files
committed
checkpoint(refactor): move handlers to root mouse hook instead of in component
this implementation is based on measureing the grid and computing the position of the mouse (which is probably unnecessarily complicated) works: initial drag and move and click out using
1 parent 1c8696b commit df20a6f

File tree

14 files changed

+171
-148
lines changed

14 files changed

+171
-148
lines changed

packages/web/src/common/types/web.event.types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ export interface Schema_SomedayEventsColumn {
5252
[key: string]: Schema_Event;
5353
};
5454
}
55+
56+
type Activity = "createShortcut" | "dragging" | "gridClick" | "resizing";
5557
export interface Status_DraftEvent {
56-
activity: string | null;
58+
activity: Activity | null;
5759
eventType: Categories_Event | null;
5860
isDrafting: boolean;
5961
dateToResize: "startDate" | "endDate" | null;
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { MouseEvent } from "react";
2+
import dayjs, { Dayjs } from "dayjs";
3+
import {
4+
ID_GRID_EVENTS_ALLDAY,
5+
ID_GRID_EVENTS_TIMED,
6+
} from "@web/common/constants/web.constants";
7+
import { roundToNext } from "@web/common/utils";
8+
import { getElemById, getX } from "@web/common/utils/grid.util";
9+
import {
10+
DRAFT_DURATION_MIN,
11+
GRID_TIME_STEP,
12+
GRID_Y_START,
13+
SIDEBAR_X_START,
14+
} from "@web/views/Calendar/layout.constants";
15+
import { Categories_Event } from "@core/types/event.types";
16+
import { assembleDefaultEvent } from "@web/common/utils/event.util";
17+
import { DateCalcs } from "@web/views/Calendar/hooks/grid/useDateCalcs";
18+
import { Schema_GridEvent } from "@web/common/types/web.event.types";
19+
import { getMousePosition } from "../position/mouse.position";
20+
21+
export const assembleTimedDraft = async (
22+
e: MouseEvent,
23+
dateCalcs: DateCalcs,
24+
isSidebarOpen: boolean,
25+
startOfView: Dayjs
26+
): Promise<Schema_GridEvent> => {
27+
const x = getX(e, isSidebarOpen);
28+
const _start = dateCalcs.getDateByXY(x, e.clientY, startOfView);
29+
const startDate = _start.format();
30+
const endDate = _start.add(DRAFT_DURATION_MIN, "minutes").format();
31+
32+
const event = (await assembleDefaultEvent(
33+
Categories_Event.TIMED,
34+
startDate,
35+
endDate
36+
)) as Schema_GridEvent;
37+
return event;
38+
};
39+
40+
export const getDraftTimes = (isCurrentWeek: boolean, startOfWeek: Dayjs) => {
41+
const currentMinute = dayjs().minute();
42+
const nextMinuteInterval = roundToNext(currentMinute, GRID_TIME_STEP);
43+
44+
const fullStart = isCurrentWeek ? dayjs() : startOfWeek.hour(dayjs().hour());
45+
const _start = fullStart.minute(nextMinuteInterval).second(0);
46+
47+
const _end = _start.add(1, "hour");
48+
const startDate = _start.format();
49+
const endDate = _end.format();
50+
51+
return { startDate, endDate };
52+
};
53+
54+
export const getDraftContainer = (isAllDay: boolean) => {
55+
if (isAllDay) {
56+
return getElemById(ID_GRID_EVENTS_ALLDAY);
57+
}
58+
59+
return getElemById(ID_GRID_EVENTS_TIMED);
60+
};
61+
62+
export const isOverMainGrid = (
63+
x: number,
64+
y: number,
65+
allDayRow: DOMRect | null
66+
) => {
67+
if (!allDayRow?.bottom || !allDayRow?.top) {
68+
throw Error("Missing measurements for all-day row");
69+
return false;
70+
}
71+
72+
const { isOverMainGrid } = getMousePosition(
73+
{
74+
allDayRowBottom: allDayRow.bottom,
75+
allDayRowTop: allDayRow.top,
76+
gridYStart: GRID_Y_START,
77+
sidebarXStart: SIDEBAR_X_START,
78+
},
79+
{
80+
x,
81+
y,
82+
}
83+
);
84+
85+
return isOverMainGrid;
86+
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ interface Payload_Draft_Drag {
2222
}
2323

2424
interface Payload_DraftEvent {
25+
activity?: "createShortcut" | "dragging" | "gridClick" | "resizing";
2526
event?: Schema_Event;
2627
eventType: Categories_Event;
27-
activity?: "createShortcut" | "dragging" | "resizing";
2828
}
2929

3030
interface Payload_Draft_Resize {

packages/web/src/views/Calendar/components/Event/Draft/Draft.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import { WeekProps } from "@web/views/Calendar/hooks/useWeek";
77
import { Measurements_Grid } from "@web/views/Calendar/hooks/grid/useGridLayout";
88
import { DateCalcs } from "@web/views/Calendar/hooks/grid/useDateCalcs";
99
import { getCategory } from "@web/common/utils/event.util";
10+
import { getDraftContainer } from "@web/common/utils/draft/draft.util";
1011
import { selectIsDrafting } from "@web/ducks/events/selectors/draft.selectors";
1112

12-
import { getDraftContainer } from "./draft.util";
1313
import { GridDraft } from "./GridDraft";
1414

1515
interface Props {

packages/web/src/views/Calendar/components/Event/Draft/draft.util.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

packages/web/src/views/Calendar/components/Grid/Grid.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ export const Grid: FC<Props> = ({
3737
/>
3838

3939
<MainGrid
40-
dateCalcs={dateCalcs}
41-
isSidebarOpen={isSidebarOpen}
4240
mainGridRef={mainGridRef}
4341
measurements={measurements}
4442
scrollRef={gridScrollRef}

packages/web/src/views/Calendar/components/Grid/MainGrid/MainGrid.tsx

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1-
import React, { FC, MouseEvent } from "react";
1+
import React, { FC } from "react";
22
import mergeRefs from "react-merge-refs";
33
import { Dayjs } from "dayjs";
4-
import { Categories_Event } from "@core/types/event.types";
5-
import { DRAFT_DURATION_MIN } from "@web/views/Calendar/layout.constants";
6-
import { useAppDispatch, useAppSelector } from "@web/store/store.hooks";
4+
75
import { Ref_Callback } from "@web/common/types/util.types";
86
import { WeekProps } from "@web/views/Calendar/hooks/useWeek";
9-
import { DateCalcs } from "@web/views/Calendar/hooks/grid/useDateCalcs";
107
import { Ref_Grid } from "@web/views/Calendar/components/Grid/grid.types";
118
import { ID_GRID_MAIN } from "@web/common/constants/web.constants";
129
import { Measurements_Grid } from "@web/views/Calendar/hooks/grid/useGridLayout";
13-
import { assembleDefaultEvent } from "@web/common/utils/event.util";
14-
import { getX } from "@web/common/utils/grid.util";
15-
import { draftSlice } from "@web/ducks/events/slices/draft.slice";
16-
import { isEventFormOpen } from "@web/common/utils";
10+
1711
import { getHourLabels } from "@web/common/utils/web.date.util";
1812

1913
import {
@@ -23,53 +17,24 @@ import {
2317
} from "./styled";
2418
import { MainGridEvents } from "./MainGridEvents";
2519
import { MainGridColumns } from "../Columns/MainGridColumns";
26-
import {
27-
selectIsDrafting,
28-
selectIsDraftingExisting,
29-
} from "@web/ducks/events/selectors/draft.selectors";
3020

3121
interface Props {
32-
dateCalcs: DateCalcs;
33-
isSidebarOpen: boolean;
3422
mainGridRef: Ref_Callback;
3523
measurements: Measurements_Grid;
36-
today: Dayjs;
3724
scrollRef: Ref_Grid;
25+
today: Dayjs;
3826
weekProps: WeekProps;
3927
}
4028

4129
export const MainGrid: FC<Props> = ({
42-
dateCalcs,
43-
isSidebarOpen,
4430
mainGridRef,
4531
measurements,
4632
today,
4733
scrollRef,
4834
weekProps,
4935
}) => {
50-
const dispatch = useAppDispatch();
51-
5236
const { component } = weekProps;
5337
const { isCurrentWeek, week, weekDays } = component;
54-
const isDrafting = useAppSelector(selectIsDrafting);
55-
56-
//TODO remove
57-
// const onGridClick = async (e: MouseEvent) => {
58-
// if (isDrafting) {
59-
// console.log("todo discarding...");
60-
61-
// // todo next: this works for main grid, but now when
62-
// // clicking outside on any other element its
63-
// // still lingering the old draft cuz were not discarding there
64-
65-
// // ESC and repeating this flow also breaks
66-
// dispatch(draftSlice.actions.discard());
67-
// return;
68-
// }
69-
70-
// console.log("form not open, so starting draft");
71-
// await startTimedDraft(e);
72-
// };
7338

7439
return (
7540
<StyledMainGrid id={ID_GRID_MAIN} ref={mergeRefs([mainGridRef, scrollRef])}>
@@ -84,7 +49,7 @@ export const MainGrid: FC<Props> = ({
8449
{getHourLabels().map((dayTime, index) => (
8550
<StyledGridRow
8651
key={`${dayTime}-${index}:dayTimes`}
87-
// onClick={onGridClick}
52+
id={`gridRow-${index}`}
8853
/>
8954
))}
9055
</StyledGridWithTimeLabels>

packages/web/src/views/Calendar/hooks/draft/useDraftUtil.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,15 @@ export const useDraftUtil = (
7373
const [isFormOpen, setIsFormOpen] = useState(false);
7474
const onIsFormOpenChange = (isOpen: boolean) => {
7575
const formAlreadyOpen = isFormOpen === true;
76-
const shouldJustDiscard = formAlreadyOpen;
7776

78-
if (shouldJustDiscard) {
79-
// console.log("clicked out, discarding...");
80-
console.log("clicked out, just resetting...");
77+
if (formAlreadyOpen) {
78+
// console.log("clicked out, so reseting + discarding...");
79+
console.log("clicked out, so reseting ..");
8180
reset();
8281
// discard();
8382
return;
84-
} else {
85-
// we wont actually get here
86-
console.log("clicked empty, starting new ...");
8783
}
8884
setIsFormOpen(isOpen);
89-
console.log("closed form [onIsFormOpenChange]");
9085

9186
/*
9287
@@ -142,12 +137,12 @@ export const useDraftUtil = (
142137

143138
const handleChange = useCallback(async () => {
144139
if (isDrafting) {
145-
if (activity === "createShortcut") {
146-
const defaultDraft = await assembleDefaultEvent(
140+
if (activity === "createShortcut" || activity === "gridClick") {
141+
const defaultDraft = (await assembleDefaultEvent(
147142
reduxDraftType,
148143
reduxDraft?.startDate,
149144
reduxDraft?.endDate
150-
);
145+
)) as Schema_GridEvent;
151146
setDraft(defaultDraft);
152147
setIsFormOpen(true);
153148
return;
@@ -165,7 +160,6 @@ export const useDraftUtil = (
165160
setDateBeingChanged(dateToResize);
166161
}
167162
}
168-
// eslint-disable-next-line react-hooks/exhaustive-deps
169163
}, [
170164
activity,
171165
dateToResize,

packages/web/src/views/Calendar/hooks/draft/useGridDraft.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { DateCalcs } from "@web/views/Calendar/hooks/grid/useDateCalcs";
33
import { WeekProps } from "../useWeek";
44
import { useDraftUtil } from "./useDraftUtil";
55
import { useGridClick } from "./useGridClick";
6-
import { useGridMouseMove } from "./useGridMouseMove";
6+
import { useMouseHandlers } from "./useMouseHandlers";
77
import { Measurements_Grid } from "../grid/useGridLayout";
88

99
export const useGridDraft = (
@@ -19,7 +19,7 @@ export const useGridDraft = (
1919
);
2020

2121
useGridClick(draftState, draftUtil);
22-
useGridMouseMove(
22+
useMouseHandlers(
2323
draftState,
2424
draftUtil,
2525
dateCalcs,

0 commit comments

Comments
 (0)