From b03a47ddc660f511495710a400cf3eb54822b280 Mon Sep 17 00:00:00 2001 From: ValdiANS Date: Tue, 27 Jan 2026 17:03:06 +0700 Subject: [PATCH] refactor: delete unnecessary page and component --- .../transfer-to-laying/add/page.tsx | 11 - .../transfer-to-laying/detail/edit/page.tsx | 63 -- .../transfer-to-laying/detail/layout.tsx | 11 - .../transfer-to-laying/detail/page.tsx | 56 -- .../form/TransferToLayingForm.tsx | 932 ------------------ 5 files changed, 1073 deletions(-) delete mode 100644 src/app/production/transfer-to-laying/add/page.tsx delete mode 100644 src/app/production/transfer-to-laying/detail/edit/page.tsx delete mode 100644 src/app/production/transfer-to-laying/detail/layout.tsx delete mode 100644 src/app/production/transfer-to-laying/detail/page.tsx delete mode 100644 src/components/pages/production/transfer-to-laying/form/TransferToLayingForm.tsx diff --git a/src/app/production/transfer-to-laying/add/page.tsx b/src/app/production/transfer-to-laying/add/page.tsx deleted file mode 100644 index 6f29085f..00000000 --- a/src/app/production/transfer-to-laying/add/page.tsx +++ /dev/null @@ -1,11 +0,0 @@ -import TransferToLayingForm from '@/components/pages/production/transfer-to-laying/form/TransferToLayingForm'; - -const AddTransferToLaying = () => { - return ( -
- -
- ); -}; - -export default AddTransferToLaying; diff --git a/src/app/production/transfer-to-laying/detail/edit/page.tsx b/src/app/production/transfer-to-laying/detail/edit/page.tsx deleted file mode 100644 index d5498e08..00000000 --- a/src/app/production/transfer-to-laying/detail/edit/page.tsx +++ /dev/null @@ -1,63 +0,0 @@ -'use client'; - -import { useRouter, useSearchParams } from 'next/navigation'; -import useSWR from 'swr'; - -import TransferToLayingForm from '@/components/pages/production/transfer-to-laying/form/TransferToLayingForm'; - -import { TransferToLayingApi } from '@/services/api/production/transfer-to-laying'; -import { isResponseError, isResponseSuccess } from '@/lib/api-helper'; - -const TransferToLayingEdit = () => { - const router = useRouter(); - const searchParams = useSearchParams(); - - const transferToLayingId = searchParams.get('transferToLayingId'); - - const { data: transferToLaying, isLoading: isLoadingTransferToLaying } = - useSWR(transferToLayingId, (id: number) => - TransferToLayingApi.getSingle(id) - ); - - if (!transferToLayingId) { - router.back(); - - return ( -
- -
- ); - } - - if ( - !isLoadingTransferToLaying && - (!transferToLaying || isResponseError(transferToLaying)) - ) { - router.replace('/404'); - return; - } - - if ( - isResponseSuccess(transferToLaying) && - transferToLaying.data.approval.step_number === 2 - ) { - router.replace('/production/transfer-to-laying'); - return; - } - - return ( -
- {isLoadingTransferToLaying && ( - - )} - {!isLoadingTransferToLaying && isResponseSuccess(transferToLaying) && ( - - )} -
- ); -}; - -export default TransferToLayingEdit; diff --git a/src/app/production/transfer-to-laying/detail/layout.tsx b/src/app/production/transfer-to-laying/detail/layout.tsx deleted file mode 100644 index 7220dfa1..00000000 --- a/src/app/production/transfer-to-laying/detail/layout.tsx +++ /dev/null @@ -1,11 +0,0 @@ -import SuspenseHelper from '@/components/helper/SuspenseHelper'; - -const Layout = ({ - children, -}: Readonly<{ - children: React.ReactNode; -}>) => { - return {children}; -}; - -export default Layout; diff --git a/src/app/production/transfer-to-laying/detail/page.tsx b/src/app/production/transfer-to-laying/detail/page.tsx deleted file mode 100644 index 9ff6ed5e..00000000 --- a/src/app/production/transfer-to-laying/detail/page.tsx +++ /dev/null @@ -1,56 +0,0 @@ -'use client'; - -import { useRouter, useSearchParams } from 'next/navigation'; -import useSWR from 'swr'; - -import TransferToLayingForm from '@/components/pages/production/transfer-to-laying/form/TransferToLayingForm'; - -import { TransferToLayingApi } from '@/services/api/production/transfer-to-laying'; -import { isResponseError, isResponseSuccess } from '@/lib/api-helper'; - -const TransferToLayingDetail = () => { - const router = useRouter(); - const searchParams = useSearchParams(); - - const transferToLayingId = searchParams.get('transferToLayingId'); - - const { data: transferToLaying, isLoading: isLoadingTransferToLaying } = - useSWR(transferToLayingId, (id: number) => - TransferToLayingApi.getSingle(id) - ); - - if (!transferToLayingId) { - router.back(); - - return ( -
- -
- ); - } - - if ( - !isLoadingTransferToLaying && - (!transferToLaying || isResponseError(transferToLaying)) - ) { - router.replace('/404'); - return; - } - - return ( -
- {isLoadingTransferToLaying && ( - - )} - - {!isLoadingTransferToLaying && isResponseSuccess(transferToLaying) && ( - - )} -
- ); -}; - -export default TransferToLayingDetail; diff --git a/src/components/pages/production/transfer-to-laying/form/TransferToLayingForm.tsx b/src/components/pages/production/transfer-to-laying/form/TransferToLayingForm.tsx deleted file mode 100644 index a257af0d..00000000 --- a/src/components/pages/production/transfer-to-laying/form/TransferToLayingForm.tsx +++ /dev/null @@ -1,932 +0,0 @@ -'use client'; - -import { useCallback, useEffect, useState } from 'react'; -import { useRouter } from 'next/navigation'; -import { useFormik } from 'formik'; -import { toast } from 'react-hot-toast'; -import useSWR from 'swr'; - -import { Icon } from '@iconify/react'; -import Button from '@/components/Button'; -import RequirePermission from '@/components/helper/RequirePermission'; -import SelectInput, { - OptionType, - useSelect, -} from '@/components/input/SelectInput'; -import TextArea from '@/components/input/TextArea'; -import { useModal } from '@/components/Modal'; -import ConfirmationModal from '@/components/modal/ConfirmationModal'; -import DateInput from '@/components/input/DateInput'; -import NumberInput from '@/components/input/NumberInput'; -import ConfirmationModalWithNotes from '@/components/modal/ConfirmationModalWithNotes'; -import ApprovalSteps, { - formatGroupedApprovalsToApprovalSteps, -} from '@/components/pages/ApprovalSteps'; - -import { - getFilledTransferToLayingFormInitialValues, - getTransferToLayingFormInitialValues, - TransferToLayingFormSchema, - TransferToLayingFormValues, - UpdateTransferToLayingFormSchema, -} from '@/components/pages/production/transfer-to-laying/form/TransferToLayingForm.schema'; -import { isResponseError, isResponseSuccess } from '@/lib/api-helper'; -import { - TransferToLaying, - CreateTransferToLayingPayload, - UpdateTransferToLayingPayload, -} from '@/types/api/production/transfer-to-laying'; -import { cn } from '@/lib/helper'; - -import { TransferToLayingApi } from '@/services/api/production/transfer-to-laying'; -import { ProjectFlock } from '@/types/api/production/project-flock'; -import { TRANSFER_TO_LAYING_APPROVAL_LINE } from '@/config/approval-line'; -import { useFormikErrorList } from '@/services/hooks/useFormikErrorList'; -import AlertErrorList from '@/components/helper/form/FormErrors'; - -interface TransferToLayingFormProps { - type?: 'add' | 'edit' | 'detail'; - initialValues?: TransferToLaying; -} - -const TransferToLayingForm = ({ - type = 'add', - initialValues, -}: TransferToLayingFormProps) => { - const router = useRouter(); - - // Modal hooks - const deleteModal = useModal(); - const approveModal = useModal(); - const rejectModal = useModal(); - - const [formErrorMessage, setFormErrorMessage] = useState(''); - - // Modal loading state - const [isDeleteLoading, setIsDeleteLoading] = useState(false); - const [isApproveLoading, setIsApproveLoading] = useState(false); - const [isRejectLoading, setIsRejectLoading] = useState(false); - - const { data: approvalHistory, isLoading: isLoadingApprovalHistory } = useSWR( - type === 'detail' && initialValues ? [String(initialValues.id)] : null, - ([id]: string[]) => TransferToLayingApi.getApprovalHistory(Number(id)) - ); - - const createTransferToLayingHandler = useCallback( - async (payload: CreateTransferToLayingPayload) => { - const createTransferToLayingRes = - await TransferToLayingApi.create(payload); - - if (isResponseError(createTransferToLayingRes)) { - setFormErrorMessage(createTransferToLayingRes.message); - return; - } - - toast.success(createTransferToLayingRes?.message as string); - router.push('/production/transfer-to-laying'); - }, - [router] - ); - - const updateTransferToLayingHandler = useCallback( - async ( - transferToLayingId: number, - payload: UpdateTransferToLayingPayload - ) => { - const updateKandangRes = await TransferToLayingApi.update( - transferToLayingId, - payload - ); - - if (updateKandangRes?.status === 'error') { - setFormErrorMessage(updateKandangRes.message); - return; - } - - toast.success(updateKandangRes?.message as string); - router.refresh(); - router.push('/production/transfer-to-laying'); - }, - [router] - ); - - // const formikInitialValues = useMemo(() => { - // return getTransferToLayingFormInitialValues(initialValues); - // }, [initialValues]); - - const [formikInitialValues, setFormikInitialValues] = useState( - getTransferToLayingFormInitialValues() - ); - - const formik = useFormik({ - initialValues: formikInitialValues, - validationSchema: - type === 'edit' - ? UpdateTransferToLayingFormSchema - : TransferToLayingFormSchema, - onSubmit: async (values) => { - setFormErrorMessage(''); - - const transferToLayingPayload: CreateTransferToLayingPayload = { - transfer_date: values.transfer_date as string, - source_project_flock_id: values.flockSource?.value as number, - target_project_flock_id: values.flockDestination?.value as number, - totalQuantity: values.totalQuantity as number, - - source_kandangs: values.flockSourceKandangs?.map((kandang) => ({ - project_flock_kandang_id: kandang.kandang.value, - quantity: parseFloat(kandang.quantity as string), - })) as CreateTransferToLayingPayload['source_kandangs'], - - target_kandangs: values.flockDestinationKandangs?.map((kandang) => ({ - project_flock_kandang_id: kandang.kandang.value, - quantity: parseFloat(kandang.quantity as string), - })) as CreateTransferToLayingPayload['target_kandangs'], - - reason: values.reason as string, - }; - - switch (type) { - case 'add': - await createTransferToLayingHandler(transferToLayingPayload); - break; - - case 'edit': - await updateTransferToLayingHandler( - initialValues?.id as number, - transferToLayingPayload - ); - break; - } - }, - }); - - const { setValues: formikSetValues, values: formikValues } = formik; - const { formErrorList, close, handleFormSubmit } = useFormikErrorList(formik); - const { - flockSourceKandangs: flockSourceKandangsValue, - flockDestinationKandangs: flockDestinationKandangsValue, - totalQuantity, - } = formikValues; - - const deleteTransferToLayingClickHandler = () => { - deleteModal.openModal(); - }; - - const approveClickHandler = () => { - approveModal.openModal(); - }; - - const rejectClickHandler = () => { - rejectModal.openModal(); - }; - - // Modal confirm click handler - const confirmationModalDeleteClickHandler = async () => { - setIsDeleteLoading(true); - - try { - await TransferToLayingApi.delete(initialValues?.id as number); - - toast.success('Berhasil menghapus data transfer ke laying!'); - router.push('/production/transfer-to-laying'); - } catch (error) { - toast.success('Gagal menghapus data transfer ke laying!'); - } finally { - deleteModal.closeModal(); - setIsDeleteLoading(false); - } - }; - - const confirmationModalApproveClickHandler = async (notes: string) => { - setIsApproveLoading(true); - - const approveResponse = await TransferToLayingApi.approve( - initialValues?.id as number, - notes - ); - - if (isResponseSuccess(approveResponse)) { - approveModal.closeModal(); - - toast.success('Berhasil approve data transfer ke laying!'); - router.push('/production/transfer-to-laying'); - } else { - approveModal.closeModal(); - - toast.error('Gagal approve data transfer ke laying!'); - } - - setIsApproveLoading(false); - }; - - const confirmationModalRejectClickHandler = async (notes: string) => { - setIsRejectLoading(true); - - const rejectResponse = await TransferToLayingApi.reject( - initialValues?.id as number, - notes - ); - - if (isResponseSuccess(rejectResponse)) { - rejectModal.closeModal(); - - toast.success('Berhasil reject data transfer ke laying!'); - router.push('/production/transfer-to-laying'); - } else { - rejectModal.closeModal(); - - toast.error('Gagal reject data transfer ke laying!'); - } - - setIsRejectLoading(false); - }; - - // flock source - const isFlockSourceKandangsRepeaterInputError = ( - column: keyof TransferToLayingFormValues['flockSourceKandangs'][0], - idx: number - ) => { - return ( - formik.touched.flockSourceKandangs?.[idx]?.[column] && - Boolean( - formik.errors.flockSourceKandangs?.[idx] instanceof Object && - formik.errors.flockSourceKandangs?.[idx]?.[column] - ) - ); - }; - - const flockSourceKandangsRepeaterInputErrorMessage = ( - column: keyof TransferToLayingFormValues['flockSourceKandangs'][0], - idx: number - ) => { - return ( - formik.errors.flockSourceKandangs?.[idx] as Record - )?.[column]; - }; - - const { - setInputValue: setFlockSourceInputValue, - options: flockSourceOptions, - isLoadingOptions: isLoadingFlockSourceOptions, - rawData: flockSources, - loadMore: loadMoreFlockSource, - hasMore: hasMoreFlockSource, - } = useSelect( - '/production/project-flocks', - 'id', - 'flock_name', - 'search', - { - category: 'GROWING', - } - ); - - const flockSourceChangeHandler = async ( - val: OptionType | OptionType[] | null - ) => { - // Get flock source data for total quantity and kandang - const flockSource = - isResponseSuccess(flockSources) && val !== null - ? flockSources.data.find( - (item) => item.id === (val as OptionType).value - ) - : undefined; - - // Set total quantity and kandangs - if (flockSource) { - const mappedFlockKandangsAvailableQty = - await TransferToLayingApi.getMappedFlockKandangsAvailability( - flockSource.id - ); - - const formattedKandangs = flockSource.kandangs.map((item) => ({ - kandang: { - value: item.project_flock_kandang_id, - label: item.name, - }, - quantity: '', - maxQuantity: - (mappedFlockKandangsAvailableQty && - mappedFlockKandangsAvailableQty[item.project_flock_kandang_id] - .available_qty) ?? - 0, - })); - - let maxTotalQuantity = 0; - // flockSource.kandangs.forEach((item) => { - // maxTotalQuantity += item.capacity; - // }); - formattedKandangs.forEach((item) => { - maxTotalQuantity += item.maxQuantity; - }); - - formik.setFieldValue('totalQuantity', ''); - formik.setFieldValue('maxTotalQuantity', maxTotalQuantity); - formik.setFieldValue('flockSourceKandangs', formattedKandangs); - } else { - formik.setFieldValue('totalQuantity', undefined); - formik.setFieldValue('flockSourceKandangs', undefined); - formik.setFieldValue('reason', ''); - } - - formik.setFieldTouched('flockSource', true); - formik.setFieldValue('flockSource', val); - }; - - // flock destination - const isFlockDestinationKandangsRepeaterInputError = ( - column: keyof TransferToLayingFormValues['flockDestinationKandangs'][0], - idx: number - ) => { - return ( - formik.touched.flockDestinationKandangs?.[idx]?.[column] && - Boolean( - formik.errors.flockDestinationKandangs?.[idx] instanceof Object && - formik.errors.flockDestinationKandangs?.[idx]?.[column] - ) - ); - }; - - const flockDestinationKandangsRepeaterInputErrorMessage = ( - column: keyof TransferToLayingFormValues['flockDestinationKandangs'][0], - idx: number - ) => { - return ( - formik.errors.flockDestinationKandangs?.[idx] as Record - )?.[column]; - }; - - const { - setInputValue: setFlockDestinationInputValue, - options: flockDestinationOptions, - isLoadingOptions: isLoadingFlockDestinationOptions, - rawData: flockDestinations, - loadMore: loadMoreFlockDestination, - hasMore: hasMoreFlockDestination, - } = useSelect( - '/production/project-flocks', - 'id', - 'flock_name', - 'search', - { - category: 'LAYING', - } - ); - - const flockDestinationChangeHandler = ( - val: OptionType | OptionType[] | null - ) => { - // Get flock destination data for total quantity and kandang - const flockDestination = - isResponseSuccess(flockDestinations) && val !== null - ? flockDestinations.data.find( - (item) => item.id === (val as OptionType).value - ) - : undefined; - - // Set total quantity and kandangs - if (flockDestination) { - const formattedKandangs = flockDestination.kandangs.map((item) => ({ - kandang: { - value: item.project_flock_kandang_id, - label: item.name, - }, - quantity: '', - - // TODO: integrate this later to real kandang capacity API - // maxQuantity: item.capacity ?? 0, - maxQuantity: item.capacity ?? Infinity, - })); - - formik.setFieldValue('flockDestinationKandangs', formattedKandangs); - } - - formik.setFieldTouched('flockDestination', true); - formik.setFieldValue('flockDestination', val); - }; - - const isShowApproveRejectButton = - initialValues && - initialValues?.approval?.step_number === 1 && - initialValues?.approval.action !== 'REJECTED'; - - const isShowDeleteButton = - initialValues && - initialValues?.approval.action !== 'REJECTED' && - initialValues?.approval.action !== 'APPROVED'; - - const isShowEditButton = isShowDeleteButton; - - useEffect(() => { - const getFilledInitialValues = async () => { - if (initialValues) { - const filledInitialValues = - await getFilledTransferToLayingFormInitialValues(initialValues); - - setFormikInitialValues(filledInitialValues); - } - }; - - getFilledInitialValues(); - }, [initialValues, setFormikInitialValues]); - - useEffect(() => { - formikSetValues(formikInitialValues); - }, [formikSetValues, formikInitialValues]); - - useEffect(() => { - // calculate total quantity if kandangs quantity change - if (flockSourceKandangsValue && flockSourceKandangsValue.length > 0) { - let newTotalQuantity = 0; - - flockSourceKandangsValue.forEach((item) => { - newTotalQuantity += parseFloat(item.quantity as string); - }); - - formik.setFieldValue('totalQuantity', newTotalQuantity); - formik.validateField('totalQuantity'); - } - }, [formikSetValues, flockSourceKandangsValue]); - - useEffect(() => { - // calculate total quantity if kandangs quantity change - if ( - flockDestinationKandangsValue && - flockDestinationKandangsValue.length > 0 - ) { - let destinationKandangsTotalQuantity = 0; - - flockDestinationKandangsValue.forEach((item) => { - destinationKandangsTotalQuantity += parseFloat(item.quantity as string); - }); - - if ( - destinationKandangsTotalQuantity > parseFloat(String(totalQuantity)) - ) { - } - } - }, [formikSetValues, flockDestinationKandangsValue]); - - return ( - <> -
-
- - -

- {type === 'add' && 'Tambah Transfer ke Laying'} - {type === 'edit' && 'Edit Transfer ke Laying'} - {type === 'detail' && 'Detail Transfer ke Laying'} -

-
- - {type === 'detail' && - initialValues && - !isLoadingApprovalHistory && - isResponseSuccess(approvalHistory) && ( -
- -
- )} - -
- {type === 'detail' && ( - <> - {isShowApproveRejectButton && ( -
- - - - - - - -
- )} - - )} -
- -
-
- - -
- - - -
- - - -
-
- - - - - - - - - - {(!formik.values.flockSourceKandangs || - formik.values.flockSourceKandangs.length === 0) && ( - - - - )} - - {formik.values.flockSourceKandangs && - formik.values.flockSourceKandangs.map((kandang, idx) => ( - - - - - - ))} - -
Kandang Flock AsalKuantitas
-

- Pilih flock asal terlebih dahulu! -

-
- - - -
-
- -
- - - - - - - - - - {(!formik.values.flockDestinationKandangs || - formik.values.flockDestinationKandangs.length === 0) && ( - - - - )} - - {formik.values.flockDestinationKandangs && - formik.values.flockDestinationKandangs.map( - (kandang, idx) => ( - - - - - - ) - )} - -
Kandang Flock TujuanKuantitas
-

- Pilih flock tujuan terlebih dahulu! -

-
- - - -
-
-
- -