Skip to content

Commit 0345c70

Browse files
committed
fix(web): delete draft events immediately
1 parent 4ddc2b3 commit 0345c70

3 files changed

Lines changed: 67 additions & 15 deletions

File tree

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { type Schema_Event } from "@core/types/event.types";
77
import dayjs from "@core/util/date/dayjs";
88
import { type Props as DateTimeSectionProps } from "@web/views/Forms/EventForm/DateControlsSection/DateTimeSection/DateTimeSection";
99
import { getFormDates } from "@web/views/Forms/EventForm/DateControlsSection/DateTimeSection/form.datetime.util";
10-
import { beforeEach, describe, expect, it, mock } from "bun:test";
10+
import { afterEach, beforeEach, describe, expect, it, mock } from "bun:test";
1111

1212
type CapturedDateTimeSectionProps = Pick<
1313
DateTimeSectionProps,
@@ -36,7 +36,14 @@ mock.module(
3636
);
3737

3838
mock.module("@web/views/Forms/EventForm/EventActionMenu", () => ({
39-
EventActionMenu: () => <button type="button">Event actions</button>,
39+
EventActionMenu: ({ onDelete }: { onDelete: () => void }) => (
40+
<>
41+
<button type="button">Event actions</button>
42+
<button type="button" onClick={onDelete}>
43+
Delete event
44+
</button>
45+
</>
46+
),
4047
}));
4148

4249
mock.module("@web/views/Forms/EventForm/PrioritySection", () => ({
@@ -91,12 +98,18 @@ const createEvent = (overrides: Partial<Schema_Event> = {}): Schema_Event => ({
9198
});
9299

93100
describe("EventForm", () => {
101+
const originalConfirm = window.confirm;
102+
94103
beforeEach(() => {
95104
HotkeyManager.resetInstance();
96105
capturedDateControlsSectionProps = null;
97106
document.body.removeAttribute("data-app-locked");
98107
});
99108

109+
afterEach(() => {
110+
window.confirm = originalConfirm;
111+
});
112+
100113
it("renders the title before the actions on the same row", () => {
101114
render(
102115
<EventForm
@@ -185,6 +198,33 @@ describe("EventForm", () => {
185198
expect(afterTyping.defaultPrevented).toBe(false);
186199
});
187200

201+
it("closes a draft event immediately when deleting from the menu", async () => {
202+
const user = userEvent.setup();
203+
const onClose = mock();
204+
const onDelete = mock();
205+
const confirm = mock(() => true);
206+
window.confirm = confirm;
207+
208+
render(
209+
<EventForm
210+
event={createEvent({ _id: undefined, title: "Unsaved draft" })}
211+
isDraft={true}
212+
isExistingEvent={false}
213+
onClose={onClose}
214+
onDelete={onDelete}
215+
onDuplicate={mock()}
216+
onSubmit={mock()}
217+
setEvent={mock()}
218+
/>,
219+
);
220+
221+
await user.click(screen.getByRole("button", { name: "Delete event" }));
222+
223+
expect(confirm).not.toHaveBeenCalled();
224+
expect(onDelete).not.toHaveBeenCalled();
225+
expect(onClose).toHaveBeenCalledTimes(1);
226+
});
227+
188228
it("resets title editing state when an unsaved draft session changes", async () => {
189229
const user = userEvent.setup();
190230
const event = { ...createEvent(), _id: undefined, title: "" };

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { DateControlsSection } from "@web/views/Forms/EventForm/DateControlsSect
2929
import { getFormDates } from "@web/views/Forms/EventForm/DateControlsSection/DateTimeSection/form.datetime.util";
3030
import { RecurrenceSection } from "@web/views/Forms/EventForm/DateControlsSection/RecurrenceSection/RecurrenceSection";
3131
import { EventActionMenu } from "@web/views/Forms/EventForm/EventActionMenu";
32+
import { handleEventFormDelete } from "@web/views/Forms/EventForm/eventFormDelete.util";
3233
import { PrioritySection } from "@web/views/Forms/EventForm/PrioritySection";
3334
import { SaveSection } from "@web/views/Forms/EventForm/SaveSection";
3435
import { TitleActionsRow } from "@web/views/Forms/EventForm/TitleActionsRow";
@@ -262,6 +263,10 @@ export const EventForm: React.FC<Omit<FormProps, "category">> = memo(
262263
}, 1);
263264
}, [_onClose]);
264265

266+
const onDeleteEvent = useCallback(() => {
267+
handleEventFormDelete({ isDraft, onClose, onDelete });
268+
}, [isDraft, onClose, onDelete]);
269+
265270
const onDuplicateEvent = useCallback(() => {
266271
onDuplicate?.(event);
267272
onClose();
@@ -389,18 +394,7 @@ export const EventForm: React.FC<Omit<FormProps, "category">> = memo(
389394
setEvent: setLatestEvent,
390395
};
391396

392-
useAppHotkey(
393-
"Delete",
394-
() => {
395-
if (isDraft) {
396-
onClose();
397-
return;
398-
}
399-
400-
onDelete();
401-
},
402-
EVENT_FORM_PLAIN_HOTKEY_OPTIONS,
403-
);
397+
useAppHotkey("Delete", onDeleteEvent, EVENT_FORM_PLAIN_HOTKEY_OPTIONS);
404398

405399
useAppHotkey(
406400
"Enter",
@@ -498,7 +492,7 @@ export const EventForm: React.FC<Omit<FormProps, "category">> = memo(
498492
onConvert?.();
499493
}}
500494
onDuplicate={onDuplicateEvent}
501-
onDelete={onDelete}
495+
onDelete={onDeleteEvent}
502496
/>
503497
}
504498
/>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
interface DeleteEventArgs {
2+
isDraft: boolean;
3+
onClose: () => void;
4+
onDelete: () => void;
5+
}
6+
7+
export function handleEventFormDelete({
8+
isDraft,
9+
onClose,
10+
onDelete,
11+
}: DeleteEventArgs) {
12+
if (isDraft) {
13+
onClose();
14+
return;
15+
}
16+
17+
onDelete();
18+
}

0 commit comments

Comments
 (0)