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
14 changes: 12 additions & 2 deletions frontend/src/app/portal/admin/content/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import type { Metadata } from "next";

import { Placeholder } from "@/components/portal/Placeholder";
import { ContentSettingsManager } from "@/components/admin/ContentSettingsManager";

export const metadata: Metadata = { title: "Content" };

export default function Page() {
return <Placeholder title="Content" />;
return (
<div className="space-y-6">
<header>
<h1 className="text-2xl font-semibold tracking-tight">Site content</h1>
<p className="mt-1 text-muted-foreground">
Manage site settings and page text.
</p>
</header>
<ContentSettingsManager />
</div>
);
}
291 changes: 291 additions & 0 deletions frontend/src/components/admin/ContentSettingsManager.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,291 @@
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import {
cleanup,
fireEvent,
render,
screen,
waitFor,
within,
} from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

import { ContentSettingsManager } from "./ContentSettingsManager";

const settings = [
{
key: "tagline",
value: "A peer to talk to.",
description: "Short tagline on the home page.",
public: true,
updated_by: "Casey Coordinator",
updated_at: "2026-11-01T12:00:00Z",
},
{
key: "contact_email",
value: "peer2peer@example.org",
description: "Public contact email.",
public: true,
updated_by: null,
updated_at: null,
},
{
key: "responder_form_url",
value: "https://example.org/apply",
description: "Responder application form.",
public: true,
updated_by: null,
updated_at: null,
},
{
key: "conversation_inactive_minutes",
value: 30,
description: "Conversation timeout.",
public: false,
updated_by: null,
updated_at: null,
},
{
key: "message_retention_days",
value: 30,
description: "Message retention period.",
public: false,
updated_by: null,
updated_at: null,
},
{
key: "feedback_retention_days",
value: 365,
description: "Feedback retention period.",
public: false,
updated_by: null,
updated_at: null,
},
{
key: "report_required",
value: true,
description: "Require a post-conversation report.",
public: false,
updated_by: null,
updated_at: null,
},
{
key: "channels",
value: ["web"],
description: "Supported channels.",
public: true,
updated_by: null,
updated_at: null,
},
{
key: "line_launch_at",
value: "2026-12-01T16:00:00Z",
description: "Line launch time.",
public: false,
updated_by: null,
updated_at: null,
},
{
key: "after_hours_behavior",
value: "close",
description: "After-hours behavior.",
public: false,
updated_by: null,
updated_at: null,
},
];

const blocks = [
{
slug: "home.intro",
title: "Welcome to Peer2Peer",
body_md: "Hello **students**.",
updated_at: null,
},
{
slug: "about.history",
title: "Our History",
body_md: "Our story.",
updated_at: null,
},
];

let queryClient: QueryClient;
let fetchMock: ReturnType<typeof vi.fn>;

function renderManager() {
return render(
<QueryClientProvider client={queryClient}>
<ContentSettingsManager />
</QueryClientProvider>,
);
}

function calls(method: string) {
return fetchMock.mock.calls.filter(
([, init]) => (init?.method ?? "GET") === method,
);
}

beforeEach(() => {
queryClient = new QueryClient({
defaultOptions: { queries: { retry: false } },
});
fetchMock = vi.fn(async (path: string, init?: RequestInit) => {
if (path === "/api/admin/settings") return Response.json(settings);
if (path === "/api/admin/content") return Response.json(blocks);
if ((init?.method ?? "GET") === "PUT") {
if (path === "/api/admin/settings/tagline") {
return Response.json(
{
code: "validation_error",
detail: "Invalid value for setting 'tagline'",
},
{ status: 422 },
);
}

const body = JSON.parse(String(init?.body));
const slug = decodeURIComponent(path.split("/").at(-1) ?? "");
return Response.json({
slug,
...body,
updated_at: "2026-11-29T12:00:00Z",
});
}
return Response.json({});
});
vi.stubGlobal("fetch", fetchMock);
});

afterEach(() => {
cleanup();
queryClient.clear();
vi.restoreAllMocks();
vi.unstubAllGlobals();
});

describe("ContentSettingsManager", () => {
it("renders settings with controls that match their value types", async () => {
renderManager();

expect(await screen.findByLabelText("tagline")).toHaveProperty(
"type",
"text",
);
expect(screen.getByLabelText("contact email")).toHaveProperty(
"type",
"email",
);
expect(screen.getByLabelText("responder form url")).toHaveProperty(
"type",
"url",
);
expect(
screen.getByLabelText("conversation inactive minutes"),
).toHaveProperty("type", "number");
expect(screen.getByLabelText("message retention days")).toHaveProperty(
"type",
"number",
);
expect(
screen.getByLabelText("Require a post-conversation report"),
).toHaveAttribute("role", "switch");
expect(screen.getByLabelText("WEB")).toHaveAttribute("role", "checkbox");
expect(screen.getByLabelText("SMS")).toHaveAttribute("role", "checkbox");
expect(screen.getByLabelText("line launch at")).toHaveProperty(
"type",
"datetime-local",
);
expect(screen.getByLabelText("After-hours behavior").tagName).toBe(
"SELECT",
);
expect(screen.getAllByText("Public")).toHaveLength(4);
expect(screen.getByText("Short tagline on the home page.")).toBeTruthy();
expect(screen.getByText(/Last changed by: Casey Coordinator/)).toBeTruthy();
expect(
screen.getAllByText(
"This value is a placeholder until Peer2Peer decides.",
),
).toHaveLength(3);
});

it("saves only the selected setting and shows a 422 inline", async () => {
const user = userEvent.setup();
renderManager();
const tagline = await screen.findByLabelText("tagline");
await user.clear(tagline);
await user.type(tagline, "Updated tagline");

const row = screen
.getByRole("heading", { name: "tagline" })
.closest("section");
expect(row).not.toBeNull();
await user.click(within(row!).getByRole("button", { name: "Save" }));

expect(await within(row!).findByRole("alert")).toHaveTextContent(
"Invalid value for setting 'tagline'",
);
const putCalls = calls("PUT");
expect(putCalls).toHaveLength(1);
expect(putCalls[0][0]).toBe("/api/admin/settings/tagline");
expect(JSON.parse(putCalls[0][1].body)).toEqual({
value: "Updated tagline",
});
});

it("updates the safe Markdown preview as the draft changes", async () => {
const user = userEvent.setup();
renderManager();
await user.click(screen.getByRole("tab", { name: "Page text" }));
const editor = await screen.findByLabelText("Markdown");
await user.clear(editor);
await user.type(editor, "<script>alert(1)</script>\n\n**Safe preview**");

const preview = screen.getByLabelText("Markdown preview");
expect(within(preview).getByText("Safe preview").tagName).toBe("STRONG");
expect(within(preview).queryByText("alert(1)")).toBeNull();
expect(preview.querySelector("script")).toBeNull();
});

it("saves the selected content block with its slug and edited fields", async () => {
const user = userEvent.setup();
renderManager();
await user.click(screen.getByRole("tab", { name: "Page text" }));
const title = await screen.findByLabelText("Title");
await user.clear(title);
await user.type(title, "Welcome to campus");
await user.click(screen.getByRole("button", { name: "Save page text" }));

await waitFor(() => expect(calls("PUT")).toHaveLength(1));
expect(calls("PUT")[0][0]).toBe("/api/admin/content/home.intro");
expect(JSON.parse(calls("PUT")[0][1].body)).toEqual({
title: "Welcome to campus",
body_md: "Hello **students**.",
});
expect(await screen.findByText("home.intro")).toBeTruthy();
expect(screen.queryByText(/Unsaved changes/)).toBeNull();
});

it("warns before switching blocks and before leaving with unsaved edits", async () => {
const user = userEvent.setup();
const confirm = vi.spyOn(window, "confirm").mockReturnValue(false);
renderManager();
await user.click(screen.getByRole("tab", { name: "Page text" }));
const editor = await screen.findByLabelText("Markdown");
await user.type(editor, " changed");

expect(screen.getByText(/Unsaved changes/)).toBeTruthy();
const beforeUnload = new Event("beforeunload", { cancelable: true });
fireEvent(window, beforeUnload);
expect(beforeUnload.defaultPrevented).toBe(true);

await user.click(screen.getByRole("button", { name: "Our History" }));
expect(confirm).toHaveBeenCalledWith(
"You have unsaved changes. Leave without saving?",
);
expect(screen.getByLabelText("Markdown")).toHaveValue(
"Hello **students**. changed",
);
});
});
Loading