diff --git a/frontend/src/api/trash.ts b/frontend/src/api/trash.ts new file mode 100644 index 0000000..59ea008 --- /dev/null +++ b/frontend/src/api/trash.ts @@ -0,0 +1,28 @@ +import { apiGet, apiPost, apiDelete } from "@/api/client"; + +export interface TrashItem { + type: string; + id: number; + title: string; +} + +export interface TrashBatch { + batch_id: string; + deleted_at: string | null; + summary: string; + count: number; + items: TrashItem[]; +} + +export async function listTrash(): Promise { + const data = await apiGet<{ batches: TrashBatch[] }>("/api/trash"); + return data.batches; +} + +export async function restoreBatch(batchId: string): Promise { + await apiPost(`/api/trash/${batchId}/restore`, {}); +} + +export async function purgeBatch(batchId: string): Promise { + return apiDelete(`/api/trash/${batchId}`); +} diff --git a/frontend/src/stores/trash.ts b/frontend/src/stores/trash.ts new file mode 100644 index 0000000..526a90d --- /dev/null +++ b/frontend/src/stores/trash.ts @@ -0,0 +1,53 @@ +import { ref } from "vue"; +import { defineStore } from "pinia"; +import * as api from "@/api/trash"; +import type { TrashBatch } from "@/api/trash"; +import { useToastStore } from "@/stores/toast"; + +export const useTrashStore = defineStore("trash", () => { + const batches = ref([]); + const loading = ref(false); + + async function fetchTrash() { + loading.value = true; + try { + batches.value = await api.listTrash(); + } catch (e) { + useToastStore().show("Failed to load trash", "error"); + throw e; + } finally { + loading.value = false; + } + } + + async function restore(batchId: string) { + try { + await api.restoreBatch(batchId); + batches.value = batches.value.filter((b) => b.batch_id !== batchId); + useToastStore().show("Restored", "success"); + } catch (e) { + useToastStore().show("Failed to restore", "error"); + throw e; + } + } + + async function purge(batchId: string) { + try { + await api.purgeBatch(batchId); + batches.value = batches.value.filter((b) => b.batch_id !== batchId); + } catch (e) { + useToastStore().show("Failed to delete permanently", "error"); + throw e; + } + } + + async function emptyTrash() { + const ids = batches.value.map((b) => b.batch_id); + for (const id of ids) { + await api.purgeBatch(id); + } + batches.value = []; + } + + return { batches, loading, fetchTrash, restore, purge, emptyTrash }; +});