diff --git a/frontend/composables/use-defer.ts b/frontend/composables/use-defer.ts new file mode 100644 index 0000000..17e8436 --- /dev/null +++ b/frontend/composables/use-defer.ts @@ -0,0 +1,19 @@ +type DeferFunction = (...args: TArgs) => TReturn; + +// useDefer is a function that takes a function and returns a function that +// calls the original function and then calls the onComplete function. +export function useDefer( + onComplete: (...args: TArgs) => void, + func: DeferFunction +): DeferFunction { + return (...args: TArgs) => { + let result: TReturn; + try { + result = func(...args); + } finally { + onComplete(...args); + } + + return result; + }; +} diff --git a/frontend/pages/item/[id]/index/maintenance.vue b/frontend/pages/item/[id]/index/maintenance.vue index cf6055d..7042525 100644 --- a/frontend/pages/item/[id]/index/maintenance.vue +++ b/frontend/pages/item/[id]/index/maintenance.vue @@ -71,6 +71,7 @@ } function resetEntry() { + console.log("Resetting entry"); entry.id = null; entry.name = ""; entry.completedDate = null; @@ -79,13 +80,26 @@ entry.cost = ""; } - async function createEntry() { + watch( + () => entry.modal, + (v, pv) => { + if (pv === true && v === false) { + resetEntry(); + } + } + ); + + // Calls either edit or create depending on entry.id being set + async function dispatchFormSubmit() { if (entry.id) { - editEntry(); - resetEntry(); + await editEntry(); return; } + await createEntry(); + } + + async function createEntry() { const { error } = await api.items.maintenance.create(props.item.id, { name: entry.name, completedDate: entry.completedDate ?? "", @@ -123,14 +137,13 @@ } function openEditDialog(e: MaintenanceEntry) { - console.log(e); - entry.modal = true; entry.id = e.id; entry.name = e.name; entry.completedDate = new Date(e.completedDate); entry.scheduledDate = new Date(e.scheduledDate); entry.description = e.description; entry.cost = e.cost; + entry.modal = true; } async function editEntry() { @@ -152,7 +165,6 @@ } entry.modal = false; - refreshLog(); } @@ -163,7 +175,7 @@ -
+