feat(web): delete object with confirm dialog
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -6,5 +6,5 @@
|
|||||||
"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" }
|
"actions": { "edit": "Edit", "delete": "Delete", "confirmDelete": "Delete this object? This cannot be undone." }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,5 +6,5 @@
|
|||||||
"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" }
|
"actions": { "edit": "Redigera", "delete": "Ta bort", "confirmDelete": "Ta bort detta föremål? Detta kan inte ångras." }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
import { expect, test } from "vitest";
|
||||||
|
import { screen, waitFor, within } 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 { DeleteObjectDialog } from "./delete-object-dialog";
|
||||||
|
|
||||||
|
function tree() {
|
||||||
|
return (
|
||||||
|
<Routes>
|
||||||
|
<Route path="/objects/:id" element={<DeleteObjectDialog id="o-1" />} />
|
||||||
|
<Route path="/objects" element={<div>objects list</div>} />
|
||||||
|
</Routes>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
test("confirm delete: DELETE then navigate to the list", async () => {
|
||||||
|
let deleted = false;
|
||||||
|
|
||||||
|
server.use(
|
||||||
|
http.delete("/api/admin/objects/:id", () => {
|
||||||
|
deleted = true;
|
||||||
|
return new HttpResponse(null, { status: 204 });
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
renderApp(tree(), { route: "/objects/o-1" });
|
||||||
|
|
||||||
|
// open the dialog via the trigger
|
||||||
|
await userEvent.click(await screen.findByRole("button", { name: /delete/i }));
|
||||||
|
|
||||||
|
// confirm inside the dialog
|
||||||
|
const dialog = await screen.findByRole("alertdialog");
|
||||||
|
|
||||||
|
await userEvent.click(within(dialog).getByRole("button", { name: /delete/i }));
|
||||||
|
|
||||||
|
await waitFor(() => expect(screen.getByText("objects list")).toBeInTheDocument());
|
||||||
|
|
||||||
|
expect(deleted).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("cancel does not delete", async () => {
|
||||||
|
let deleted = false;
|
||||||
|
|
||||||
|
server.use(
|
||||||
|
http.delete("/api/admin/objects/:id", () => {
|
||||||
|
deleted = true;
|
||||||
|
return new HttpResponse(null, { status: 204 });
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
renderApp(tree(), { route: "/objects/o-1" });
|
||||||
|
|
||||||
|
await userEvent.click(await screen.findByRole("button", { name: /delete/i }));
|
||||||
|
|
||||||
|
const dialog = await screen.findByRole("alertdialog");
|
||||||
|
|
||||||
|
await userEvent.click(within(dialog).getByRole("button", { name: /cancel/i }));
|
||||||
|
|
||||||
|
expect(deleted).toBe(false);
|
||||||
|
expect(screen.queryByText("objects list")).not.toBeInTheDocument();
|
||||||
|
});
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
import { useState } from "react";
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
|
import { useDeleteObject } from "../api/queries";
|
||||||
|
import {
|
||||||
|
AlertDialog,
|
||||||
|
AlertDialogTrigger,
|
||||||
|
AlertDialogContent,
|
||||||
|
AlertDialogTitle,
|
||||||
|
AlertDialogDescription,
|
||||||
|
AlertDialogFooter,
|
||||||
|
AlertDialogCancel,
|
||||||
|
AlertDialogAction,
|
||||||
|
} from "@/components/ui/alert-dialog";
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
|
||||||
|
export function DeleteObjectDialog({ id }: { id: string }) {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const del = useDeleteObject();
|
||||||
|
const [open, setOpen] = useState(false);
|
||||||
|
|
||||||
|
const onConfirm = async () => {
|
||||||
|
await del.mutateAsync(id);
|
||||||
|
navigate("/objects");
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<AlertDialog open={open} onOpenChange={setOpen}>
|
||||||
|
<AlertDialogTrigger
|
||||||
|
render={
|
||||||
|
<Button variant="ghost" size="sm" className="text-red-600">
|
||||||
|
{t("actions.delete")}
|
||||||
|
</Button>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<AlertDialogContent>
|
||||||
|
<AlertDialogTitle>{t("actions.delete")}</AlertDialogTitle>
|
||||||
|
<AlertDialogDescription>{t("actions.confirmDelete")}</AlertDialogDescription>
|
||||||
|
<AlertDialogFooter>
|
||||||
|
<AlertDialogCancel>{t("form.cancel")}</AlertDialogCancel>
|
||||||
|
<AlertDialogAction onClick={onConfirm}>
|
||||||
|
{t("actions.delete")}
|
||||||
|
</AlertDialogAction>
|
||||||
|
</AlertDialogFooter>
|
||||||
|
</AlertDialogContent>
|
||||||
|
</AlertDialog>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ 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";
|
||||||
|
import { DeleteObjectDialog } from "./delete-object-dialog";
|
||||||
import { VisibilityBadge } from "./visibility-badge";
|
import { VisibilityBadge } from "./visibility-badge";
|
||||||
import { Skeleton } from "@/components/ui/skeleton";
|
import { Skeleton } from "@/components/ui/skeleton";
|
||||||
|
|
||||||
@@ -60,6 +61,7 @@ export function ObjectDetail() {
|
|||||||
<Link to={`/objects/${object.id}/edit`} className="text-sm font-medium text-indigo-600">
|
<Link to={`/objects/${object.id}/edit`} className="text-sm font-medium text-indigo-600">
|
||||||
{t("actions.edit")}
|
{t("actions.edit")}
|
||||||
</Link>
|
</Link>
|
||||||
|
<DeleteObjectDialog id={object.id} />
|
||||||
</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} />
|
||||||
|
|||||||
Reference in New Issue
Block a user