Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 36 additions & 15 deletions src/app/e/[code]/g/[token]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import type { Metadata } from "next";
import Link from "next/link";
import { notFound } from "next/navigation";
import { CancelledEventNotice } from "@/components/CancelledEventNotice";
import { EventUnavailable } from "@/components/EventUnavailable";
import { RsvpForm } from "@/components/RsvpForm";
import { getDb } from "@/db/client";
import type { RsvpStatus } from "@/domain/headcount";
import { eventState } from "@/lib/event-state";
import { formatWhen } from "@/lib/format";
import { NotFoundError } from "@/services/errors";
import { getGuestByToken, getPublicEvent } from "@/services/rsvp";
Expand Down Expand Up @@ -40,6 +43,24 @@ export default async function GuestEditPage({ params }: PageProps<"/e/[code]/g/[
throw error;
}

const state = eventState(event);
if (state === "deleted") return <EventUnavailable hostName={event.hostName} />;
const cancelled = state === "cancelled";

const personalNote = (
<p className="px-2 text-xs text-ink-muted">
This is your personal link — don&apos;t share it.{" "}
{cancelled ? null : (
<Link
href={`/e/${encodeURIComponent(code)}?new=1`}
className="font-bold text-ketchup underline"
>
Not you? Answer as someone else
</Link>
)}
</p>
);

return (
<main>
<RsvpForm
Expand All @@ -51,23 +72,23 @@ export default async function GuestEditPage({ params }: PageProps<"/e/[code]/g/[
}}
initial={guest}
action={updateGuestRsvp.bind(null, code, token)}
readOnly={cancelled}
notice={
<div className="flex flex-col gap-2">
<div
role="status"
className="rounded-2xl bg-mustard px-5 py-4 text-lg font-bold text-ink"
>
{CONFIRMATION[guest.status]}
<span className="block text-sm font-normal">
Need to change something? Update your answer below.
</span>
</div>
<p className="px-2 text-xs text-ink-muted">
This is your personal link — don&apos;t share it.{" "}
<Link href={`/e/${encodeURIComponent(code)}?new=1`} className="font-bold text-ketchup underline">
Not you? Answer as someone else
</Link>
</p>
{cancelled ? (
<CancelledEventNotice hostName={event.hostName} />
) : (
<div
role="status"
className="rounded-2xl bg-mustard px-5 py-4 text-lg font-bold text-ink"
>
{CONFIRMATION[guest.status]}
<span className="block text-sm font-normal">
Need to change something? Update your answer below.
</span>
</div>
)}
{personalNote}
</div>
}
/>
Expand Down
54 changes: 33 additions & 21 deletions src/app/e/[code]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type { Metadata } from "next";
import { cookies } from "next/headers";
import { notFound, redirect } from "next/navigation";
import { CancelledEventNotice } from "@/components/CancelledEventNotice";
import { EventUnavailable } from "@/components/EventUnavailable";
import { RsvpForm } from "@/components/RsvpForm";
import { getDb } from "@/db/client";
import { eventState } from "@/lib/event-state";
import { formatWhen } from "@/lib/format";
import { guestCookieName } from "@/lib/guest-cookie";
import { eventPreview, wantsBlankForm } from "@/lib/guest-page";
Expand All @@ -22,7 +25,8 @@ export async function generateMetadata({ params }: PageProps<"/e/[code]">): Prom
if (error instanceof NotFoundError) return null;
throw error;
});
const { title, description } = eventPreview(event);
// A deleted event must not keep advertising its title in link previews.
const { title, description } = eventPreview(event && !event.deletedAt ? event : null);
return { title, description, openGraph: { title, description } };
}

Expand All @@ -39,6 +43,10 @@ export default async function GuestPage({ params, searchParams }: PageProps<"/e/
throw error;
}

const state = eventState(event);
if (state === "deleted") return <EventUnavailable hostName={event.hostName} />;
const cancelled = state === "cancelled";

// A returning guest goes straight to their edit page. A missing, invalid or
// stale cookie is ignored. `?new=1` ("Not you?") skips this and shows a blank form.
const token = blank ? undefined : (await cookies()).get(guestCookieName(code))?.value;
Expand Down Expand Up @@ -71,29 +79,33 @@ export default async function GuestPage({ params, searchParams }: PageProps<"/e/
goingCount: event.goingCount,
}}
action={submitGuestRsvp.bind(null, code)}
readOnly={cancelled}
notice={cancelled ? <CancelledEventNotice hostName={event.hostName} /> : undefined}
/>

<aside className="mx-auto -mt-6 w-full max-w-lg px-3 pb-14 sm:px-5">
<div className="ticket-surface rounded-2xl border border-divider bg-paper p-4">
<h2 className="text-base font-bold text-ink">Add to calendar</h2>
<p className="mt-1 text-sm leading-relaxed text-ink/80">
Save the time now. Come back to this link if plans change.
</p>
<div className="mt-3 flex flex-wrap gap-x-5 gap-y-2">
<a href={`/e/${encodeURIComponent(code)}/calendar.ics`} className="quiet-link">
Download .ics
</a>
<a
href={googleUrl}
target="_blank"
rel="noreferrer"
className="quiet-link"
>
Google Calendar
</a>
{cancelled ? null : (
<aside className="mx-auto -mt-6 w-full max-w-lg px-3 pb-14 sm:px-5">
<div className="ticket-surface rounded-2xl border border-divider bg-paper p-4">
<h2 className="text-base font-bold text-ink">Add to calendar</h2>
<p className="mt-1 text-sm leading-relaxed text-ink/80">
Save the time now. Come back to this link if plans change.
</p>
<div className="mt-3 flex flex-wrap gap-x-5 gap-y-2">
<a href={`/e/${encodeURIComponent(code)}/calendar.ics`} className="quiet-link">
Download .ics
</a>
<a
href={googleUrl}
target="_blank"
rel="noreferrer"
className="quiet-link"
>
Google Calendar
</a>
</div>
</div>
</div>
</aside>
</aside>
)}
</main>
);
}
66 changes: 66 additions & 0 deletions src/app/host/actions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// @vitest-environment node
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { Db } from "@/db/client";
import { createTestDb } from "@/db/test-db";
import { createEvent, deleteEvent, getHostEvent } from "@/services/events";

const session = vi.hoisted(() => ({ hostId: "host-1" }));
const testDb = vi.hoisted(() => ({ db: null as unknown }));

vi.mock("@/server/auth", () => ({
requireHost: async () => ({ id: session.hostId, name: "Host", email: "h@example.com" }),
}));
vi.mock("@/db/client", () => ({ getDb: async () => testDb.db }));
vi.mock("next/cache", () => ({ revalidatePath: vi.fn() }));
vi.mock("next/navigation", () => ({
notFound: () => {
throw new Error("NEXT_NOT_FOUND");
},
redirect: (url: string) => {
throw new Error(`NEXT_REDIRECT:${url}`);
},
}));

import { restoreEventAction } from "./actions";

let db: Db;
let eventId: string;

beforeEach(async () => {
session.hostId = "host-1";
db = await createTestDb();
testDb.db = db;
({ id: eventId } = await createEvent(db, "host-1", {
title: "Cookout",
startsAt: new Date("2026-07-04T18:00:00Z"),
location: "Backyard",
hostName: "Will",
}));
});

describe("restoreEventAction", () => {
it("restores the host's deleted event and opens it", async () => {
await deleteEvent(db, "host-1", eventId);

await expect(restoreEventAction(eventId)).rejects.toThrow(
`NEXT_REDIRECT:/host/events/${eventId}`,
);

const event = await getHostEvent(db, "host-1", eventId);
expect(event.deletedAt).toBeNull();
expect(event.title).toBe("Cookout");
});

it("a different signed-in host gets a 404 and the event stays deleted", async () => {
await deleteEvent(db, "host-1", eventId);
session.hostId = "host-2";

await expect(restoreEventAction(eventId)).rejects.toThrow("NEXT_NOT_FOUND");
await expect(getHostEvent(db, "host-1", eventId)).rejects.toThrow();
});

it("is a 404 for a live event or an unknown id", async () => {
await expect(restoreEventAction(eventId)).rejects.toThrow("NEXT_NOT_FOUND");
await expect(restoreEventAction("missing")).rejects.toThrow("NEXT_NOT_FOUND");
});
});
26 changes: 26 additions & 0 deletions src/app/host/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"use server";

import { revalidatePath } from "next/cache";
import { notFound, redirect } from "next/navigation";
import { getDb } from "@/db/client";
import { requireHost } from "@/server/auth";
import { NotFoundError } from "@/services/errors";
import { restoreEvent } from "@/services/events";

// The host comes from the session; the event id arrives through `bind` and is
// therefore untrusted. restoreEvent re-checks that the event is this host's and
// deleted before it comes back.

/** Undo on the dashboard's "Deleted …" notice: restores the event, then opens it. */
export async function restoreEventAction(eventId: string): Promise<void> {
const host = await requireHost();
try {
await restoreEvent(await getDb(), host.id, eventId);
} catch (error) {
if (error instanceof NotFoundError) notFound();
throw error;
}
revalidatePath("/host");
revalidatePath(`/host/events/${encodeURIComponent(eventId)}`, "layout");
redirect(`/host/events/${encodeURIComponent(eventId)}`);
}
58 changes: 54 additions & 4 deletions src/app/host/events/[id]/edit/actions.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
// @vitest-environment node
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { Db } from "@/db/client";
import { events } from "@/db/schema";
import { createTestDb } from "@/db/test-db";
import { createEvent, getHostEvent } from "@/services/events";
import { eq } from "drizzle-orm";
import {
cancelEvent,
createEvent,
findDeletedHostEvent,
getHostEvent,
} from "@/services/events";

const session = vi.hoisted(() => ({ hostId: "host-1" }));
const testDb = vi.hoisted(() => ({ db: null as unknown }));
Expand All @@ -21,7 +28,12 @@ vi.mock("next/navigation", () => ({
},
}));

import { deleteEventAction, updateEventAction } from "./actions";
import {
cancelEventAction,
deleteEventAction,
uncancelEventAction,
updateEventAction,
} from "./actions";

let db: Db;
let eventId: string;
Expand Down Expand Up @@ -77,11 +89,12 @@ describe("updateEventAction", () => {
});

describe("deleteEventAction", () => {
it("deletes the event and redirects to the dashboard with its title", async () => {
it("moves the event to the bin and redirects to the dashboard with its id", async () => {
await expect(deleteEventAction(eventId)).rejects.toThrow(
`NEXT_REDIRECT:/host?deleted=${encodeURIComponent("Cookout")}`,
`NEXT_REDIRECT:/host?deleted=${encodeURIComponent(eventId)}`,
);
await expect(getHostEvent(db, "host-1", eventId)).rejects.toThrow();
expect(await findDeletedHostEvent(db, "host-1", eventId)).toMatchObject({ title: "Cookout" });
});

it("a different signed-in host gets a 404 and the event stays", async () => {
Expand All @@ -90,3 +103,40 @@ describe("deleteEventAction", () => {
expect((await getHostEvent(db, "host-1", eventId)).title).toBe("Cookout");
});
});

describe("cancelEventAction", () => {
it("cancels the event and returns to the edit page", async () => {
await expect(cancelEventAction(eventId)).rejects.toThrow(
`NEXT_REDIRECT:/host/events/${eventId}/edit`,
);
expect((await getHostEvent(db, "host-1", eventId)).cancelledAt).toBeInstanceOf(Date);
});

it("a different signed-in host gets a 404 and nothing changes", async () => {
session.hostId = "host-2";
await expect(cancelEventAction(eventId)).rejects.toThrow("NEXT_NOT_FOUND");
expect((await getHostEvent(db, "host-1", eventId)).cancelledAt).toBeNull();
});
});

describe("uncancelEventAction", () => {
it("reinstates a cancelled event", async () => {
await cancelEvent(db, "host-1", eventId);
await expect(uncancelEventAction(eventId)).rejects.toThrow(
`NEXT_REDIRECT:/host/events/${eventId}/edit`,
);
expect((await getHostEvent(db, "host-1", eventId)).cancelledAt).toBeNull();
});

it("a different signed-in host gets a 404", async () => {
await cancelEvent(db, "host-1", eventId);
session.hostId = "host-2";
await expect(uncancelEventAction(eventId)).rejects.toThrow("NEXT_NOT_FOUND");
expect((await getHostEvent(db, "host-1", eventId)).cancelledAt).toBeInstanceOf(Date);
});

it("cannot touch a deleted event", async () => {
await db.update(events).set({ deletedAt: new Date() }).where(eq(events.id, eventId));
await expect(uncancelEventAction(eventId)).rejects.toThrow("NEXT_NOT_FOUND");
});
});
Loading
Loading