Skip to content

Commit 128703e

Browse files
committed
🚚 Refactor: Move draft utility functions to common utils
1 parent 2ebcc7d commit 128703e

File tree

5 files changed

+93
-37
lines changed

5 files changed

+93
-37
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
} from "@web/views/Calendar/layout.constants";
13+
import { Categories_Event } from "@core/types/event.types";
14+
import { assembleDefaultEvent } from "@web/common/utils/event.util";
15+
import { DateCalcs } from "@web/views/Calendar/hooks/grid/useDateCalcs";
16+
import { Schema_GridEvent } from "@web/common/types/web.event.types";
17+
18+
export const assembleAlldayDraft = async (
19+
e: MouseEvent,
20+
dateCalcs: DateCalcs,
21+
isSidebarOpen: boolean,
22+
startOfView: Dayjs
23+
): Promise<Schema_GridEvent> => {
24+
const x = getX(e, isSidebarOpen);
25+
const _start = dateCalcs.getDateByXY(x, e.clientY, startOfView);
26+
const startDate = _start.format();
27+
const endDate = _start.add(1, "day").format();
28+
29+
const event = (await assembleDefaultEvent(
30+
Categories_Event.ALLDAY,
31+
startDate,
32+
endDate
33+
)) as Schema_GridEvent;
34+
return event;
35+
};
36+
37+
export const assembleTimedDraft = async (
38+
e: MouseEvent,
39+
dateCalcs: DateCalcs,
40+
isSidebarOpen: boolean,
41+
startOfView: Dayjs
42+
): Promise<Schema_GridEvent> => {
43+
const x = getX(e, isSidebarOpen);
44+
const _start = dateCalcs.getDateByXY(x, e.clientY, startOfView);
45+
const startDate = _start.format();
46+
const endDate = _start.add(DRAFT_DURATION_MIN, "minutes").format();
47+
48+
const event = (await assembleDefaultEvent(
49+
Categories_Event.TIMED,
50+
startDate,
51+
endDate
52+
)) as Schema_GridEvent;
53+
return event;
54+
};
55+
56+
export const getDraftTimes = (isCurrentWeek: boolean, startOfWeek: Dayjs) => {
57+
const currentMinute = dayjs().minute();
58+
const nextMinuteInterval = roundToNext(currentMinute, GRID_TIME_STEP);
59+
60+
const fullStart = isCurrentWeek ? dayjs() : startOfWeek.hour(dayjs().hour());
61+
const _start = fullStart.minute(nextMinuteInterval).second(0);
62+
63+
const _end = _start.add(1, "hour");
64+
const startDate = _start.format();
65+
const endDate = _end.format();
66+
67+
return { startDate, endDate };
68+
};
69+
70+
export const getDraftContainer = (isAllDay: boolean) => {
71+
if (isAllDay) {
72+
return getElemById(ID_GRID_EVENTS_ALLDAY);
73+
}
74+
75+
return getElemById(ID_GRID_EVENTS_TIMED);
76+
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { FC, useEffect, useState } from "react";
22
import { createPortal } from "react-dom";
33
import { Categories_Event } from "@core/types/event.types";
44
import { useAppSelector } from "@web/store/store.hooks";
5+
import { getDraftContainer } from "@web/common/utils/draft/draft.util";
56
import { useGridDraft } from "@web/views/Calendar/hooks/draft/grid/useGridDraft";
67
import { WeekProps } from "@web/views/Calendar/hooks/useWeek";
78
import { Measurements_Grid } from "@web/views/Calendar/hooks/grid/useGridLayout";
@@ -10,7 +11,6 @@ import { getCategory } from "@web/common/utils/event.util";
1011
import { useEventForm } from "@web/views/Forms/hooks/useEventForm";
1112
import { selectIsDrafting } from "@web/ducks/events/selectors/draft.selectors";
1213

13-
import { getDraftContainer } from "./draft.util";
1414
import { GridDraft } from "./GridDraft";
1515

1616
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/hooks/shortcuts/useShortcuts.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Key } from "ts-keycode-enum";
55
import { Dayjs } from "dayjs";
66
import { Categories_Event } from "@core/types/event.types";
77
import {
8+
Priorities,
89
SOMEDAY_MONTH_LIMIT_MSG,
910
SOMEDAY_WEEK_LIMIT_MSG,
1011
} from "@core/constants/core.constants";
@@ -13,18 +14,19 @@ import { isEventFormOpen } from "@web/common/utils";
1314
import { draftSlice } from "@web/ducks/events/slices/draft.slice";
1415
import { viewSlice } from "@web/ducks/events/slices/view.slice";
1516
import { ROOT_ROUTES } from "@web/common/constants/routes";
17+
import { Schema_GridEvent } from "@web/common/types/web.event.types";
1618
import {
1719
selectIsAtMonthlyLimit,
1820
selectIsAtWeeklyLimit,
1921
} from "@web/ducks/events/selectors/someday.selectors";
2022
import { assembleDefaultEvent } from "@web/common/utils/event.util";
23+
import { getDraftTimes } from "@web/common/utils/draft/draft.util";
2124
import { YEAR_MONTH_FORMAT } from "@core/constants/date.constants";
2225
import { selectSidebarTab } from "@web/ducks/events/selectors/view.selectors";
2326

2427
import { DateCalcs } from "../grid/useDateCalcs";
2528
import { Util_Scroll } from "../grid/useScroll";
2629
import { WeekProps } from "../useWeek";
27-
import { getDraftTimes } from "../../components/Event/Draft/draft.util";
2830

2931
export interface ShortcutProps {
3032
today: Dayjs;
@@ -97,14 +99,22 @@ export const useShortcuts = ({
9799
const _createTimedDraft = () => {
98100
const { startDate, endDate } = getDraftTimes(isCurrentWeek, startOfView);
99101

102+
const event: Schema_GridEvent = {
103+
startDate,
104+
endDate,
105+
priority: Priorities.UNASSIGNED,
106+
position: {
107+
isOverlapping: false,
108+
widthMultiplier: 1,
109+
horizontalOrder: 1,
110+
},
111+
};
112+
100113
dispatch(
101114
draftSlice.actions.start({
102115
activity: "createShortcut",
103116
eventType: Categories_Event.TIMED,
104-
event: {
105-
startDate,
106-
endDate,
107-
},
117+
event,
108118
})
109119
);
110120
};

packages/web/src/views/CmdPalette/CmdPalette.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ import { useAppDispatch, useAppSelector } from "@web/store/store.hooks";
1515
import { ROOT_ROUTES } from "@web/common/constants/routes";
1616
import { draftSlice } from "@web/ducks/events/slices/draft.slice";
1717
import { isEventFormOpen } from "@web/common/utils";
18+
import { getDraftTimes } from "@web/common/utils/draft/draft.util";
1819
import {
1920
selectIsAtMonthlyLimit,
2021
selectIsAtWeeklyLimit,
2122
} from "@web/ducks/events/selectors/someday.selectors";
2223
import { settingsSlice } from "@web/ducks/settings/slices/settings.slice";
2324
import { selectIsCmdPaletteOpen } from "@web/ducks/settings/selectors/settings.selectors";
2425

25-
import { getDraftTimes } from "../Calendar/components/Event/Draft/draft.util";
2626
import { ShortcutProps } from "../Calendar/hooks/shortcuts/useShortcuts";
2727

2828
const CmdPalette = ({

0 commit comments

Comments
 (0)