From d30979893e293ea6db4aa106b53d295c33320983 Mon Sep 17 00:00:00 2001 From: HeWillCook Date: Sun, 15 Nov 2026 21:50:45 -0500 Subject: [PATCH] feat: hosts can delete an event Danger zone on the edit page with an inline confirmation; the delete is scoped to the signed-in host and cascades to everything on the event. The dashboard confirms with a dismissible notice. Co-Authored-By: Claude Opus 5.5 --- src/app/host/events/[id]/edit/actions.test.ts | 17 +++- src/app/host/events/[id]/edit/actions.ts | 18 ++++- src/app/host/events/[id]/edit/page.tsx | 7 +- src/app/host/page.tsx | 19 ++++- src/components/DeleteEventSection.test.tsx | 47 +++++++++++ src/components/DeleteEventSection.tsx | 77 +++++++++++++++++++ src/components/ui/Button.tsx | 6 +- src/services/events.test.ts | 34 ++++++++ src/services/events.ts | 18 +++++ 9 files changed, 236 insertions(+), 7 deletions(-) create mode 100644 src/components/DeleteEventSection.test.tsx create mode 100644 src/components/DeleteEventSection.tsx diff --git a/src/app/host/events/[id]/edit/actions.test.ts b/src/app/host/events/[id]/edit/actions.test.ts index 20861ff..0ff6340 100644 --- a/src/app/host/events/[id]/edit/actions.test.ts +++ b/src/app/host/events/[id]/edit/actions.test.ts @@ -21,7 +21,7 @@ vi.mock("next/navigation", () => ({ }, })); -import { updateEventAction } from "./actions"; +import { deleteEventAction, updateEventAction } from "./actions"; let db: Db; let eventId: string; @@ -75,3 +75,18 @@ describe("updateEventAction", () => { expect((await getHostEvent(db, "host-1", eventId)).title).toBe("Cookout"); }); }); + +describe("deleteEventAction", () => { + it("deletes the event and redirects to the dashboard with its title", async () => { + await expect(deleteEventAction(eventId)).rejects.toThrow( + `NEXT_REDIRECT:/host?deleted=${encodeURIComponent("Cookout")}`, + ); + await expect(getHostEvent(db, "host-1", eventId)).rejects.toThrow(); + }); + + it("a different signed-in host gets a 404 and the event stays", async () => { + session.hostId = "host-2"; + await expect(deleteEventAction(eventId)).rejects.toThrow("NEXT_NOT_FOUND"); + expect((await getHostEvent(db, "host-1", eventId)).title).toBe("Cookout"); + }); +}); diff --git a/src/app/host/events/[id]/edit/actions.ts b/src/app/host/events/[id]/edit/actions.ts index 3551526..55a4c30 100644 --- a/src/app/host/events/[id]/edit/actions.ts +++ b/src/app/host/events/[id]/edit/actions.ts @@ -6,11 +6,11 @@ import type { EditEventState } from "@/components/EditEventForm"; import { getDb } from "@/db/client"; import { requireHost } from "@/server/auth"; import { NotFoundError, ValidationError } from "@/services/errors"; -import { updateEvent } from "@/services/events"; +import { deleteEvent, updateEvent } from "@/services/events"; // The host comes from the session. The event id arrives through `bind`, but in Next 16 // bound arguments are sent to the browser and can be altered, so they are NOT protected: -// this is safe only because updateEvent re-checks that the event belongs to the host. +// this is safe only because these services re-check that the event belongs to the host. function text(formData: FormData, key: string): string { const v = formData.get(key); @@ -41,3 +41,17 @@ export async function updateEventAction( revalidatePath("/host"); redirect(`/host/events/${encodeURIComponent(eventId)}`); } + +/** Deletes the event (and everything on it), then returns to the dashboard to confirm. */ +export async function deleteEventAction(eventId: string): Promise { + const host = await requireHost(); + let title: string; + try { + ({ title } = await deleteEvent(await getDb(), host.id, eventId)); + } catch (error) { + if (error instanceof NotFoundError) notFound(); + throw error; + } + revalidatePath("/host"); + redirect(`/host?deleted=${encodeURIComponent(title)}`); +} diff --git a/src/app/host/events/[id]/edit/page.tsx b/src/app/host/events/[id]/edit/page.tsx index 840fbb1..a606c63 100644 --- a/src/app/host/events/[id]/edit/page.tsx +++ b/src/app/host/events/[id]/edit/page.tsx @@ -1,7 +1,8 @@ +import { DeleteEventSection } from "@/components/DeleteEventSection"; import { EditEventForm } from "@/components/EditEventForm"; import { dateToZonedLocal } from "@/lib/zoned-time"; import { loadHostEvent } from "@/server/host-event"; -import { updateEventAction } from "./actions"; +import { deleteEventAction, updateEventAction } from "./actions"; // Reads the session and the database, so it must never be prerendered. export const dynamic = "force-dynamic"; @@ -23,6 +24,10 @@ export default async function EditEventPage({ params }: PageProps<"/host/events/ action={updateEventAction.bind(null, event.id)} backHref={`/host/events/${encodeURIComponent(event.id)}`} /> + ); } diff --git a/src/app/host/page.tsx b/src/app/host/page.tsx index 12fbf61..2ada45b 100644 --- a/src/app/host/page.tsx +++ b/src/app/host/page.tsx @@ -12,12 +12,15 @@ import { listHostEvents } from "@/services/events"; // Reads the session and the database, so it must never be prerendered. export const dynamic = "force-dynamic"; -export default async function HostDashboardPage() { +export default async function HostDashboardPage({ searchParams }: PageProps<"/host">) { const host = await requireHost(); const [events, baseUrl] = await Promise.all([ getDb().then((db) => listHostEvents(db, host.id)), getBaseUrl(), ]); + // Set by deleteEventAction; the notice stays until the host follows the Dismiss link. + const deleted = (await searchParams).deleted; + const deletedTitle = typeof deleted === "string" && deleted.trim() ? deleted : null; return (
@@ -38,6 +41,20 @@ export default async function HostDashboardPage() { + {deletedTitle ? ( +
+

+ Deleted “{deletedTitle}”. +

+ + Dismiss + +
+ ) : null} + {events.length === 0 ? (
diff --git a/src/components/DeleteEventSection.test.tsx b/src/components/DeleteEventSection.test.tsx new file mode 100644 index 0000000..e13dba1 --- /dev/null +++ b/src/components/DeleteEventSection.test.tsx @@ -0,0 +1,47 @@ +import { fireEvent, render, screen, waitFor } from "@testing-library/react"; +import { describe, expect, it, vi } from "vitest"; +import { DeleteEventSection } from "./DeleteEventSection"; + +const renderSection = () => + render(); + +describe("DeleteEventSection", () => { + it("shows only the Delete event button until it is clicked", () => { + renderSection(); + expect(screen.getByRole("button", { name: "Delete event" })).toBeInTheDocument(); + expect(screen.queryByRole("button", { name: "Yes, delete it" })).not.toBeInTheDocument(); + expect(screen.queryByText(/permanently removes/)).not.toBeInTheDocument(); + }); + + it("reveals a confirmation naming the event, with focus on the confirm button", () => { + renderSection(); + fireEvent.click(screen.getByRole("button", { name: "Delete event" })); + + expect( + screen.getByText(/permanently removes all RSVPs, the menu, shopping and split data/), + ).toBeInTheDocument(); + expect(screen.getByText("Burger Night")).toBeInTheDocument(); + expect(screen.getByRole("button", { name: "Yes, delete it" })).toHaveFocus(); + expect(screen.queryByRole("button", { name: "Delete event" })).not.toBeInTheDocument(); + }); + + it("hides the confirmation again when Cancel is pressed", () => { + renderSection(); + fireEvent.click(screen.getByRole("button", { name: "Delete event" })); + fireEvent.click(screen.getByRole("button", { name: "Cancel" })); + + expect(screen.queryByRole("button", { name: "Yes, delete it" })).not.toBeInTheDocument(); + expect(screen.queryByText(/permanently removes/)).not.toBeInTheDocument(); + expect(screen.getByRole("button", { name: "Delete event" })).toBeInTheDocument(); + }); + + it("submits the bound delete action from the confirmation", async () => { + const action = vi.fn(); + render(); + fireEvent.click(screen.getByRole("button", { name: "Delete event" })); + fireEvent.click(screen.getByRole("button", { name: "Yes, delete it" })); + + await waitFor(() => expect(action).toHaveBeenCalledTimes(1)); + expect(action.mock.calls[0][0]).toBeInstanceOf(FormData); + }); +}); diff --git a/src/components/DeleteEventSection.tsx b/src/components/DeleteEventSection.tsx new file mode 100644 index 0000000..735ef66 --- /dev/null +++ b/src/components/DeleteEventSection.tsx @@ -0,0 +1,77 @@ +"use client"; + +import { useEffect, useId, useRef, useState, type RefObject } from "react"; +import { useFormStatus } from "react-dom"; +import { Button } from "./ui/Button"; +import { Card } from "./ui/Card"; + +/** A server action already bound to the event id (see deleteEventAction). */ +export type DeleteEventAction = (formData: FormData) => void | Promise; + +/** The two confirm buttons; inside the form so useFormStatus sees its pending state. */ +function DeleteActions({ + confirmRef, + describedBy, + onCancel, +}: { + confirmRef: RefObject; + describedBy: string; + onCancel: () => void; +}) { + const { pending } = useFormStatus(); + return ( +
+ + +
+ ); +} + +/** "Danger zone": Delete event reveals an inline confirmation, then submits the action. */ +export function DeleteEventSection({ title, action }: { title: string; action: DeleteEventAction }) { + const [confirming, setConfirming] = useState(false); + const confirmRef = useRef(null); + const questionId = useId(); + + // The confirm step replaces the button that was pressed, so put focus on its first action. + useEffect(() => { + if (confirming) confirmRef.current?.focus(); + }, [confirming]); + + return ( + +

Danger zone

+

+ Deleting an event can’t be undone. +

+ + {confirming ? ( +
+

+ Delete {title}? This permanently removes all + RSVPs, the menu, shopping and split data. +

+ setConfirming(false)} + /> + + ) : ( + + )} +
+ ); +} diff --git a/src/components/ui/Button.tsx b/src/components/ui/Button.tsx index b52d49a..77dc566 100644 --- a/src/components/ui/Button.tsx +++ b/src/components/ui/Button.tsx @@ -1,13 +1,15 @@ -import type { ButtonHTMLAttributes } from "react"; +import type { ComponentPropsWithRef } from "react"; const variants = { red: "bg-ketchup text-paper shadow-[inset_0_-3px_0_rgb(0_0_0/0.18)] hover:bg-ketchup-deep", mustard: "bg-mustard text-ink shadow-[inset_0_-3px_0_rgb(0_0_0/0.12)] hover:bg-mustard-deep", outline: "border-[1.5px] border-ink/70 bg-paper text-ink hover:border-ink hover:bg-cream/40", + danger: + "border-[1.5px] border-ketchup bg-paper text-ketchup hover:border-ketchup-deep hover:bg-ketchup/5 hover:text-ketchup-deep", } as const; -export type ButtonProps = ButtonHTMLAttributes & { +export type ButtonProps = ComponentPropsWithRef<"button"> & { variant: keyof typeof variants; }; diff --git a/src/services/events.test.ts b/src/services/events.test.ts index 64b7c34..e6313d8 100644 --- a/src/services/events.test.ts +++ b/src/services/events.test.ts @@ -1,6 +1,8 @@ // @vitest-environment node import { afterEach, beforeEach, describe, expect, it } from "vitest"; import type { Db } from "@/db/client"; +import { eventDishes, events, guests, invitees, plusOnes } from "@/db/schema"; +import { seedStarterRecipes } from "@/db/seed"; import { createTestDb } from "@/db/test-db"; import { formatWhen } from "@/lib/format"; import { zonedLocalToDate } from "@/lib/zoned-time"; @@ -8,12 +10,14 @@ import { NotFoundError, ValidationError } from "./errors"; import { addInvitees, createEvent, + deleteEvent, getHostEvent, headcountForEvents, listHostEvents, noReplyNames, updateEvent, } from "./events"; +import { addDishToEvent } from "./menu"; import { getPublicEvent, submitRsvp } from "./rsvp"; let db: Db; @@ -235,6 +239,36 @@ describe("updateEvent", () => { }); }); +describe("deleteEvent", () => { + it("deletes the host's event and cascades its guests, plus-ones, invitees and dishes", async () => { + await seedStarterRecipes(db); + const { id, shareCode } = await createEvent(db, "host-1", input); + await submitRsvp(db, { shareCode, name: "Alex", status: "yes", plusOneNames: ["Sam", "Jo"] }); + await addInvitees(db, "host-1", id, ["Bea"]); + await addDishToEvent(db, "host-1", id, "starter-burgers"); + await createEvent(db, "host-2", { ...input, title: "Not mine" }); + + expect(await deleteEvent(db, "host-1", id)).toEqual({ title: "Fourth of July" }); + + expect((await db.select().from(events)).map((e) => e.title)).toEqual(["Not mine"]); + expect(await db.select().from(guests)).toHaveLength(0); + expect(await db.select().from(plusOnes)).toHaveLength(0); + expect(await db.select().from(invitees)).toHaveLength(0); + expect(await db.select().from(eventDishes)).toHaveLength(0); + await expect(getPublicEvent(db, shareCode)).rejects.toBeInstanceOf(NotFoundError); + }); + + it("is a NotFoundError for another host's event and leaves it in place", async () => { + const { id } = await createEvent(db, "host-1", input); + await expect(deleteEvent(db, "host-2", id)).rejects.toBeInstanceOf(NotFoundError); + expect((await getHostEvent(db, "host-1", id)).title).toBe("Fourth of July"); + }); + + it("is a NotFoundError for an unknown id", async () => { + await expect(deleteEvent(db, "host-1", "missing")).rejects.toBeInstanceOf(NotFoundError); + }); +}); + describe("event time zone", () => { const originalTz = process.env.TZ; diff --git a/src/services/events.ts b/src/services/events.ts index 515f3e9..24587d4 100644 --- a/src/services/events.ts +++ b/src/services/events.ts @@ -111,6 +111,24 @@ export async function updateEvent( .where(and(eq(events.id, eventId), eq(events.hostId, hostId))); } +/** + * Deletes the host's event and returns its title (for a confirmation message). + * Every table that hangs off an event — guests and their plus-ones, dishes, + * invitees, shopping ticks, extra costs — cascades in the database. + */ +export async function deleteEvent( + db: Db, + hostId: string, + eventId: string, +): Promise<{ title: string }> { + const [deleted] = await db + .delete(events) + .where(and(eq(events.id, eventId), eq(events.hostId, hostId))) + .returning({ title: events.title }); + if (!deleted) throw new NotFoundError("Event not found"); + return deleted; +} + /** The host's events, soonest first, each with its planning headcount. */ export async function listHostEvents(db: Db, hostId: string): Promise { const rows = await db