chore: add handler parent
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import { ref } from 'vue'
|
||||
|
||||
// Handlers
|
||||
import { type ToastFn, handleAsyncAction } from '~/handlers/_handler'
|
||||
|
||||
// Services
|
||||
import { postSourceMaterial, patchSourceMaterial, removeSourceMaterial } from '~/services/material.service'
|
||||
|
||||
@@ -17,36 +20,23 @@ function onResetState() {
|
||||
recItem.value = null
|
||||
}
|
||||
|
||||
export async function handleActionSave(
|
||||
values: any,
|
||||
refresh: () => void,
|
||||
reset: () => void,
|
||||
toast: (params: any) => void,
|
||||
) {
|
||||
let isSuccess = false
|
||||
export async function handleActionSave(values: any, refresh: () => void, reset: () => void, toast: ToastFn) {
|
||||
isProcessing.value = true
|
||||
try {
|
||||
const result = await postSourceMaterial(values)
|
||||
if (result.success) {
|
||||
toast({ title: 'Berhasil', description: 'Data berhasil disimpan', variant: 'default' })
|
||||
await handleAsyncAction<[any], any>({
|
||||
action: postSourceMaterial,
|
||||
args: [values],
|
||||
toast,
|
||||
successMessage: 'Data berhasil disimpan',
|
||||
errorMessage: 'Gagal menyimpan data',
|
||||
onSuccess: () => {
|
||||
isFormEntryDialogOpen.value = false
|
||||
isSuccess = true
|
||||
if (refresh) refresh()
|
||||
} else {
|
||||
toast({ title: 'Gagal', description: 'Gagal menyimpan data', variant: 'destructive' })
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('Error saving form:', error)
|
||||
toast({ title: 'Gagal', description: 'Gagal menyimpan data', variant: 'destructive' })
|
||||
isSuccess = false
|
||||
} finally {
|
||||
if (isSuccess) {
|
||||
setTimeout(() => {
|
||||
reset()
|
||||
}, 500)
|
||||
}
|
||||
isProcessing.value = false
|
||||
}
|
||||
},
|
||||
onFinally: (isSuccess: boolean) => {
|
||||
if (isSuccess) setTimeout(reset, 500)
|
||||
isProcessing.value = false
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export async function handleActionEdit(
|
||||
@@ -54,51 +44,42 @@ export async function handleActionEdit(
|
||||
values: any,
|
||||
refresh: () => void,
|
||||
reset: () => void,
|
||||
toast: (params: any) => void,
|
||||
toast: ToastFn,
|
||||
) {
|
||||
let isSuccess = false
|
||||
isProcessing.value = true
|
||||
try {
|
||||
const result = await patchSourceMaterial(id, values)
|
||||
if (result.success) {
|
||||
toast({ title: 'Berhasil', description: 'Data berhasil diubah', variant: 'default' })
|
||||
await handleAsyncAction<[number | string, any], any>({
|
||||
action: patchSourceMaterial,
|
||||
args: [id, values],
|
||||
toast,
|
||||
successMessage: 'Data berhasil diubah',
|
||||
errorMessage: 'Gagal mengubah data',
|
||||
onSuccess: () => {
|
||||
isFormEntryDialogOpen.value = false
|
||||
isSuccess = true
|
||||
if (refresh) refresh()
|
||||
} else {
|
||||
toast({ title: 'Gagal', description: 'Gagal mengubah data', variant: 'destructive' })
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('Error editing form:', error)
|
||||
toast({ title: 'Gagal', description: 'Gagal mengubah data', variant: 'destructive' })
|
||||
isSuccess = false
|
||||
} finally {
|
||||
if (isSuccess) {
|
||||
setTimeout(() => {
|
||||
reset()
|
||||
}, 500)
|
||||
}
|
||||
isProcessing.value = false
|
||||
}
|
||||
},
|
||||
onFinally: (isSuccess: boolean) => {
|
||||
if (isSuccess) setTimeout(reset, 500)
|
||||
isProcessing.value = false
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export async function handleActionRemove(id: number | string, refresh: () => void, toast: (params: any) => void) {
|
||||
export async function handleActionRemove(id: number | string, refresh: () => void, toast: ToastFn) {
|
||||
isProcessing.value = true
|
||||
try {
|
||||
const result = await removeSourceMaterial(id)
|
||||
if (result.success) {
|
||||
toast({ title: 'Berhasil', description: 'Data berhasil dihapus', variant: 'default' })
|
||||
await handleAsyncAction<[number | string], any>({
|
||||
action: removeSourceMaterial,
|
||||
args: [id],
|
||||
toast,
|
||||
successMessage: 'Data berhasil dihapus',
|
||||
errorMessage: 'Gagal menghapus data',
|
||||
onSuccess: () => {
|
||||
if (refresh) refresh()
|
||||
} else {
|
||||
toast({ title: 'Gagal', description: 'Gagal menghapus data', variant: 'destructive' })
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error deleting record:', error)
|
||||
toast({ title: 'Gagal', description: 'Gagal menghapus data', variant: 'destructive' })
|
||||
} finally {
|
||||
onResetState()
|
||||
isProcessing.value = false
|
||||
}
|
||||
},
|
||||
onFinally: () => {
|
||||
onResetState()
|
||||
isProcessing.value = false
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function handleCancelForm(reset: () => void) {
|
||||
|
||||
Reference in New Issue
Block a user