Skip to content

Commit f12d9b0

Browse files
committed
refactor: remove discardIfDrafting util and replace Schema_GridEvent with Schema_Event in Sidebar components and hooks
1 parent 946ba49 commit f12d9b0

File tree

5 files changed

+21
-34
lines changed

5 files changed

+21
-34
lines changed

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import {
88
import { useAppSelector } from "@web/store/store.hooks";
99
import { selectSidebarTab } from "@web/ducks/events/selectors/view.selectors";
1010

11-
import { SidebarContainer, SidebarTabContainer } from "./styled";
11+
import { useSidebar } from "../../hooks/draft/sidebar/useSidebar";
1212
import { SomedayTab } from "./SomedayTab/SomedayTab";
1313
import { SidebarIconRow } from "./SidebarIconRow";
1414
import { MonthTab } from "./MonthTab/MonthTab";
15-
import { useSidebar } from "../../hooks/draft/sidebar/useSidebar";
15+
import { SidebarContainer, SidebarTabContainer } from "./styled";
1616

1717
interface Props {
1818
dateCalcs: DateCalcs;
@@ -34,11 +34,7 @@ export const Sidebar: FC<Props & React.HTMLAttributes<HTMLDivElement>> = ({
3434
const sidebarProps = useSidebar(measurements, dateCalcs);
3535

3636
return (
37-
<SidebarContainer
38-
id="sidebar"
39-
role="complementary"
40-
onClick={sidebarProps.util.discardIfDrafting}
41-
>
37+
<SidebarContainer id="sidebar" role="complementary">
4238
<SidebarTabContainer>
4339
{tab === "tasks" && (
4440
<SomedayTab

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import {
1212
useInteractions,
1313
} from "@floating-ui/react";
1414
import { Categories_Event, Schema_Event } from "@core/types/event.types";
15-
import { Schema_GridEvent } from "@web/common/types/web.event.types";
1615
import { SIDEBAR_OPEN_WIDTH } from "@web/views/Calendar/layout.constants";
1716
import { SomedayEventForm } from "@web/views/Forms/SomedayEventForm";
1817
import { StyledFloatContainer } from "@web/views/Forms/SomedayEventForm/styled";
1918
import { useEventForm } from "@web/views/Forms/hooks/useEventForm";
2019
import { Util_Sidebar } from "@web/views/Calendar/hooks/draft/sidebar/useSidebarUtil";
20+
import { draftSlice } from "@web/ducks/events/slices/draft.slice";
21+
import { useAppDispatch } from "@web/store/store.hooks";
2122

2223
import { StyledNewSomedayEvent } from "./styled";
2324
import { SomedayEventRectangle } from "./SomedayEventRectangle";
@@ -49,17 +50,17 @@ function getStyle(
4950

5051
export interface Props {
5152
category: Categories_Event;
52-
event: Schema_GridEvent; //TODO make this Event (not grid)
53+
event: Schema_Event;
5354
isDrafting: boolean;
5455
isDragging: boolean;
5556
isOverGrid: boolean;
5657
onClose: () => void;
57-
onDraft: (event: Schema_GridEvent, category: Categories_Event) => void;
58+
onDraft: (event: Schema_Event, category: Categories_Event) => void;
5859
onMigrate: Util_Sidebar["onMigrate"];
5960
onSubmit: (event?: Schema_Event) => void;
6061
provided: DraggableProvided;
6162
snapshot: DraggableStateSnapshot;
62-
setEvent: Dispatch<SetStateAction<Schema_GridEvent>>;
63+
setEvent: Dispatch<SetStateAction<Schema_Event>>;
6364
}
6465

6566
export const SomedayEvent = ({
@@ -76,20 +77,24 @@ export const SomedayEvent = ({
7677
snapshot,
7778
setEvent,
7879
}: Props) => {
79-
const formType =
80-
category === Categories_Event.SOMEDAY_WEEK ? "sidebarWeek" : "sidebarMonth";
80+
const dispatch = useAppDispatch();
8181

82-
const initialFormOpen = event?.isOpen || (isDrafting && !isDragging);
83-
const [isFocused, setIsFocused] = useState(false);
82+
const initialFormOpen = isDrafting && !isDragging;
8483
const [isFormOpen, setIsFormOpen] = useState(initialFormOpen);
84+
const [isFocused, setIsFocused] = useState(false);
8585

8686
const onIsFormOpenChange = (isOpen: boolean) => {
8787
setIsFormOpen(isOpen);
8888
if (!isOpen) {
8989
setIsFocused(false);
9090
}
91+
92+
dispatch(draftSlice.actions.discard());
9193
};
9294

95+
const formType =
96+
category === Categories_Event.SOMEDAY_WEEK ? "sidebarWeek" : "sidebarMonth";
97+
9398
const { context, refs, strategy, y } = useEventForm(
9499
formType,
95100
isFormOpen,
@@ -100,8 +105,8 @@ export const SomedayEvent = ({
100105
const { getReferenceProps, getFloatingProps } = useInteractions([dismiss]);
101106

102107
useEffect(() => {
103-
setIsFormOpen(event?.isOpen || (isDrafting && !isDragging));
104-
}, [event?.isOpen, isDrafting, isDragging]);
108+
setIsFormOpen(isDrafting && !isDragging);
109+
}, [isDrafting, isDragging]);
105110

106111
const onKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {
107112
switch (e.key) {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import React, { FC, memo } from "react";
22
import { Categories_Event, Schema_Event } from "@core/types/event.types";
33
import { ID_SOMEDAY_DRAFT } from "@web/common/constants/web.constants";
4-
import { Schema_GridEvent } from "@web/common/types/web.event.types";
54
import { SidebarProps } from "@web/views/Calendar/hooks/draft/sidebar/useSidebar";
65

76
import { DraggableSomedayEvent } from "./DraggableSomedayEvent";
87

98
export const DraggableSomedayEvents: FC<{
109
category: Categories_Event;
1110
events: Schema_Event[];
12-
draft: Schema_GridEvent;
11+
draft: Schema_Event;
1312
isOverGrid: boolean;
1413
util: SidebarProps["util"];
1514
}> = memo(({ category, draft, events, isOverGrid, util }) => {

packages/web/src/views/Calendar/hooks/draft/sidebar/useSidebarState.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useState, useEffect } from "react";
2-
import { Schema_GridEvent } from "@web/common/types/web.event.types";
2+
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";
@@ -18,7 +18,7 @@ export const useSidebarState = (measurements: Measurements_Grid) => {
1818

1919
const { eventType: draftType } = useAppSelector(selectDraftStatus);
2020

21-
const [draft, setDraft] = useState<Schema_GridEvent | null>(null);
21+
const [draft, setDraft] = useState<Schema_Event | null>(null);
2222
const [isDrafting, setIsDrafting] = useState(false);
2323
const [isDraftingExisting, setIsDraftingExisting] = useState(false);
2424

packages/web/src/views/Calendar/hooks/draft/sidebar/useSidebarUtil.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -199,18 +199,6 @@ export const useSidebarUtil = (
199199
);
200200
};
201201

202-
const discardIfDrafting = () => {
203-
if (state.isDrafting) {
204-
console.log("drafting in sidebar, so discarding");
205-
dispatch(draftSlice.actions.discard());
206-
close();
207-
return;
208-
}
209-
if (isDraftingOnGrid) {
210-
dispatch(draftSlice.actions.discard());
211-
}
212-
};
213-
214202
const onDragEnd = (result: DropResult) => {
215203
const { destination, draggableId, source } = result;
216204

@@ -412,7 +400,6 @@ export const useSidebarUtil = (
412400
return {
413401
close,
414402
createDefaultSomeday,
415-
discardIfDrafting,
416403
onDraft,
417404
onDragEnd,
418405
onDragStart,

0 commit comments

Comments
 (0)