refactor(FE-170): clean up imports and enhance state management in RecordingForm

This commit is contained in:
rstubryan
2025-11-06 10:58:14 +07:00
parent 3d3569bbc0
commit fa42f9b941
@@ -2,19 +2,28 @@
import { useMemo, useState, useEffect, useCallback } from 'react'; import { useMemo, useState, useEffect, useCallback } from 'react';
import { useRouter } from 'next/navigation'; import { useRouter } from 'next/navigation';
import { useFormik } from 'formik'; import { useFormik } from 'formik';
import useSWR from 'swr'; import useSWR from 'swr';
import { Icon } from '@iconify/react'; import { Icon } from '@iconify/react';
import Button from '@/components/Button'; import Button from '@/components/Button';
import Card from '@/components/Card';
import Badge from '@/components/Badge';
import NumberInput from '@/components/input/NumberInput'; import NumberInput from '@/components/input/NumberInput';
import SelectInput, { OptionType } from '@/components/input/SelectInput'; import SelectInput, { OptionType } from '@/components/input/SelectInput';
import CheckboxInput from '@/components/input/CheckboxInput'; import CheckboxInput from '@/components/input/CheckboxInput';
import ConfirmationModal from '@/components/modal/ConfirmationModal'; import ConfirmationModal from '@/components/modal/ConfirmationModal';
import { useModal } from '@/components/Modal';
import { import {
ProjectFlockKandangApi, ProjectFlockKandangApi,
RecordingApi, RecordingApi,
ProjectFlockApi,
} from '@/services/api/production'; } from '@/services/api/production';
import { LocationApi } from '@/services/api/master-data';
import { ProductWarehouseApi } from '@/services/api/inventory';
import { import {
CreateGrowingRecordingPayload, CreateGrowingRecordingPayload,
CreateLayingRecordingPayload, CreateLayingRecordingPayload,
@@ -23,6 +32,10 @@ import {
Recording, Recording,
} from '@/types/api/production/recording'; } from '@/types/api/production/recording';
import { type BaseApiResponse, FormStepStatus } from '@/types/api/api-general'; import { type BaseApiResponse, FormStepStatus } from '@/types/api/api-general';
import { ProjectFlockKandangLookup } from '@/types/api/production/project-flock';
import { ProjectFlockKandang } from '@/types/api/production/project-flock-kandang';
import { Kandang } from '@/types/api/master-data/kandang';
import { import {
RecordingGrowingFormSchema, RecordingGrowingFormSchema,
RecordingLayingFormSchema, RecordingLayingFormSchema,
@@ -33,28 +46,21 @@ import {
UpdateRecordingGrowingFormSchema, UpdateRecordingGrowingFormSchema,
UpdateRecordingLayingFormSchema, UpdateRecordingLayingFormSchema,
} from './RecordingForm.schema'; } from './RecordingForm.schema';
import { ProjectFlockApi } from '@/services/api/production';
import { LocationApi } from '@/services/api/master-data';
import { ProductWarehouseApi } from '@/services/api/inventory';
import { isResponseSuccess, isResponseError } from '@/lib/api-helper'; import { isResponseSuccess, isResponseError } from '@/lib/api-helper';
import { cn, formatDate } from '@/lib/helper'; import { cn, formatDate } from '@/lib/helper';
import { ProjectFlockKandangLookup } from '@/types/api/production/project-flock';
import { ProjectFlockKandang } from '@/types/api/production/project-flock-kandang';
import { useModal } from '@/components/Modal';
import toast from 'react-hot-toast'; import toast from 'react-hot-toast';
import Card from '@/components/Card';
import Badge from '@/components/Badge';
import StepItem from '@/components/steps/StepItem';
import { Kandang } from '@/types/api/master-data/kandang';
interface RecordingFormProps { interface RecordingFormProps {
type?: 'add' | 'edit' | 'detail'; type?: 'add' | 'edit' | 'detail';
initialValues?: Recording; initialValues?: Recording;
} }
const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
// ===== HOOKS & ROUTER =====
const router = useRouter(); const router = useRouter();
// ===== STATE MANAGEMENT =====
const [selectedBodyWeights, setSelectedBodyWeights] = useState<number[]>([]); const [selectedBodyWeights, setSelectedBodyWeights] = useState<number[]>([]);
const [selectedStocks, setSelectedStocks] = useState<number[]>([]); const [selectedStocks, setSelectedStocks] = useState<number[]>([]);
const [selectedDepletions, setSelectedDepletions] = useState<number[]>([]); const [selectedDepletions, setSelectedDepletions] = useState<number[]>([]);
@@ -78,7 +84,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
const [isApproveLoading, setIsApproveLoading] = useState(false); const [isApproveLoading, setIsApproveLoading] = useState(false);
const [isRejectLoading, setIsRejectLoading] = useState(false); const [isRejectLoading, setIsRejectLoading] = useState(false);
const [formSteps, setFormSteps] = useState<FormStepStatus[] | null>(null); const [, setFormSteps] = useState<FormStepStatus[] | null>(null);
const [recordingFormErrorMessage, setRecordingFormErrorMessage] = const [recordingFormErrorMessage, setRecordingFormErrorMessage] =
useState(''); useState('');
const [isDeleteLoading, setIsDeleteLoading] = useState(false); const [isDeleteLoading, setIsDeleteLoading] = useState(false);
@@ -489,6 +495,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
projectFlockKandangLookup?.project_flock?.category === 'LAYING' || projectFlockKandangLookup?.project_flock?.category === 'LAYING' ||
projectFlockKandangDetail?.project_flock?.category === 'LAYING'; projectFlockKandangDetail?.project_flock?.category === 'LAYING';
// ===== FORMIK SETUP =====
const formikInitialValues = useMemo(() => { const formikInitialValues = useMemo(() => {
let baseValues; let baseValues;
if (isLayingCategory) { if (isLayingCategory) {
@@ -1235,6 +1242,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
} }
}, [bodyWeightValues, editingAverageIndex, manuallyEditedRows]); }, [bodyWeightValues, editingAverageIndex, manuallyEditedRows]);
// ===== RENDER =====
return ( return (
<> <>
<section className='w-full'> <section className='w-full'>
@@ -2448,6 +2456,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
</form> </form>
</section> </section>
{/* ===== MODALS ===== */}
{type !== 'add' && ( {type !== 'add' && (
<> <>
<ConfirmationModal <ConfirmationModal