Skip to content

Commit 588c948

Browse files
committed
refactor: extract draft actions into separate hook for improved modularity
This commit introduces a new useDraftActions hook that centralizes draft event management actions, further modularizing the draft event handling logic. Key changes include: - Extracting complex draft-related methods from useDraft - Improving separation of concerns - Simplifying the main draft hook's implementation - Maintaining consistent state management across draft interactions
1 parent 1e765a9 commit 588c948

File tree

10 files changed

+551
-447
lines changed

10 files changed

+551
-447
lines changed

packages/web/src/common/constants/web.constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const ID_GRID_ALLDAY_ROW = "allDayRow";
1212
export const ID_GRID_EVENTS_TIMED = "timedEvents";
1313
export const ID_GRID_MAIN = "mainGrid";
1414
export const ID_GRID_ROW = "gridRow";
15+
export const ID_GRID_ROW_CONTAINER = "mainGridContainer";
1516
export const ID_MAIN = "mainSection";
1617
export const ID_DATEPICKER_SIDEBAR = "sidebarDatePicker";
1718
export const ID_SOMEDAY_DRAFT = "somedayDraft";

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface Props {
1919
measurements: Measurements_Grid;
2020
startOfView: WeekProps["component"]["startOfView"];
2121
endOfView: WeekProps["component"]["endOfView"];
22-
onMouseDown: (e: MouseEvent, event: Schema_GridEvent) => void;
22+
onClick: (e: MouseEvent, event: Schema_GridEvent) => void;
2323
}
2424

2525
const AllDayEvent = ({
@@ -28,7 +28,7 @@ const AllDayEvent = ({
2828
measurements,
2929
startOfView,
3030
endOfView,
31-
onMouseDown,
31+
onClick,
3232
}: Props) => {
3333
const position = getEventPosition(
3434
event,
@@ -51,7 +51,7 @@ const AllDayEvent = ({
5151
isResizing={false}
5252
left={position.left}
5353
lineClamp={1}
54-
onMouseDown={(e) => onMouseDown(e, event)}
54+
onClick={(e: MouseEvent) => onClick(e, event)}
5555
priority={event.priority || Priorities.UNASSIGNED}
5656
role="button"
5757
top={position.top}

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

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import { Measurements_Grid } from "@web/views/Calendar/hooks/grid/useGridLayout"
55
import { WeekProps } from "@web/views/Calendar/hooks/useWeek";
66
import { useAppDispatch, useAppSelector } from "@web/store/store.hooks";
77
import { selectAllDayEvents } from "@web/ducks/events/selectors/event.selectors";
8-
import { selectDraftId } from "@web/ducks/events/selectors/draft.selectors";
8+
import {
9+
selectDraftId,
10+
selectIsDrafting,
11+
} from "@web/ducks/events/selectors/draft.selectors";
912
import { draftSlice } from "@web/ducks/events/slices/draft.slice";
1013
import { isSomedayEventFormOpen } from "@web/common/utils";
1114

@@ -16,25 +19,39 @@ interface Props {
1619
measurements: Measurements_Grid;
1720
startOfView: WeekProps["component"]["startOfView"];
1821
endOfView: WeekProps["component"]["endOfView"];
22+
onClick: (e: MouseEvent, event: Schema_GridEvent) => Promise<void>;
1923
}
2024
export const AllDayEvents = ({
2125
measurements,
2226
startOfView,
2327
endOfView,
28+
onClick,
2429
}: Props) => {
30+
// const dispatch = useAppDispatch();
2531
const allDayEvents = useAppSelector(selectAllDayEvents);
2632
const draftId = useAppSelector(selectDraftId);
27-
const dispatch = useAppDispatch();
33+
// const isDrafting = useAppSelector(selectIsDrafting);
2834

29-
const onMouseDown = (e: MouseEvent, event: Schema_GridEvent) => {
30-
e.stopPropagation();
35+
// const onClick = (e: MouseEvent, event: Schema_GridEvent) => {
36+
// e.stopPropagation();
3137

32-
if (isSomedayEventFormOpen()) {
33-
dispatch(draftSlice.actions.discard());
34-
}
38+
// if (isDrafting) {
39+
// console.log("todo: close draft");
40+
// } else {
41+
// dispatch(draftSlice.actions.)
42+
// console.log("todo open this draft");
43+
// }
3544

36-
dispatch(draftSlice.actions.startDragging({ event }));
37-
};
45+
// TODO remove - shouldn't need to do this anymore
46+
// if (isSomedayEventFormOpen()) {
47+
// console.log("discarding (someday form)");
48+
// dispatch(draftSlice.actions.discard());
49+
// return;
50+
// }
51+
52+
//TODO move this to onMouseDown?
53+
// dispatch(draftSlice.actions.startDragging({ event }));
54+
// };
3855

3956
return (
4057
<StyledEvents id={ID_GRID_ALLDAY_ROW}>
@@ -47,7 +64,7 @@ export const AllDayEvents = ({
4764
startOfView={startOfView}
4865
endOfView={endOfView}
4966
measurements={measurements}
50-
onMouseDown={onMouseDown}
67+
onClick={onClick}
5168
/>
5269
);
5370
})}

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import { isEventFormOpen } from "@web/common/utils";
2020
import { StyledGridCol } from "../Columns/styled";
2121
import { StyledAllDayColumns, StyledAllDayRow } from "./styled";
2222
import { AllDayEvents } from "./AllDayEvents";
23+
import { Schema_GridEvent } from "@web/common/types/web.event.types";
24+
import { selectIsDrafting } from "@web/ducks/events/selectors/draft.selectors";
2325

2426
interface Props {
2527
dateCalcs: DateCalcs;
@@ -40,13 +42,32 @@ export const AllDayRow: FC<Props> = ({
4042

4143
const { startOfView, weekDays } = weekProps.component;
4244
const rowsCount = useAppSelector(selectRowCount);
45+
const isDrafting = useAppSelector(selectIsDrafting);
4346

4447
useEffect(() => {
4548
measurements.remeasure(ID_GRID_MAIN);
4649
// eslint-disable-next-line react-hooks/exhaustive-deps
4750
}, [rowsCount]);
4851

49-
const startAlldayDraft = async (e: MouseEvent) => {
52+
const openAlldayDraft = async (e: MouseEvent, event: Schema_GridEvent) => {
53+
e.stopPropagation();
54+
55+
if (isDrafting) {
56+
console.log("todo: close draft");
57+
} else {
58+
console.log("opening draft for:", event.title);
59+
dispatch(
60+
draftSlice.actions.start({
61+
activity: "gridClick",
62+
eventType: Categories_Event.ALLDAY,
63+
event,
64+
})
65+
);
66+
}
67+
};
68+
69+
const startNewAlldayDraft = async (e: MouseEvent) => {
70+
//TODO move this to mouse handler?
5071
const x = getX(e, isSidebarOpen);
5172
const startDate = dateCalcs.getDateStrByXY(
5273
x,
@@ -73,14 +94,14 @@ export const AllDayRow: FC<Props> = ({
7394
return;
7495
}
7596

76-
await startAlldayDraft(e);
97+
await startNewAlldayDraft(e);
7798
};
7899

79100
return (
80101
<StyledAllDayRow
81102
id={ID_GRID_ALLDAY_CONTAINER}
82103
rowsCount={rowsCount}
83-
onMouseDown={onSectionMouseDown}
104+
// onMouseDown={onSectionMouseDown}
84105
>
85106
<StyledAllDayColumns id={ID_ALLDAY_COLUMNS} ref={allDayRef}>
86107
{weekDays.map((day) => (
@@ -91,6 +112,7 @@ export const AllDayRow: FC<Props> = ({
91112
measurements={measurements}
92113
startOfView={weekProps.component.startOfView}
93114
endOfView={weekProps.component.endOfView}
115+
onClick={openAlldayDraft}
94116
/>
95117
</StyledAllDayRow>
96118
);

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import { Dayjs } from "dayjs";
55
import { Ref_Callback } from "@web/common/types/util.types";
66
import { WeekProps } from "@web/views/Calendar/hooks/useWeek";
77
import { Ref_Grid } from "@web/views/Calendar/components/Grid/grid.types";
8-
import { ID_GRID_MAIN, ID_GRID_ROW } from "@web/common/constants/web.constants";
8+
import {
9+
ID_GRID_MAIN,
10+
ID_GRID_ROW_CONTAINER,
11+
ID_GRID_ROW,
12+
} from "@web/common/constants/web.constants";
913
import { Measurements_Grid } from "@web/views/Calendar/hooks/grid/useGridLayout";
1014

1115
import { getHourLabels } from "@web/common/utils/web.date.util";
@@ -45,7 +49,7 @@ export const MainGrid: FC<Props> = ({
4549
weekDays={weekDays}
4650
/>
4751

48-
<StyledGridWithTimeLabels>
52+
<StyledGridWithTimeLabels id={ID_GRID_ROW_CONTAINER}>
4953
{getHourLabels().map((dayTime, index) => (
5054
<StyledGridRow
5155
key={`${dayTime}-${index}:dayTimes`}

0 commit comments

Comments
 (0)