fix(ui): policy form opens blank on New, id read-only when editing, dirty draft asks first (#1730)
New policy now resets the draft instead of reusing the open policy's, so Save cannot overwrite the policy that happens to be open. The id input is readonly while editing a persisted policy, so a rename can no longer write a second policy. New/Edit/Cancel route through openForm, which asks for confirmation before discarding a dirty draft.
This commit is contained in:
1 file changed
+70
-5
@@ -40,7 +40,54 @@ const loading = ref(true);
|
||||
|
||||
const blank = (): Policy => ({ id: "", level: "org", text: "" });
|
||||
const draft = ref<Policy>(blank());
|
||||
// The snapshot the draft was opened from; any difference is unsaved work.
|
||||
const original = ref<Policy>(blank());
|
||||
const editing = ref(false);
|
||||
// True while the draft is a persisted policy: its id is then read-only, so
|
||||
// Save can never write a second policy and leave the original in place.
|
||||
const editingExisting = ref(false);
|
||||
|
||||
const dirty = computed(() => JSON.stringify(draft.value) !== JSON.stringify(original.value));
|
||||
|
||||
// Leaving a dirty draft (New, Edit on another row, Cancel) asks first.
|
||||
const discardConfirm = ref(false);
|
||||
let afterDiscard: (() => void) | null = null;
|
||||
|
||||
function openForm(next: () => void): void {
|
||||
if (dirty.value) {
|
||||
afterDiscard = next;
|
||||
discardConfirm.value = true;
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
}
|
||||
|
||||
function newPolicy(): void {
|
||||
openForm(() => {
|
||||
draft.value = blank();
|
||||
original.value = blank();
|
||||
editingExisting.value = false;
|
||||
editing.value = true;
|
||||
});
|
||||
}
|
||||
|
||||
function cancel(): void {
|
||||
openForm(() => {
|
||||
draft.value = blank();
|
||||
editing.value = false;
|
||||
});
|
||||
}
|
||||
|
||||
function confirmDiscard(): void {
|
||||
const fn = afterDiscard;
|
||||
closeDiscard();
|
||||
fn?.();
|
||||
}
|
||||
|
||||
function closeDiscard(): void {
|
||||
discardConfirm.value = false;
|
||||
afterDiscard = null;
|
||||
}
|
||||
|
||||
const scope = (p: Policy) =>
|
||||
p.level === "workspace"
|
||||
@@ -73,8 +120,12 @@ async function load(): Promise<void> {
|
||||
}
|
||||
|
||||
function edit(p: Policy): void {
|
||||
draft.value = { ...p };
|
||||
editing.value = true;
|
||||
openForm(() => {
|
||||
draft.value = { ...p };
|
||||
original.value = { ...p };
|
||||
editingExisting.value = true;
|
||||
editing.value = true;
|
||||
});
|
||||
}
|
||||
|
||||
const saveError = ref("");
|
||||
@@ -84,7 +135,9 @@ async function save(): Promise<void> {
|
||||
try {
|
||||
await api.putPolicy(draft.value);
|
||||
draft.value = blank();
|
||||
original.value = blank();
|
||||
editing.value = false;
|
||||
editingExisting.value = false;
|
||||
await load();
|
||||
} catch (err) {
|
||||
saveError.value = (err as Error).message;
|
||||
@@ -113,7 +166,7 @@ onMounted(load);
|
||||
<ReadError v-if="error" :message="error" retry-label="Retry" class="mb-4" @retry="load" />
|
||||
|
||||
<Section>
|
||||
<template #action><Button size="sm" @click="editing = true">New policy</Button></template>
|
||||
<template #action><Button size="sm" @click="newPolicy">New policy</Button></template>
|
||||
<div class="flex flex-col gap-3">
|
||||
<p v-if="loading" class="flex items-center gap-2 text-label text-fg-muted"><Spinner />Loading policies…</p>
|
||||
<Empty v-else-if="!error && !policies.length"><EmptyHeader><EmptyTitle>No policies yet</EmptyTitle></EmptyHeader></Empty>
|
||||
@@ -142,7 +195,7 @@ onMounted(load);
|
||||
<Section v-if="editing" :title="draft.id ? draft.id : 'New policy'">
|
||||
<div class="flex flex-col gap-3">
|
||||
<div class="flex flex-wrap gap-2">
|
||||
<Input v-model="draft.id" placeholder="policy id" aria-label="Policy id" class="w-56" />
|
||||
<Input v-model="draft.id" :readonly="editingExisting" placeholder="policy id" aria-label="Policy id" class="w-56" />
|
||||
<Select v-model="draft.level">
|
||||
<SelectTrigger class="w-40" aria-label="Level"><SelectValue /></SelectTrigger>
|
||||
<SelectContent>
|
||||
@@ -173,11 +226,23 @@ onMounted(load);
|
||||
<ReadError v-if="saveError" :message="saveError" class="mb-2" />
|
||||
<div class="flex gap-2">
|
||||
<Button :disabled="!canSave || saving" @click="save">Save</Button>
|
||||
<Button variant="outline" @click="(editing = false), (draft = blank())">Cancel</Button>
|
||||
<Button variant="outline" @click="cancel">Cancel</Button>
|
||||
</div>
|
||||
</div>
|
||||
</Section>
|
||||
</div>
|
||||
<AlertDialog :open="discardConfirm" @update:open="(v: boolean) => { if (!v) closeDiscard(); }">
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>Discard unsaved changes?</AlertDialogTitle>
|
||||
<AlertDialogDescription>The policy form has edits that have not been saved.</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>Keep editing</AlertDialogCancel>
|
||||
<AlertDialogAction @click="confirmDiscard">Discard changes</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
<AlertDialog :open="pending !== null" @update:open="(v: boolean) => { if (!v) pending = null; }">
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
|
||||
Reference in new issue
Block a user