feat(web): nested object routes + in-pane edit form + edit flow
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+8
-2
@@ -5,6 +5,9 @@ import { LoginPage } from "./auth/login-page";
|
|||||||
import { AppShell } from "./shell/app-shell";
|
import { AppShell } from "./shell/app-shell";
|
||||||
import { ObjectsPage } from "./objects/objects-page";
|
import { ObjectsPage } from "./objects/objects-page";
|
||||||
import { ObjectNewPage } from "./objects/object-new-page";
|
import { ObjectNewPage } from "./objects/object-new-page";
|
||||||
|
import { ObjectDetail } from "./objects/object-detail";
|
||||||
|
import { ObjectEditForm } from "./objects/object-edit-form";
|
||||||
|
import { SelectPrompt } from "./objects/select-prompt";
|
||||||
|
|
||||||
export function App() {
|
export function App() {
|
||||||
return (
|
return (
|
||||||
@@ -14,8 +17,11 @@ export function App() {
|
|||||||
<Route element={<RequireAuth />}>
|
<Route element={<RequireAuth />}>
|
||||||
<Route element={<AppShell />}>
|
<Route element={<AppShell />}>
|
||||||
<Route path="/objects/new" element={<ObjectNewPage />} />
|
<Route path="/objects/new" element={<ObjectNewPage />} />
|
||||||
<Route path="/objects" element={<ObjectsPage />} />
|
<Route path="/objects" element={<ObjectsPage />}>
|
||||||
<Route path="/objects/:id" element={<ObjectsPage />} />
|
<Route index element={<SelectPrompt />} />
|
||||||
|
<Route path=":id" element={<ObjectDetail />} />
|
||||||
|
<Route path=":id/edit" element={<ObjectEditForm />} />
|
||||||
|
</Route>
|
||||||
<Route path="/" element={<Navigate to="/objects" replace />} />
|
<Route path="/" element={<Navigate to="/objects" replace />} />
|
||||||
</Route>
|
</Route>
|
||||||
</Route>
|
</Route>
|
||||||
|
|||||||
@@ -5,5 +5,6 @@
|
|||||||
"objects": { "title": "Objects", "empty": "No objects yet", "loadError": "Could not load objects", "selectPrompt": "Select an object to view its details", "notFound": "Object not found", "prev": "Previous", "next": "Next", "of": "of", "new": "New object" },
|
"objects": { "title": "Objects", "empty": "No objects yet", "loadError": "Could not load objects", "selectPrompt": "Select an object to view its details", "notFound": "Object not found", "prev": "Previous", "next": "Next", "of": "of", "new": "New object" },
|
||||||
"fieldsLabels": { "objectNumber": "Object number", "objectName": "Name", "count": "Number of objects", "briefDescription": "Brief description", "currentLocation": "Current location", "currentOwner": "Current owner", "recorder": "Recorder", "recordingDate": "Recording date", "visibility": "Visibility", "flexible": "Catalogue fields" },
|
"fieldsLabels": { "objectNumber": "Object number", "objectName": "Name", "count": "Number of objects", "briefDescription": "Brief description", "currentLocation": "Current location", "currentOwner": "Current owner", "recorder": "Recorder", "recordingDate": "Recording date", "visibility": "Visibility", "flexible": "Catalogue fields" },
|
||||||
"visibility": { "draft": "Draft", "internal": "Internal", "public": "Public" },
|
"visibility": { "draft": "Draft", "internal": "Internal", "public": "Public" },
|
||||||
"form": { "selectPlaceholder": "— select —", "create": "Create object", "save": "Save", "cancel": "Cancel", "visibility": "Visibility", "draft": "Draft", "internal": "Internal", "required": "This field is required", "rejected": "The server rejected the changes — check required and referenced fields", "flexibleHeading": "Catalogue fields" }
|
"form": { "selectPlaceholder": "— select —", "create": "Create object", "save": "Save", "cancel": "Cancel", "visibility": "Visibility", "draft": "Draft", "internal": "Internal", "required": "This field is required", "rejected": "The server rejected the changes — check required and referenced fields", "flexibleHeading": "Catalogue fields" },
|
||||||
|
"actions": { "edit": "Edit" }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,5 +5,6 @@
|
|||||||
"objects": { "title": "Föremål", "empty": "Inga föremål ännu", "loadError": "Kunde inte ladda föremål", "selectPrompt": "Välj ett föremål för att se detaljer", "notFound": "Föremålet hittades inte", "prev": "Föregående", "next": "Nästa", "of": "av", "new": "Nytt föremål" },
|
"objects": { "title": "Föremål", "empty": "Inga föremål ännu", "loadError": "Kunde inte ladda föremål", "selectPrompt": "Välj ett föremål för att se detaljer", "notFound": "Föremålet hittades inte", "prev": "Föregående", "next": "Nästa", "of": "av", "new": "Nytt föremål" },
|
||||||
"fieldsLabels": { "objectNumber": "Föremålsnummer", "objectName": "Namn", "count": "Antal föremål", "briefDescription": "Kort beskrivning", "currentLocation": "Nuvarande plats", "currentOwner": "Nuvarande ägare", "recorder": "Registrerad av", "recordingDate": "Registreringsdatum", "visibility": "Synlighet", "flexible": "Katalogfält" },
|
"fieldsLabels": { "objectNumber": "Föremålsnummer", "objectName": "Namn", "count": "Antal föremål", "briefDescription": "Kort beskrivning", "currentLocation": "Nuvarande plats", "currentOwner": "Nuvarande ägare", "recorder": "Registrerad av", "recordingDate": "Registreringsdatum", "visibility": "Synlighet", "flexible": "Katalogfält" },
|
||||||
"visibility": { "draft": "Utkast", "internal": "Intern", "public": "Publik" },
|
"visibility": { "draft": "Utkast", "internal": "Intern", "public": "Publik" },
|
||||||
"form": { "selectPlaceholder": "— välj —", "create": "Skapa föremål", "save": "Spara", "cancel": "Avbryt", "visibility": "Synlighet", "draft": "Utkast", "internal": "Intern", "required": "Fältet är obligatoriskt", "rejected": "Servern avvisade ändringarna — kontrollera obligatoriska och refererade fält", "flexibleHeading": "Katalogfält" }
|
"form": { "selectPlaceholder": "— välj —", "create": "Skapa föremål", "save": "Spara", "cancel": "Avbryt", "visibility": "Synlighet", "draft": "Utkast", "internal": "Intern", "required": "Fältet är obligatoriskt", "rejected": "Servern avvisade ändringarna — kontrollera obligatoriska och refererade fält", "flexibleHeading": "Katalogfält" },
|
||||||
|
"actions": { "edit": "Redigera" }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useParams } from "react-router-dom";
|
import { Link, useParams } from "react-router-dom";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
import { useObject, useFieldDefinitions } from "../api/queries";
|
import { useObject, useFieldDefinitions } from "../api/queries";
|
||||||
@@ -57,6 +57,9 @@ export function ObjectDetail() {
|
|||||||
<div className="mb-4 flex items-center gap-3">
|
<div className="mb-4 flex items-center gap-3">
|
||||||
<h2 className="text-xl font-semibold">{object.object_name}</h2>
|
<h2 className="text-xl font-semibold">{object.object_name}</h2>
|
||||||
<VisibilityBadge visibility={object.visibility} />
|
<VisibilityBadge visibility={object.visibility} />
|
||||||
|
<Link to={`/objects/${object.id}/edit`} className="text-sm font-medium text-indigo-600">
|
||||||
|
{t("actions.edit")}
|
||||||
|
</Link>
|
||||||
</div>
|
</div>
|
||||||
<Field label={t("fieldsLabels.objectNumber")} value={object.object_number} />
|
<Field label={t("fieldsLabels.objectNumber")} value={object.object_number} />
|
||||||
<Field label={t("fieldsLabels.count")} value={object.number_of_objects} />
|
<Field label={t("fieldsLabels.count")} value={object.number_of_objects} />
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
import { expect, test } from "vitest";
|
||||||
|
import { screen, waitFor } from "@testing-library/react";
|
||||||
|
import userEvent from "@testing-library/user-event";
|
||||||
|
import { http, HttpResponse } from "msw";
|
||||||
|
import { Routes, Route } from "react-router-dom";
|
||||||
|
import { server } from "../test/server";
|
||||||
|
import { renderApp } from "../test/render";
|
||||||
|
import { ObjectEditForm } from "./object-edit-form";
|
||||||
|
import { amphora } from "../test/fixtures";
|
||||||
|
|
||||||
|
function tree() {
|
||||||
|
return (
|
||||||
|
<Routes>
|
||||||
|
<Route path="/objects/:id/edit" element={<ObjectEditForm />} />
|
||||||
|
<Route path="/objects/:id" element={<div>detail view</div>} />
|
||||||
|
</Routes>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
test("edit: prefilled, save -> PUT core + PUT fields -> back to detail", async () => {
|
||||||
|
let putCore: unknown;
|
||||||
|
let putFields: unknown;
|
||||||
|
|
||||||
|
server.use(
|
||||||
|
http.get("/api/admin/objects/:id", () =>
|
||||||
|
HttpResponse.json({ ...amphora, fields: { inscription: "old" } }),
|
||||||
|
),
|
||||||
|
http.put("/api/admin/objects/:id", async ({ request }) => {
|
||||||
|
putCore = await request.json();
|
||||||
|
return new HttpResponse(null, { status: 204 });
|
||||||
|
}),
|
||||||
|
http.put("/api/admin/objects/:id/fields", async ({ request }) => {
|
||||||
|
putFields = await request.json();
|
||||||
|
return new HttpResponse(null, { status: 204 });
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
renderApp(tree(), { route: `/objects/${amphora.id}/edit` });
|
||||||
|
|
||||||
|
const name = await screen.findByDisplayValue("Amphora");
|
||||||
|
|
||||||
|
await userEvent.clear(name);
|
||||||
|
await userEvent.type(name, "Big amphora");
|
||||||
|
await userEvent.click(screen.getByRole("button", { name: /save/i }));
|
||||||
|
|
||||||
|
await waitFor(() => expect(screen.getByText("detail view")).toBeInTheDocument());
|
||||||
|
expect((putCore as { object_name: string }).object_name).toBe("Big amphora");
|
||||||
|
expect((putFields as { inscription: string }).inscription).toBe("old");
|
||||||
|
});
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
import { useState } from "react";
|
||||||
|
import { useLocation, useNavigate, useParams } from "react-router-dom";
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
|
import { useObject, useUpdateObject, useSetFields } from "../api/queries";
|
||||||
|
import { ObjectForm, type ObjectCore, type ObjectFormValues } from "./object-form";
|
||||||
|
|
||||||
|
export function ObjectEditForm() {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const { id } = useParams();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const location = useLocation();
|
||||||
|
|
||||||
|
const { data: object, isLoading } = useObject(id!);
|
||||||
|
const update = useUpdateObject();
|
||||||
|
const setFields = useSetFields();
|
||||||
|
|
||||||
|
const [error, setError] = useState<string | null>(
|
||||||
|
(location.state as { fieldsError?: boolean } | null)?.fieldsError ? t("form.rejected") : null,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (isLoading) return <div className="p-4" role="status" aria-label="loading" />;
|
||||||
|
|
||||||
|
if (!object) return <p className="p-4 text-sm text-neutral-500">{t("objects.notFound")}</p>;
|
||||||
|
|
||||||
|
const core: ObjectCore = {
|
||||||
|
object_number: object.object_number,
|
||||||
|
object_name: object.object_name,
|
||||||
|
number_of_objects: object.number_of_objects,
|
||||||
|
brief_description: object.brief_description ?? null,
|
||||||
|
current_location: object.current_location ?? null,
|
||||||
|
current_owner: object.current_owner ?? null,
|
||||||
|
recorder: object.recorder ?? null,
|
||||||
|
recording_date: object.recording_date ?? null,
|
||||||
|
};
|
||||||
|
|
||||||
|
const defaults = { core, fields: object.fields as Record<string, unknown> };
|
||||||
|
|
||||||
|
const onSubmit = async (values: ObjectFormValues) => {
|
||||||
|
setError(null);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await update.mutateAsync({ id: id!, body: values.core });
|
||||||
|
await setFields.mutateAsync({ id: id!, fields: values.fields });
|
||||||
|
} catch {
|
||||||
|
setError(t("form.rejected"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
navigate(`/objects/${id}`);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ObjectForm
|
||||||
|
mode="edit"
|
||||||
|
defaults={defaults}
|
||||||
|
formError={error}
|
||||||
|
onSubmit={onSubmit}
|
||||||
|
onCancel={() => navigate(`/objects/${id}`)}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -4,12 +4,16 @@ import userEvent from "@testing-library/user-event";
|
|||||||
import { Routes, Route } from "react-router-dom";
|
import { Routes, Route } from "react-router-dom";
|
||||||
import { renderApp } from "../test/render";
|
import { renderApp } from "../test/render";
|
||||||
import { ObjectsPage } from "./objects-page";
|
import { ObjectsPage } from "./objects-page";
|
||||||
|
import { ObjectDetail } from "./object-detail";
|
||||||
|
import { SelectPrompt } from "./select-prompt";
|
||||||
|
|
||||||
function tree() {
|
function tree() {
|
||||||
return (
|
return (
|
||||||
<Routes>
|
<Routes>
|
||||||
<Route path="/objects" element={<ObjectsPage />} />
|
<Route path="/objects" element={<ObjectsPage />}>
|
||||||
<Route path="/objects/:id" element={<ObjectsPage />} />
|
<Route index element={<SelectPrompt />} />
|
||||||
|
<Route path=":id" element={<ObjectDetail />} />
|
||||||
|
</Route>
|
||||||
</Routes>
|
</Routes>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,26 +1,15 @@
|
|||||||
import { useParams } from "react-router-dom";
|
import { Outlet } from "react-router-dom";
|
||||||
import { useTranslation } from "react-i18next";
|
|
||||||
|
|
||||||
import { ObjectList } from "./object-list";
|
import { ObjectList } from "./object-list";
|
||||||
import { ObjectDetail } from "./object-detail";
|
|
||||||
|
|
||||||
export function ObjectsPage() {
|
export function ObjectsPage() {
|
||||||
const { t } = useTranslation();
|
|
||||||
const { id } = useParams();
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="grid h-full grid-cols-[20rem_1fr]">
|
<div className="grid h-full grid-cols-[20rem_1fr]">
|
||||||
<div className="overflow-hidden border-r">
|
<div className="overflow-hidden border-r">
|
||||||
<ObjectList />
|
<ObjectList />
|
||||||
</div>
|
</div>
|
||||||
<div className="overflow-hidden">
|
<div className="overflow-hidden">
|
||||||
{id ? (
|
<Outlet />
|
||||||
<ObjectDetail />
|
|
||||||
) : (
|
|
||||||
<div className="flex h-full items-center justify-center p-4 text-sm text-neutral-400">
|
|
||||||
{t("objects.selectPrompt")}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
|
export function SelectPrompt() {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex h-full items-center justify-center p-4 text-sm text-neutral-400">
|
||||||
|
{t("objects.selectPrompt")}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user