mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 07:42:23 +00:00
feat(illness): restore a soft-deleted episode via delete-Undo
Add POST /api/illness/episodes/{id}/restore: clears deletedAt so the
episode and its preserved day-logs and encrypted note re-surface in
every read. Soft-delete keeps the row and its children intact, so
restore is a pure flag flip rather than a re-create. apiHandler +
idempotency + module gate + owner-scoped 404; restoring a live episode
is a no-op.
Wire the episode-menu delete to a restore Undo toast, mirroring the
labs delete-Undo affordance. Register the route in the OpenAPI source
and regenerate the contract.
This commit is contained in:
1 parent
c5fd21b8a6
commit
55e7835ef4
12 files changed
+350
-1
No files matched your search
@@ -9,6 +9,7 @@
|
||||
*/
|
||||
import { useState } from "react";
|
||||
import { MoreVertical, Pencil, CheckCircle2, Trash2 } from "lucide-react";
|
||||
import { toast } from "sonner";
|
||||
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
@@ -30,7 +31,7 @@ import {
|
||||
} from "@/components/ui/alert-dialog";
|
||||
import { useTranslations } from "@/lib/i18n/context";
|
||||
|
||||
import { useDeleteEpisode } from "./use-illness";
|
||||
import { useDeleteEpisode, useRestoreEpisode } from "./use-illness";
|
||||
import type { IllnessEpisodeDTO } from "./types";
|
||||
|
||||
export interface EpisodeMenuProps {
|
||||
@@ -52,6 +53,7 @@ export function EpisodeMenu({
|
||||
}: EpisodeMenuProps) {
|
||||
const { t } = useTranslations();
|
||||
const del = useDeleteEpisode();
|
||||
const restore = useRestoreEpisode();
|
||||
const [confirmOpen, setConfirmOpen] = useState(false);
|
||||
|
||||
async function handleConfirmDelete() {
|
||||
@@ -59,6 +61,19 @@ export function EpisodeMenu({
|
||||
await del.mutateAsync(episode.id);
|
||||
setConfirmOpen(false);
|
||||
onDeleted?.();
|
||||
// Soft-delete keeps the episode + its day-logs intact, so the delete is
|
||||
// reversible. Surface an Undo toast wired to the restore route (the labs
|
||||
// delete+undo UX).
|
||||
toast.success(t("illness.deletedToast"), {
|
||||
action: {
|
||||
label: t("common.undo"),
|
||||
onClick: () => {
|
||||
restore.mutate(episode.id, {
|
||||
onError: () => toast.error(t("illness.restoreError")),
|
||||
});
|
||||
},
|
||||
},
|
||||
});
|
||||
} catch {
|
||||
// Keep the dialog open; the list will surface any error on next read.
|
||||
}
|
||||
|
||||
@@ -148,6 +148,20 @@ export function useDeleteEpisode() {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore a soft-deleted episode (the delete-Undo affordance). Clears
|
||||
* `deletedAt` server-side, bringing the episode and its preserved day-logs
|
||||
* back. Idempotent + owner-scoped.
|
||||
*/
|
||||
export function useRestoreEpisode() {
|
||||
const invalidate = useInvalidateIllness();
|
||||
return useMutation({
|
||||
mutationFn: (id: string) =>
|
||||
apiPost<IllnessEpisodeDTO>(`/api/illness/episodes/${id}/restore`, {}),
|
||||
onSuccess: invalidate,
|
||||
});
|
||||
}
|
||||
|
||||
/** Upsert one day-log on an episode. */
|
||||
export function useUpsertDayLog(episodeId: string) {
|
||||
const invalidate = useInvalidateIllness();
|
||||
|
||||
Reference in new issue
Block a user