Files
simrsx-fe/app/handlers/material.handler.ts
T
2025-09-25 11:36:14 +07:00

112 lines
3.1 KiB
TypeScript

import { ref } from 'vue'
// Services
import { postSourceMaterial, patchSourceMaterial, removeSourceMaterial } from '~/services/material.service'
const recId = ref<number>(0)
const recAction = ref<string>('')
const recItem = ref<any>(null)
const isReadonly = ref(false)
const isProcessing = ref(false)
const isFormEntryDialogOpen = ref(false)
const isRecordConfirmationOpen = ref(false)
function onResetState() {
recId.value = 0
recAction.value = ''
recItem.value = null
}
export async function handleActionSave(
values: any,
refresh: () => void,
reset: () => void,
toast: (params: any) => void,
) {
let isSuccess = false
isProcessing.value = true
try {
const result = await postSourceMaterial(values)
if (result.success) {
toast({ title: 'Berhasil', description: 'Data berhasil disimpan', variant: 'default' })
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
}
}
export async function handleActionEdit(
id: number | string,
values: any,
refresh: () => void,
reset: () => void,
toast: (params: any) => void,
) {
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' })
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
}
}
export async function handleActionRemove(id: number | string, refresh: () => void, toast: (params: any) => void) {
isProcessing.value = true
try {
const result = await removeSourceMaterial(id)
if (result.success) {
toast({ title: 'Berhasil', description: 'Data berhasil dihapus', variant: 'default' })
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
}
}
export function handleCancelForm(reset: () => void) {
isFormEntryDialogOpen.value = false
setTimeout(() => {
reset()
}, 500)
}
export { recId, recAction, recItem, isReadonly, isProcessing, isFormEntryDialogOpen, isRecordConfirmationOpen }