From 18650f1cbff5a93f81e96700eb993a5de6e446fb Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Tue, 23 Jun 2026 11:07:16 +0700 Subject: [PATCH] refactor: abstract delete logic into useDestroy composable to reduce code duplication in data table actions --- resources/js/composables/useDestroy.ts | 46 +++++++++++++++++++ .../finance/cash/table/data-table-actions.vue | 28 +++-------- .../table/data-table-actions.vue | 23 ++-------- .../expenses/table/data-table-actions.vue | 27 ++--------- .../table/AttendanceDetailDialog.vue | 35 ++++---------- .../attendances/table/data-table-actions.vue | 27 ++--------- .../hr/employees/table/data-table-actions.vue | 23 +++------- .../table/data-table-actions.vue | 23 ++-------- .../cuttings/table/data-table-actions.vue | 25 +++------- .../js/pages/admin/manage/orders/Show.vue | 25 +++------- .../orders/table/data-table-actions.vue | 25 +++------- .../purchases/table/data-table-actions.vue | 28 +++-------- .../categories/table/data-table-actions.vue | 27 ++--------- .../customers/table/data-table-actions.vue | 27 ++--------- .../products/table/data-table-actions.vue | 28 +++-------- .../table/data-table-actions.vue | 28 +++-------- .../suppliers/table/data-table-actions.vue | 27 ++--------- .../system/roles/table/data-table-actions.vue | 29 +++--------- 18 files changed, 147 insertions(+), 354 deletions(-) create mode 100644 resources/js/composables/useDestroy.ts diff --git a/resources/js/composables/useDestroy.ts b/resources/js/composables/useDestroy.ts new file mode 100644 index 0000000..494d0e8 --- /dev/null +++ b/resources/js/composables/useDestroy.ts @@ -0,0 +1,46 @@ +import { router } from '@inertiajs/vue3'; +import { ref } from 'vue'; +import { toast } from 'vue-sonner'; + +interface UseDestroyOptions { + url: string; + preserveScroll?: boolean; + errorMessage?: string; + onSuccess?: () => void; + onError?: (errors: Record) => string | void; +} + +export function useDestroy({ url, preserveScroll = true, errorMessage, onSuccess, onError }: UseDestroyOptions) { + const open = ref(false); + const processing = ref(false); + + function destroy() { + processing.value = true; + + router.delete(url, { + preserveScroll, + onSuccess: () => { + open.value = false; + onSuccess?.(); + }, + onError: (errors) => { + if (onError) { + const result = onError(errors); + if (typeof result === 'string') { + toast.error(result); + return; + } + } + + if (errorMessage) { + toast.error(errorMessage); + } + }, + onFinish: () => { + processing.value = false; + }, + }); + } + + return { open, processing, destroy }; +} diff --git a/resources/js/pages/admin/finance/cash/table/data-table-actions.vue b/resources/js/pages/admin/finance/cash/table/data-table-actions.vue index 55981e5..63615ed 100644 --- a/resources/js/pages/admin/finance/cash/table/data-table-actions.vue +++ b/resources/js/pages/admin/finance/cash/table/data-table-actions.vue @@ -1,12 +1,11 @@