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
46 changes: 42 additions & 4 deletions packages/app/src/features/projects/detail.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
import { useState } from "react";
import { Link, useParams } from "@tanstack/react-router";
import { useQuery } from "@tanstack/react-query";
import { Button, Page, PageHeader } from "../../shared/ui";
import { projectQuery } from "./queries";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { api } from "factorize-api-client";
import { Button, Input, Label, Page, PageHeader, Textarea } from "../../shared/ui";
import { Dialog } from "../../shared/dialog";
import { messageOf } from "../auth/session";
import { projectQuery, projectsQuery } from "./queries";
import { JobsTable } from "../jobs/list";
export function ProjectDetail() { const { projectId } = useParams({ from: "/projects/$projectId" }); const project=useQuery(projectQuery(projectId)); return <Page className="max-w-6xl"><Link to="/projects" className="text-sm font-semibold">Projects</Link>{project.data&&<><PageHeader title={project.data.name} description={project.data.description}><Link to="/jobs/new" search={{ projectId }}><Button>Create job</Button></Link></PageHeader><JobsTable projectId={projectId} /></>}</Page>; }

export function ProjectDetail() {
const { projectId } = useParams({ from: "/projects/$projectId" });
const project = useQuery(projectQuery(projectId));
const cache = useQueryClient();
const [editing, setEditing] = useState(false);
const [name, setName] = useState("");
const [description, setDescription] = useState("");
const [validationError, setValidationError] = useState("");
const edit = useMutation({
mutationFn: async () => {
const trimmedName = name.trim();
if (!trimmedName) throw new Error("Project name is required.");
const { data, error } = await api.PUT("/api/v1/projects/{projectId}", { params: { path: { projectId } }, body: { name: trimmedName, description: description.trim() || undefined } });
if (!data || error) throw new Error(messageOf(error));
return data;
},
onSuccess: async updated => {
cache.setQueryData(projectQuery(projectId).queryKey, updated);
cache.setQueryData(projectsQuery.queryKey, (projects) => projects?.map(item => item.id === updated.id ? { ...item, ...updated } : item));
setEditing(false);
setValidationError("");
await cache.invalidateQueries({ queryKey: projectsQuery.queryKey });
},
onError: error => setValidationError(error.message),
});
const openEditor = () => {
if (!project.data) return;
setName(project.data.name);
setDescription(project.data.description ?? "");
setValidationError("");
setEditing(true);
};
return <Page className="max-w-6xl"><Link to="/projects" className="text-sm font-semibold">Projects</Link>{project.data&&<><PageHeader title={project.data.name} description={project.data.description}><div className="flex flex-wrap gap-2"><Button onClick={openEditor}>Edit</Button><Link to="/jobs/new" search={{ projectId }}><Button>Create job</Button></Link></div></PageHeader><JobsTable projectId={projectId} />{editing&&<Dialog title="Edit project" close={() => { if (!edit.isPending) setEditing(false); }}><form className="space-y-5" onSubmit={event => { event.preventDefault(); edit.mutate(); }}><Label>Name<Input required maxLength={120} value={name} onChange={event => setName(event.target.value)} /></Label><Label>Description<Textarea maxLength={2000} value={description} onChange={event => setDescription(event.target.value)} /></Label>{validationError&&<p role="alert">{validationError}</p>}<div className="flex justify-end gap-2"><Button type="button" onClick={() => setEditing(false)} disabled={edit.isPending}>Cancel</Button><Button type="submit" variant="primary" disabled={edit.isPending}>{edit.isPending ? "Saving…" : "Save changes"}</Button></div></form></Dialog>}</>}</Page>;
}
27 changes: 27 additions & 0 deletions packages/app/test/jobs.browser.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,33 @@ async function contextFor(options) {
await page.route(`**/api/v1/projects/${projectId}`, route => route.fulfill({ json: { id: projectId, name: "Release project", description: "Project jobs", jobCount: 31 } }));
return { context, page };
}
test("project details can rename and describe a project", async () => {
const { page, context } = await contextFor({ viewport: { width: 1280, height: 844 } });
const saved = [];
let projectResponse = { id: projectId, name: "Release project", description: "Project jobs", jobCount: 31 };
await page.route(`**/api/v1/projects/${projectId}`, async route => {
if (route.request().method() !== "PUT") return route.fulfill({ json: projectResponse });
saved.push(route.request().postDataJSON());
projectResponse = { id: projectId, name: "Renamed project", description: "Updated details", jobCount: 31 };
return route.fulfill({ json: projectResponse });
});
await page.route("**/api/v1/job-summaries?**", route => route.fulfill({ json: { items: [], nextCursor: null } }));
await page.goto(origin + `/projects/${projectId}`);
await page.getByRole("heading", { name: "Release project", exact: true }).waitFor();
await page.getByRole("button", { name: "Edit", exact: true }).click();
await page.getByLabel("Name", { exact: true }).fill("Renamed project");
await page.locator("dialog textarea").fill("Updated details");
await page.getByRole("button", { name: "Save changes", exact: true }).click();
await page.getByRole("heading", { name: "Renamed project", exact: true }).waitFor();
assert.deepEqual(saved, [{ name: "Renamed project", description: "Updated details" }]);
assert.match(await page.locator("main").textContent(), /Updated details/);
await page.getByRole("button", { name: "Edit", exact: true }).click();
await page.getByLabel("Name", { exact: true }).fill(" ");
await page.getByRole("button", { name: "Save changes", exact: true }).click();
await page.getByRole("alert").filter({ hasText: "Project name is required." }).waitFor();
assert.equal(saved.length, 1);
await context.close();
});
for (const width of [1280, 390, 320]) for (const theme of ["light", "dark"]) test(`compact jobs grouping, pagination, focus and long titles at ${width} ${theme}`, async () => {
const { page, context } = await contextFor({ viewport: { width, height: 844 }, colorScheme: theme });
const longTitle = "Zulu " + "long title ".repeat(30);
Expand Down
Loading