26 lines
761 B
TypeScript
26 lines
761 B
TypeScript
export function useSaveEntry(options: { url: string, dataKey: string | string[], saved: string, failed: string, onSaved: () => void }) {
|
|
const toast = useToast()
|
|
const saving = ref(false)
|
|
|
|
async function save(body: Record<string, unknown>) {
|
|
saving.value = true
|
|
try {
|
|
await $fetch(options.url, { method: 'POST', body })
|
|
await refreshNuxtData(options.dataKey)
|
|
toast.add({ title: options.saved, color: 'success', icon: 'i-lucide-check' })
|
|
options.onSaved()
|
|
} catch (error) {
|
|
toast.add({
|
|
title: options.failed,
|
|
description: errorMessage(error),
|
|
color: 'error',
|
|
icon: 'i-lucide-triangle-alert'
|
|
})
|
|
} finally {
|
|
saving.value = false
|
|
}
|
|
}
|
|
|
|
return { saving, save }
|
|
}
|