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);
- +