30 lines
877 B
TypeScript
30 lines
877 B
TypeScript
function capitalize(word: string): string {
|
|
return word.charAt(0).toUpperCase() + word.slice(1)
|
|
}
|
|
|
|
export function useSaveEntry(options: { url: string, dataKey: string, noun: 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: `${capitalize(options.noun)} saved`, color: 'success', icon: 'i-lucide-check' })
|
|
options.onSaved()
|
|
} catch (error) {
|
|
toast.add({
|
|
title: `Couldn't save the ${options.noun}`,
|
|
description: errorMessage(error),
|
|
color: 'error',
|
|
icon: 'i-lucide-triangle-alert'
|
|
})
|
|
} finally {
|
|
saving.value = false
|
|
}
|
|
}
|
|
|
|
return { saving, save }
|
|
}
|