102 lines
2.4 KiB
TypeScript
102 lines
2.4 KiB
TypeScript
import { ref } from 'vue'
|
|
|
|
// Handlers
|
|
import { type ToastFn, handleAsyncAction } from '~/handlers/_handler'
|
|
|
|
// Services
|
|
import { postDevice, patchDevice, removeDevice } from '~/services/device.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: ToastFn
|
|
) {
|
|
isProcessing.value = true;
|
|
await handleAsyncAction<[any], any>({
|
|
action: postDevice,
|
|
args: [values],
|
|
toast,
|
|
successMessage: 'Data berhasil disimpan',
|
|
errorMessage: 'Gagal menyimpan data',
|
|
onSuccess: () => {
|
|
isFormEntryDialogOpen.value = false;
|
|
if (refresh) refresh();
|
|
},
|
|
onFinally: (isSuccess: boolean) => {
|
|
if (isSuccess) setTimeout(reset, 500);
|
|
isProcessing.value = false;
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function handleActionEdit(
|
|
id: number | string,
|
|
values: any,
|
|
refresh: () => void,
|
|
reset: () => void,
|
|
toast: ToastFn
|
|
) {
|
|
isProcessing.value = true;
|
|
await handleAsyncAction<[number | string, any], any>({
|
|
action: patchDevice,
|
|
args: [id, values],
|
|
toast,
|
|
successMessage: 'Data berhasil diubah',
|
|
errorMessage: 'Gagal mengubah data',
|
|
onSuccess: () => {
|
|
isFormEntryDialogOpen.value = false;
|
|
if (refresh) refresh();
|
|
},
|
|
onFinally: (isSuccess: boolean) => {
|
|
if (isSuccess) setTimeout(reset, 500);
|
|
isProcessing.value = false;
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function handleActionRemove(
|
|
id: number | string,
|
|
refresh: () => void,
|
|
toast: ToastFn
|
|
) {
|
|
isProcessing.value = true;
|
|
await handleAsyncAction<[number | string], any>({
|
|
action: removeDevice,
|
|
args: [id],
|
|
toast,
|
|
successMessage: 'Data berhasil dihapus',
|
|
errorMessage: 'Gagal menghapus data',
|
|
onSuccess: () => {
|
|
if (refresh) refresh();
|
|
},
|
|
onFinally: () => {
|
|
onResetState();
|
|
isProcessing.value = false;
|
|
},
|
|
});
|
|
}
|
|
|
|
export function handleCancelForm(reset: () => void) {
|
|
isFormEntryDialogOpen.value = false
|
|
setTimeout(() => {
|
|
reset()
|
|
}, 500)
|
|
}
|
|
|
|
export { recId, recAction, recItem, isReadonly, isProcessing, isFormEntryDialogOpen, isRecordConfirmationOpen }
|