diff --git a/src/app/production/project-flock/chickin/add/kandang/page.tsx b/src/app/production/project-flock/chickin/add/kandang/page.tsx index 56dfc2db..0805f010 100644 --- a/src/app/production/project-flock/chickin/add/kandang/page.tsx +++ b/src/app/production/project-flock/chickin/add/kandang/page.tsx @@ -12,9 +12,12 @@ export default function AddChickinKandang() { const projectFlockId = searchParams.get('projectFlockId'); const router = useRouter(); - const { data: projectFlockKandang, isLoading: isLoading } = useSWR( - projectFlockKandangId, - (id: number) => ProjectFlockKandangApi.getSingle(id) + const { + data: projectFlockKandang, + isLoading: isLoading, + mutate: refreshProjectFlockKandang, + } = useSWR(projectFlockKandangId, (id: number) => + ProjectFlockKandangApi.getSingle(id) ); if (!projectFlockKandangId) { @@ -31,6 +34,10 @@ export default function AddChickinKandang() { return; } + const handleAfterSubmit = () => { + refreshProjectFlockKandang(); + }; + return ( <>
@@ -38,7 +45,10 @@ export default function AddChickinKandang() { {!isLoading && isResponseSuccess(projectFlockKandang) && projectFlockId && ( - + )}
diff --git a/src/app/production/project-flock/chickin/detail/page.tsx b/src/app/production/project-flock/chickin/detail/page.tsx index da0c8466..daea0f0a 100644 --- a/src/app/production/project-flock/chickin/detail/page.tsx +++ b/src/app/production/project-flock/chickin/detail/page.tsx @@ -6,7 +6,7 @@ import Modal, { useModal } from '@/components/Modal'; import ConfirmationModal from '@/components/modal/ConfirmationModal'; import ChickinForm from '@/components/pages/production/chickin/form/ChickinForm'; import { isResponseError, isResponseSuccess } from '@/lib/api-helper'; -import { ChickinApi } from '@/services/api/production'; +import { ChickinApi } from '@/services/api/production/chickin'; import { BaseApiResponse } from '@/types/api/api-general'; import { Chickin, diff --git a/src/app/production/project-flock/detail/page.tsx b/src/app/production/project-flock/detail/page.tsx index 811aed54..e64005d4 100644 --- a/src/app/production/project-flock/detail/page.tsx +++ b/src/app/production/project-flock/detail/page.tsx @@ -57,7 +57,7 @@ const ProjectFlockDetail = () => { flock: flock.data.find( (flock) => flock.name == - projectFlock.data.flock_name + projectFlock?.data?.flock_name .trim() .split(/\s+/) .slice(0, -1) diff --git a/src/components/pages/production/chickin/ChickinTable.tsx b/src/components/pages/production/chickin/ChickinTable.tsx index db5265c8..d76115c5 100644 --- a/src/components/pages/production/chickin/ChickinTable.tsx +++ b/src/components/pages/production/chickin/ChickinTable.tsx @@ -13,7 +13,7 @@ import { TableRowSizeSelector } from '@/components/table/TableRowSizeSelector'; import { ROWS_OPTIONS } from '@/config/constant'; import { isResponseSuccess } from '@/lib/api-helper'; import { cn, formatNumber } from '@/lib/helper'; -import { ChickinApi } from '@/services/api/production'; +import { ChickinApi } from '@/services/api/production/chickin'; import { useTableFilter } from '@/services/hooks/useTableFilter'; import { Chickin } from '@/types/api/production/chickin'; import { Icon } from '@iconify/react'; diff --git a/src/components/pages/production/chickin/form/ChickinForm.tsx b/src/components/pages/production/chickin/form/ChickinForm.tsx index af9ccd66..f007a1cf 100644 --- a/src/components/pages/production/chickin/form/ChickinForm.tsx +++ b/src/components/pages/production/chickin/form/ChickinForm.tsx @@ -1,33 +1,15 @@ 'use client'; -import Button from '@/components/Button'; import Card from '@/components/Card'; import { FormHeader } from '@/components/helper/form/FormHeader'; -import DateInput from '@/components/input/DateInput'; -import FileInput from '@/components/input/FileInput'; -import NumberInput from '@/components/input/NumberInput'; -import SelectInput, { OptionType } from '@/components/input/SelectInput'; -import TextInput from '@/components/input/TextInput'; import Table from '@/components/Table'; import { formatNumber } from '@/lib/helper'; import { Kandang } from '@/types/api/master-data/kandang'; -import { - AvailableQty, - ProjectFlockKandang, -} from '@/types/api/production/project-flock-kandang'; -import { useFormik } from 'formik'; -import { - ChickinFormValues, - ChickinRequestFormValues, - ChickinSchema, -} from './ChickinForm.schema'; -import { useCallback, useEffect, useState } from 'react'; -import { useRouter } from 'next/navigation'; -import { CreateChickinPayload } from '@/types/api/production/chickin'; -import { ChickinApi } from '@/services/api/production'; -import { isResponseError } from '@/lib/api-helper'; -import toast from 'react-hot-toast'; -import { flushSync } from 'react-dom'; +import { ProjectFlockKandang } from '@/types/api/production/project-flock-kandang'; +import Tabs from '@/components/Tabs'; +import ChickinFormView from './tabs/ChickinFormView'; +import ChickinLogsView from './tabs/ChickLogsView'; +import { useState } from 'react'; const ChickinFormKandang = ({ formType = 'add', initialValues, @@ -37,91 +19,13 @@ const ChickinFormKandang = ({ initialValues: ProjectFlockKandang; afterSubmit?: () => void; }) => { - const router = useRouter(); - const [chickinErrorMessage, setChickinErrorMessage] = useState(''); + const [activeTabId, setActiveTabId] = useState('formChickIn'); - const createChickin = useCallback( - async (payload: CreateChickinPayload) => { - const createChickinRes = await ChickinApi.create(payload); - if (isResponseError(createChickinRes)) { - setChickinErrorMessage(createChickinRes.message); - return; - } - - toast.success(createChickinRes?.message as string); - router.push( - `/production/project-flock/chickin/add?projectFlockId=${initialValues?.project_flock?.id}` - ); - }, - [router] - ); - - const handleReset = async () => { - flushSync(() => { - formik.resetForm({ - values: { - project_flock_kandang_id: initialValues?.id, - chickin_requests: initialValues?.available_qtys - ? initialValues.available_qtys.map((availableQty) => ({ - chick_in_date: '', - product_warehouse_id: availableQty.product_warehouse.id, - available_qty: availableQty.available_qty, - note: `Chickin project-flock-kandang-${initialValues?.id} product-warehouse-${availableQty.product_warehouse.id}`, - })) - : [], - }, - }); - }); - - formik.setTouched({ - chickin_requests: initialValues?.available_qtys?.map(() => ({ - chick_in_date: true, - })), - }); - - const errors = await formik.validateForm(); - formik.setErrors(errors); + const afterSubmitFormChickin = () => { + setActiveTabId('logsChickIn'); + afterSubmit && afterSubmit(); }; - const formik = useFormik({ - enableReinitialize: true, - validationSchema: ChickinSchema, - initialValues: { - project_flock_kandang_id: initialValues?.id, - chickin_requests: initialValues?.available_qtys - ? initialValues.available_qtys.map((availableQty) => ({ - chick_in_date: '', - product_warehouse_id: availableQty.product_warehouse.id, - available_qty: availableQty.available_qty, - note: `Chickin project-flock-kandang-${initialValues?.id} product-warehouse-${availableQty.product_warehouse.id}`, - })) - : [], - }, - onSubmit: (values) => { - setChickinErrorMessage(''); - createChickin(values as CreateChickinPayload); - if (afterSubmit) { - afterSubmit(); - } - }, - }); - - const { setValues: formikSetValues } = formik; - - useEffect(() => { - formikSetValues({ - project_flock_kandang_id: initialValues?.id, - chickin_requests: initialValues?.available_qtys - ? initialValues.available_qtys.map((availableQty) => ({ - chick_in_date: '', - product_warehouse_id: availableQty.product_warehouse.id, - available_qty: availableQty.available_qty, - note: `Chickin project-flock-kandang-${initialValues?.id} product-warehouse-${availableQty.product_warehouse.id}`, - })) - : [], - }); - }, [formikSetValues, initialValues]); - return (
-
{ - handleReset(); + - + emptyContent={ +
+ + Informasi Kandang belum tersedia... + +
+ } + data={[initialValues?.kandang]} + columns={[ + { + header: 'Area', + accessorFn: () => initialValues?.project_flock?.area.name || '-', + }, + { + header: 'Lokasi', + accessorFn: () => + initialValues?.project_flock?.location.name || '-', + }, + { + header: 'Flock', + accessorFn: () => initialValues?.project_flock?.flock_name || '-', + }, + { + header: 'Kandang', + accessorFn: (row) => row?.name || '-', + }, + { + header: 'Kapasitas', + accessorFn: (row) => + (row?.capacity && formatNumber(row?.capacity)) || '-', + }, + { + header: 'Penanggung Jawab', + accessorFn: (row) => row?.pic?.name || '-', + }, + ]} className={{ - wrapper: 'w-full bg-white mt-4', + tableWrapperClassName: 'overflow-x-auto min-h-full!', + tableClassName: 'font-inter w-full table-auto min-h-full!', + headerRowClassName: 'border-b border-b-gray-200', + headerColumnClassName: + 'px-6 py-3 text-xs font-semibold text-gray-500 last:flex last:flex-row last:justify-end', + bodyRowClassName: 'border-b border-b-gray-200', + bodyColumnClassName: + 'px-6 py-3 last:flex last:flex-row last:justify-end', + paginationClassName: 'hidden', }} - > - - emptyContent={ -
- - Informasi Kandang belum tersedia... - -
- } - data={[initialValues?.kandang]} - columns={[ - { - header: 'Area', - accessorFn: () => - initialValues?.project_flock?.area.name || '-', - }, - { - header: 'Lokasi', - accessorFn: () => - initialValues?.project_flock?.location.name || '-', - }, - { - header: 'Flock', - accessorFn: () => - initialValues?.project_flock?.flock_name || '-', - }, - { - header: 'Kandang', - accessorFn: (row) => row?.name || '-', - }, - { - header: 'Kapasitas', - accessorFn: (row) => - (row?.capacity && formatNumber(row?.capacity)) || '-', - }, - { - header: 'Penanggung Jawab', - accessorFn: (row) => row?.pic?.name || '-', - }, - ]} - className={{ - tableWrapperClassName: 'overflow-x-auto min-h-full!', - tableClassName: 'font-inter w-full table-auto min-h-full!', - headerRowClassName: 'border-b border-b-gray-200', - headerColumnClassName: - 'px-6 py-3 text-xs font-semibold text-gray-500 last:flex last:flex-row last:justify-end', - bodyRowClassName: 'border-b border-b-gray-200', - bodyColumnClassName: - 'px-6 py-3 last:flex last:flex-row last:justify-end', - paginationClassName: 'hidden', - }} - /> -
- - - data={formik.values.chickin_requests || []} - columns={[ - { - accessorFn: (row) => row.chick_in_date, - header: 'Tanggal Chick In', - cell(props) { - return ( - - ); - }, - }, - { - accessorFn: (row) => row.product_warehouse_id, - header: 'Produk', - cell(props) { - const availableQty = initialValues?.available_qtys?.find( - (availableQty) => - availableQty.product_warehouse.id === - props.row.original.product_warehouse_id - ); - return ( - - ); - }, - }, - { - accessorFn: (row) => row.product_warehouse_id, - header: 'Jumlah (ekor)', - cell(props) { - const availableQty = initialValues?.available_qtys?.find( - (availableQty) => - availableQty.product_warehouse.id === - props.row.original.product_warehouse_id - ); - return ( - - ); - }, - }, - ]} - className={{ - tableWrapperClassName: 'overflow-x-auto min-h-full!', - tableClassName: 'font-inter w-full table-auto min-h-full!', - headerRowClassName: 'border-b border-b-gray-200', - headerColumnClassName: - 'px-2 py-3 text-xs font-semibold text-gray-500 last:flex last:flex-row last:justify-end', - bodyRowClassName: 'border-b border-b-gray-200', - bodyColumnClassName: - 'px-2 py-2 last:flex last:flex-row last:justify-end', - paginationClassName: 'hidden', - }} - emptyContent={ -
- - Isi persediaan DOC untuk kandang belum tersedia... - -
- } - /> -
- {JSON.stringify(formik.values)} -
- - -
- {JSON.stringify(formik.errors)} - + /> +
+ + ), + }, + { + content: ( + + ), + id: 'logsChickIn', + label: 'Riwayat Chick In', + }, + ]} + variant='lifted' + />
); }; diff --git a/src/components/pages/production/project-flock/ProjectFlockTable.tsx b/src/components/pages/production/project-flock/ProjectFlockTable.tsx index fb876acb..4bafac88 100644 --- a/src/components/pages/production/project-flock/ProjectFlockTable.tsx +++ b/src/components/pages/production/project-flock/ProjectFlockTable.tsx @@ -573,7 +573,7 @@ const ProjectFlockTable = () => { )} - {JSON.stringify(formik.errors)} {formType != 'add' && (
diff --git a/src/services/api/production.ts b/src/services/api/production.ts index a69818e6..5a15bb8b 100644 --- a/src/services/api/production.ts +++ b/src/services/api/production.ts @@ -9,11 +9,6 @@ import { Recording, UpdateRecordingPayload, } from '@/types/api/production/recording'; -import { - Chickin, - CreateChickinPayload, - UpdateChickinPayload, -} from '@/types/api/production/chickin'; import { ProjectFlockKandang } from '@/types/api/production/project-flock-kandang'; export const ProjectFlockApi = new BaseApiService< @@ -31,8 +26,3 @@ export const RecordingApi = new BaseApiService< CreateRecordingPayload, UpdateRecordingPayload >('/flock/recordings'); -export const ChickinApi = new BaseApiService< - Chickin, - CreateChickinPayload, - UpdateChickinPayload ->('/production/chickins'); diff --git a/src/types/api/production/project-flock-kandang.d.ts b/src/types/api/production/project-flock-kandang.d.ts index 222096f4..dafd9aa5 100644 --- a/src/types/api/production/project-flock-kandang.d.ts +++ b/src/types/api/production/project-flock-kandang.d.ts @@ -11,6 +11,7 @@ export type BaseProjectFlockKandang = { kandang: Kandang; project_flock: ProjectFlock; available_qtys?: AvailableQty[]; + chickins?: Chickin[]; approval: BaseApproval; }; @@ -21,6 +22,17 @@ export type AvailableQty = { note?: string; }; +export type Chickin = { + id: number; + project_flock_kandang_id: number; + chick_in_date: string; + product_warehouse_id: number; + product_warehouse: ProductWarehouse; + usage_qty: number; + pending_usage_qty: number; + note: string; +}; + export type ProjectFlockKandang = BaseProjectFlockKandang; export type LookupProjectFlockKandangPayload = {