From a2c6341544f2844b2dac3dcfba9c33feb789480f Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 7 Nov 2026 01:16:48 +1100 Subject: [PATCH] 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. --- ui/src/org/OrgAccessPage.vue | 75 +++++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 5 deletions(-) diff --git a/ui/src/org/OrgAccessPage.vue b/ui/src/org/OrgAccessPage.vue index eff6e2316..90f45ee4b 100644 --- a/ui/src/org/OrgAccessPage.vue +++ b/ui/src/org/OrgAccessPage.vue @@ -40,7 +40,54 @@ const loading = ref(true); const blank = (): Policy => ({ id: "", level: "org", text: "" }); const draft = ref(blank()); +// The snapshot the draft was opened from; any difference is unsaved work. +const original = ref(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 { } 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 { 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);
- +

Loading policies…

No policies yet @@ -142,7 +195,7 @@ onMounted(load);
- +