Skip to content

Commit fc5670f

Browse files
committed
refactor(web): remove unused isEventDirty logic and related props from event forms
1 parent 2aee3a2 commit fc5670f

File tree

8 files changed

+36
-231
lines changed

8 files changed

+36
-231
lines changed

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ export const GridDraft: FC<Props> = ({ measurements, weekProps }) => {
6363
handleDrag,
6464
);
6565

66-
const formDirty = useMemo(
67-
() => (draft ? actions.isEventDirty(draft) : false),
68-
[draft, actions.isEventDirty],
69-
);
70-
7166
if (!draft) return null;
7267

7368
return (
@@ -115,7 +110,6 @@ export const GridDraft: FC<Props> = ({ measurements, weekProps }) => {
115110
onConvert={onConvert}
116111
onDelete={onDelete}
117112
onDuplicate={duplicateEvent}
118-
disableSaveBtn={!formDirty}
119113
onSubmit={onSubmit}
120114
setEvent={setDraft}
121115
/>

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

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

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

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
import { devAlert } from "@core/util/app.util";
1616
import dayjs, { Dayjs } from "@core/util/date/dayjs";
1717
import { getUserId } from "@web/auth/auth.util";
18+
import { isEventDirty } from "@web/common/parsers/event.parser";
1819
import { PartialMouseEvent } from "@web/common/types/util.types";
1920
import {
2021
Schema_GridEvent,
@@ -155,33 +156,6 @@ export const useDraftActions = (
155156
[reduxDraft, isRecurrence],
156157
);
157158

158-
const isEventDirty = useCallback(
159-
(currentDraft: Schema_WebEvent): boolean => {
160-
if (!reduxDraft) return true; // New event is always dirty
161-
162-
// Compare relevant fields that can change in the form
163-
const fieldsToCompare = [
164-
"title",
165-
"description",
166-
"startDate",
167-
"endDate",
168-
"priority",
169-
"recurrence",
170-
] as const;
171-
172-
return fieldsToCompare.some((field) => {
173-
const current = currentDraft[field];
174-
const original = reduxDraft[field];
175-
const recurrence = field === "recurrence";
176-
177-
return recurrence
178-
? isRecurrenceChanged(currentDraft)
179-
: current !== original;
180-
});
181-
},
182-
[reduxDraft, isRecurrenceChanged],
183-
);
184-
185159
const closeForm = useCallback(() => {
186160
setIsFormOpen(false);
187161
}, [setIsFormOpen]);
@@ -262,24 +236,44 @@ export const useDraftActions = (
262236
setIsFormOpen(true);
263237
}, [setIsFormOpen]);
264238

239+
const determineSubmitAction = useCallback(
240+
(draft: Schema_WebEvent) => {
241+
const isExisting = draft._id;
242+
const isOptimistic = draft._id?.startsWith(ID_OPTIMISTIC_PREFIX);
243+
if (isExisting && !isOptimistic) {
244+
if (isFormOpenBeforeDragging) {
245+
openForm();
246+
return "OPEN_FORM";
247+
}
248+
const isSame = !isEventDirty(draft, reduxDraft);
249+
if (isSame) {
250+
// no need to make HTTP request
251+
discard();
252+
return "DISCARD";
253+
}
254+
}
255+
return "SUBMIT";
256+
},
257+
[reduxDraft, isFormOpenBeforeDragging, openForm, discard],
258+
);
259+
265260
const submit = useCallback(
266261
async (
267262
draft: Schema_GridEvent,
268263
applyTo: RecurringEventUpdateScope = RecurringEventUpdateScope.THIS_EVENT,
269264
) => {
270-
// For new events, skip the dirty check and allow saving blank events
271-
const isNewEvent =
272-
!draft._id || draft._id.startsWith(ID_OPTIMISTIC_PREFIX);
273-
274-
// Check if the event has actually changed (skip for new events)
275-
if (!isNewEvent && !isEventDirty(draft)) {
276-
// No changes detected, just close the form without making HTTP request
277-
if (isFormOpenBeforeDragging) {
265+
const action = determineSubmitAction(draft);
266+
switch (action) {
267+
case "OPEN_FORM":
278268
openForm();
279-
} else {
269+
return;
270+
case "DISCARD":
280271
discard();
281-
}
282-
return;
272+
return;
273+
case "SUBMIT":
274+
default:
275+
// Continue with the submit logic below
276+
break;
283277
}
284278

285279
const userId = await getUserId();
@@ -293,8 +287,9 @@ export const useDraftActions = (
293287
}
294288

295289
const { startOfView, endOfView } = weekProps.component;
290+
const isExisting = draft._id;
296291

297-
if (!isNewEvent) {
292+
if (isExisting) {
298293
const isDateWithinView = (date: string) =>
299294
dayjs(date).isBetween(startOfView, endOfView, null, "[]");
300295

@@ -344,7 +339,6 @@ export const useDraftActions = (
344339
}
345340
},
346341
[
347-
isEventDirty,
348342
isFormOpenBeforeDragging,
349343
weekProps,
350344
currentWeekEvents,
@@ -631,7 +625,6 @@ export const useDraftActions = (
631625
openForm,
632626
reset,
633627
resize,
634-
isEventDirty,
635628
isInstance,
636629
isRecurrence,
637630
isRecurrenceChanged,

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const useDraftConfirmation = ({
99
}: Omit<ReturnType<typeof useDraftContext>, "setters" | "confirmation">) => {
1010
const { discard, deleteEvent, submit } = actions;
1111
const { isRecurrenceChanged } = actions;
12-
const { isInstance, isRecurrence, isEventDirty } = actions;
12+
const { isInstance, isRecurrence } = actions;
1313
const { draft } = state;
1414

1515
const recurrenceChanged = useMemo(
@@ -45,9 +45,6 @@ export const useDraftConfirmation = ({
4545
const isRecurringEvent = isRecurrence();
4646
const instanceEvent = isInstance();
4747
const toStandAlone = instanceEvent && rule === null;
48-
const dirty = isEventDirty(_draft);
49-
50-
if (!dirty) return discard();
5148

5249
if (!toStandAlone && isRecurringEvent) {
5350
setFinalDraft(_draft);
@@ -69,7 +66,6 @@ export const useDraftConfirmation = ({
6966
submit,
7067
setRecurrenceUpdateScopeDialogOpen,
7168
setFinalDraft,
72-
isEventDirty,
7369
isRecurrence,
7470
isInstance,
7571
discard,

packages/web/src/views/Forms/EventForm/EventForm.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ export const EventForm: React.FC<FormProps> = ({
4545
onSubmit,
4646
onDuplicate,
4747
setEvent,
48-
disableSaveBtn,
4948
...props
5049
}) => {
5150
const { title } = event || {};
@@ -350,7 +349,6 @@ export const EventForm: React.FC<FormProps> = ({
350349
/>
351350

352351
<SaveSection
353-
disableSaveBtn={disableSaveBtn}
354352
priority={priority}
355353
onSubmit={onSubmitForm}
356354
onCancel={onClose}

packages/web/src/views/Forms/EventForm/SaveSection/SaveSection.test.tsx

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,6 @@ describe("SaveSection", () => {
8989
expect(mockOnCancel).toHaveBeenCalledTimes(1);
9090
});
9191

92-
it("does not call onSubmit when save button is disabled and clicked", async () => {
93-
render(<SaveSection onSubmit={mockOnSubmit} disableSaveBtn={true} />);
94-
95-
const saveButton = screen.getByRole("tab", { name: "Save" });
96-
// Use fireEvent instead of userEvent for disabled elements
97-
fireEvent.click(saveButton);
98-
99-
expect(mockOnSubmit).not.toHaveBeenCalled();
100-
});
101-
10292
it("handles keyboard navigation with tabIndex", () => {
10393
render(<SaveSection onSubmit={mockOnSubmit} onCancel={mockOnCancel} />);
10494

packages/web/src/views/Forms/EventForm/SaveSection/SaveSection.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { StyledSubmitRow } from "../styled";
99
interface Props {
1010
saveText?: string;
1111
cancelText?: string;
12-
disableSaveBtn?: boolean;
1312
onSubmit: () => void;
1413
onCancel?: () => void;
1514
priority?: Priority;
@@ -19,14 +18,10 @@ export const SaveSection: React.FC<Props> = ({
1918
saveText = "Save",
2019
cancelText = "Cancel",
2120
onSubmit: _onSubmit,
22-
disableSaveBtn,
2321
onCancel,
2422
priority,
2523
}) => {
26-
const onSave = useCallback(
27-
() => (disableSaveBtn ? null : _onSubmit()),
28-
[disableSaveBtn, _onSubmit],
29-
);
24+
const onSave = useCallback(() => _onSubmit(), [_onSubmit]);
3025

3126
return (
3227
<StyledSubmitRow>
@@ -54,7 +49,6 @@ export const SaveSection: React.FC<Props> = ({
5449
<StyledSaveBtn
5550
minWidth={110}
5651
priority={priority!}
57-
disabled={disableSaveBtn}
5852
role="tab"
5953
tabIndex={0}
6054
aria-keyshortcuts="Meta+Enter"

0 commit comments

Comments
 (0)