Skip to content

Commit aeec0dd

Browse files
✨ Feat: event window not reopening (#341)
* chore(web): refactor `GridDraft` initiate drag logic Replacing `setIsDragging` with `startDragging` makes code more predictable and easier to maintain * feat(web): re-open window after dragging event if it was open before drag
1 parent a8a729e commit aeec0dd

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

packages/web/src/views/Calendar/components/Draft/grid/GridDraft.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ interface Props {
2121

2222
export const GridDraft: FC<Props> = ({ measurements, weekProps }) => {
2323
const { actions, setters, state } = useDraftContext();
24-
const { discard, deleteEvent, submit } = actions;
25-
const { setDraft, setDateBeingChanged, setIsDragging, setIsResizing } =
26-
setters;
24+
const { discard, deleteEvent, submit, startDragging } = actions;
25+
const { setDraft, setDateBeingChanged, setIsResizing } = setters;
2726
const { draft, isDragging, formProps, isFormOpen, isResizing } = state;
2827
const { context, getReferenceProps, getFloatingProps, x, y, refs, strategy } =
2928
formProps;
@@ -37,7 +36,7 @@ export const GridDraft: FC<Props> = ({ measurements, weekProps }) => {
3736

3837
const handleClick = () => {};
3938
const handleDrag = () => {
40-
setIsDragging(true);
39+
startDragging();
4140
};
4241

4342
const { onMouseDown } = useGridEventMouseDown(

packages/web/src/views/Calendar/components/Draft/hooks/actions/useDraftActions.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const useDraftActions = (
4747
const isAtWeeklyLimit = useAppSelector(selectIsAtWeeklyLimit);
4848
const somedayWeekCount = useAppSelector(selectSomedayWeekCount);
4949
const reduxDraft = useAppSelector(selectDraft);
50+
5051
const {
5152
activity,
5253
dateToResize,
@@ -61,6 +62,8 @@ export const useDraftActions = (
6162
isDragging,
6263
isResizing,
6364
resizeStatus,
65+
isFormOpen,
66+
isFormOpenBeforeDragging,
6467
} = draftState;
6568

6669
const {
@@ -71,6 +74,7 @@ export const useDraftActions = (
7174
setDateBeingChanged,
7275
setDraft,
7376
setIsFormOpen,
77+
setIsFormOpenBeforeDragging,
7478
} = setters;
7579

7680
const startDragging = useCallback(() => {
@@ -87,6 +91,7 @@ export const useDraftActions = (
8791
const stopDragging = () => {
8892
setIsDragging(false);
8993
setDragStatus(null);
94+
setIsFormOpenBeforeDragging(null);
9095
};
9196

9297
const stopResizing = () => {
@@ -122,7 +127,11 @@ export const useDraftActions = (
122127
dispatch(createEventSlice.actions.request(event));
123128
}
124129

125-
discard();
130+
if (isFormOpenBeforeDragging) {
131+
openForm();
132+
} else {
133+
discard();
134+
}
126135
};
127136

128137
const closeForm = () => {
@@ -411,6 +420,14 @@ export const useDraftActions = (
411420
openForm,
412421
reset,
413422
resize,
423+
startDragging: () => {
424+
// Placing `setIsFormOpenBeforeDragging` here rather than inside `startDragging`
425+
// because `setIsFormOpenBeforeDragging` depends on `isFormOpen` and re-calculates
426+
// `startDragging` (due to it being a react callback) which causes issues.
427+
// This is a hacky solution to the issue.
428+
setIsFormOpenBeforeDragging(isFormOpen);
429+
startDragging();
430+
},
414431
stopDragging,
415432
stopResizing,
416433
};

packages/web/src/views/Calendar/components/Draft/hooks/state/useDraftState.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface State_Draft_Local {
1616
isDragging: boolean;
1717
isResizing: boolean;
1818
isFormOpen: boolean;
19+
isFormOpenBeforeDragging: boolean | null;
1920
resizeStatus: Status_Resize | null;
2021
}
2122

@@ -27,6 +28,7 @@ export interface Setters_Draft {
2728
setResizeStatus: (value: Status_Resize | null) => void;
2829
setDateBeingChanged: (value: "startDate" | "endDate" | null) => void;
2930
setIsFormOpen: (value: boolean) => void;
31+
setIsFormOpenBeforeDragging: (value: boolean | null) => void;
3032
}
3133

3234
export const useDraftState = () => {
@@ -39,6 +41,9 @@ export const useDraftState = () => {
3941
"startDate" | "endDate" | null
4042
>("endDate");
4143
const [isFormOpen, setIsFormOpen] = useState(false);
44+
const [isFormOpenBeforeDragging, setIsFormOpenBeforeDragging] = useState<
45+
boolean | null
46+
>(null);
4247

4348
const state: State_Draft_Local = {
4449
draft,
@@ -48,6 +53,7 @@ export const useDraftState = () => {
4853
isResizing,
4954
resizeStatus,
5055
dateBeingChanged,
56+
isFormOpenBeforeDragging,
5157
};
5258

5359
const setters: Setters_Draft = {
@@ -58,6 +64,7 @@ export const useDraftState = () => {
5864
setResizeStatus,
5965
setDateBeingChanged,
6066
setIsFormOpen,
67+
setIsFormOpenBeforeDragging,
6168
};
6269

6370
return { state, setters };

0 commit comments

Comments
 (0)