mirror of
https://github.com/hay-kot/homebox.git
synced 2024-11-22 08:35:43 +00:00
fix: prevent resetting dialog state on error (#524)
This commit is contained in:
parent
5a219f6a9c
commit
8c7d91ea52
2 changed files with 38 additions and 7 deletions
19
frontend/composables/use-defer.ts
Normal file
19
frontend/composables/use-defer.ts
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
type DeferFunction<TArgs extends any[], TReturn> = (...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<TArgs extends any[], TReturn>(
|
||||||
|
onComplete: (...args: TArgs) => void,
|
||||||
|
func: DeferFunction<TArgs, TReturn>
|
||||||
|
): DeferFunction<TArgs, TReturn> {
|
||||||
|
return (...args: TArgs) => {
|
||||||
|
let result: TReturn;
|
||||||
|
try {
|
||||||
|
result = func(...args);
|
||||||
|
} finally {
|
||||||
|
onComplete(...args);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
}
|
|
@ -71,6 +71,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
function resetEntry() {
|
function resetEntry() {
|
||||||
|
console.log("Resetting entry");
|
||||||
entry.id = null;
|
entry.id = null;
|
||||||
entry.name = "";
|
entry.name = "";
|
||||||
entry.completedDate = null;
|
entry.completedDate = null;
|
||||||
|
@ -79,13 +80,26 @@
|
||||||
entry.cost = "";
|
entry.cost = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createEntry() {
|
watch(
|
||||||
if (entry.id) {
|
() => entry.modal,
|
||||||
editEntry();
|
(v, pv) => {
|
||||||
|
if (pv === true && v === false) {
|
||||||
resetEntry();
|
resetEntry();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Calls either edit or create depending on entry.id being set
|
||||||
|
async function dispatchFormSubmit() {
|
||||||
|
if (entry.id) {
|
||||||
|
await editEntry();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await createEntry();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function createEntry() {
|
||||||
const { error } = await api.items.maintenance.create(props.item.id, {
|
const { error } = await api.items.maintenance.create(props.item.id, {
|
||||||
name: entry.name,
|
name: entry.name,
|
||||||
completedDate: entry.completedDate ?? "",
|
completedDate: entry.completedDate ?? "",
|
||||||
|
@ -123,14 +137,13 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
function openEditDialog(e: MaintenanceEntry) {
|
function openEditDialog(e: MaintenanceEntry) {
|
||||||
console.log(e);
|
|
||||||
entry.modal = true;
|
|
||||||
entry.id = e.id;
|
entry.id = e.id;
|
||||||
entry.name = e.name;
|
entry.name = e.name;
|
||||||
entry.completedDate = new Date(e.completedDate);
|
entry.completedDate = new Date(e.completedDate);
|
||||||
entry.scheduledDate = new Date(e.scheduledDate);
|
entry.scheduledDate = new Date(e.scheduledDate);
|
||||||
entry.description = e.description;
|
entry.description = e.description;
|
||||||
entry.cost = e.cost;
|
entry.cost = e.cost;
|
||||||
|
entry.modal = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function editEntry() {
|
async function editEntry() {
|
||||||
|
@ -152,7 +165,6 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
entry.modal = false;
|
entry.modal = false;
|
||||||
|
|
||||||
refreshLog();
|
refreshLog();
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
@ -163,7 +175,7 @@
|
||||||
<template #title>
|
<template #title>
|
||||||
{{ entry.id ? "Edit Entry" : "New Entry" }}
|
{{ entry.id ? "Edit Entry" : "New Entry" }}
|
||||||
</template>
|
</template>
|
||||||
<form @submit.prevent="createEntry">
|
<form @submit.prevent="dispatchFormSubmit">
|
||||||
<FormTextField v-model="entry.name" autofocus label="Entry Name" />
|
<FormTextField v-model="entry.name" autofocus label="Entry Name" />
|
||||||
<DatePicker v-model="entry.completedDate" label="Completed Date" />
|
<DatePicker v-model="entry.completedDate" label="Completed Date" />
|
||||||
<DatePicker v-model="entry.scheduledDate" label="Scheduled Date" />
|
<DatePicker v-model="entry.scheduledDate" label="Scheduled Date" />
|
||||||
|
|
Loading…
Reference in a new issue