From 23ee8828f0cdb369acffb0e3a44ef66d495f19bc Mon Sep 17 00:00:00 2001 From: ValdiANS Date: Tue, 30 Dec 2025 22:24:22 +0700 Subject: [PATCH] feat(FE-442): create ProductionResultContent component --- .../ProductionResultContent.tsx | 409 ++++++++++++++++++ 1 file changed, 409 insertions(+) create mode 100644 src/components/pages/report/production-result/ProductionResultContent.tsx diff --git a/src/components/pages/report/production-result/ProductionResultContent.tsx b/src/components/pages/report/production-result/ProductionResultContent.tsx new file mode 100644 index 00000000..6fc8f7ea --- /dev/null +++ b/src/components/pages/report/production-result/ProductionResultContent.tsx @@ -0,0 +1,409 @@ +'use client'; + +import { useState } from 'react'; +import toast from 'react-hot-toast'; + +import { Icon } from '@iconify/react'; +import Button from '@/components/Button'; +import Dropdown from '@/components/dropdown/Dropdown'; +import SelectInput, { + OptionType, + useSelect, +} from '@/components/input/SelectInput'; +import Menu from '@/components/menu/Menu'; +import MenuItem from '@/components/menu/MenuItem'; +import Card from '@/components/Card'; +import ProductionResultProjectFlockKandangTable from '@/components/pages/report/production-result/ProductionResultProjectFlockKandangTable'; + +import { BaseKandang } from '@/types/api/master-data/kandang'; +import { AreaApi, LocationApi } from '@/services/api/master-data'; +import { + ProjectFlockApi, + ProjectFlockKandangApi, +} from '@/services/api/production'; +import { ProjectFlockKandang } from '@/types/api/production/project-flock-kandang'; +import { isResponseError } from '@/lib/api-helper'; +import Pagination from '@/components/Pagination'; + +const ProductionResultContent = () => { + const [projectFlockKandangs, setProjectFlockKandangs] = useState< + ProjectFlockKandang[] | null + >(null); + const [projectFlockKandangMetadata, setProjectFlockKandangMetadata] = + useState< + | { + page: number; + limit: number; + total_pages: number; + total_results: number; + } + | undefined + >(undefined); + + const [page, setPage] = useState(1); + const [pageSize, setPageSize] = useState(10); + + const [isLoadingSearch, setIsLoadingSearch] = useState(false); + + const [isLoadingExportingToExcel, setIsLoadingExportingToExcel] = + useState(false); + + const [selectedArea, setSelectedArea] = useState(null); + const [selectedLocation, setSelectedLocation] = useState( + null + ); + const [selectedProjectFlock, setSelectedProjectFlock] = + useState(null); + const [selectedProjectFlockKandang, setSelectedProjectFlockKandang] = + useState(null); + + const { + setInputValue: setAreaInputValue, + options: areaOptions, + isLoadingOptions: isLoadingAreaOptions, + } = useSelect(AreaApi.basePath, 'id', 'name'); + + const areaChangeHandler = (val: OptionType | OptionType[] | null) => { + setSelectedArea(val as OptionType); + + setSelectedLocation(null); + + setSelectedProjectFlock(null); + + setSelectedProjectFlockKandang(null); + }; + + const { + setInputValue: setLocationInputValue, + options: locationOptions, + isLoadingOptions: isLoadingLocationOptions, + } = useSelect(LocationApi.basePath, 'id', 'name', 'search', { + area_id: selectedArea ? ((selectedArea as OptionType).value as string) : '', + }); + + const locationChangeHandler = (val: OptionType | OptionType[] | null) => { + setSelectedLocation(val as OptionType); + + setSelectedProjectFlock(null); + + setSelectedProjectFlockKandang(null); + }; + + const { + setInputValue: setProjectFlockInputValue, + options: projectFlockOptions, + isLoadingOptions: isLoadingProjectFlockOptions, + } = useSelect( + ProjectFlockApi.basePath, + 'id', + 'flock_name', + 'search', + { + area_id: selectedArea + ? ((selectedArea as OptionType).value as string) + : '', + location_id: selectedLocation + ? ((selectedLocation as OptionType).value as string) + : '', + category: 'LAYING', + } + ); + + const projectFlockChangeHandler = (val: OptionType | OptionType[] | null) => { + setSelectedProjectFlock(val as OptionType); + + setSelectedProjectFlockKandang(null); + }; + + const { + setInputValue: setProjectFlockKandangInputValue, + options: projectFlockKandangOptions, + isLoadingOptions: isLoadingProjectFlockKandangOptions, + } = useSelect( + ProjectFlockKandangApi.basePath, + 'id', + 'kandang.name', + 'search', + { + area_id: selectedArea + ? ((selectedArea as OptionType).value as string) + : '', + location_id: selectedLocation + ? ((selectedLocation as OptionType).value as string) + : '', + project_flock_id: selectedProjectFlock + ? ((selectedProjectFlock as OptionType).value as string) + : '', + } + ); + + const projectFlockKandangChangeHandler = ( + val: OptionType | OptionType[] | null + ) => { + setSelectedProjectFlockKandang(val as OptionType); + }; + + const exportToExcelHandler = async () => { + setIsLoadingExportingToExcel(true); + // TODO: Implement export functionality in API service first if needed + toast.error('Fitur export belum tersedia'); + setIsLoadingExportingToExcel(false); + }; + + const searchHandler = async () => { + setProjectFlockKandangs(null); + setIsLoadingSearch(true); + + try { + if (selectedProjectFlockKandang) { + const projectFlockKandangResponse = + await ProjectFlockKandangApi.getSingle( + selectedProjectFlockKandang?.value as number + ); + + if ( + !projectFlockKandangResponse || + isResponseError(projectFlockKandangResponse) + ) { + throw new Error(); + } + + setProjectFlockKandangs([projectFlockKandangResponse.data]); + setProjectFlockKandangMetadata(projectFlockKandangResponse.meta); + setIsLoadingSearch(false); + return; + } + + const projectFlockKandangsResponse = await ProjectFlockKandangApi.getAll({ + area_id: selectedArea?.value, + project_flock_id: selectedProjectFlock?.value, + }); + + if ( + !projectFlockKandangsResponse || + isResponseError(projectFlockKandangsResponse) + ) { + throw new Error(); + } + + setProjectFlockKandangs(projectFlockKandangsResponse.data); + setProjectFlockKandangMetadata(projectFlockKandangsResponse.meta); + setIsLoadingSearch(false); + } catch (error) { + toast.error('Gagal mencari data! Coba lagi.'); + setProjectFlockKandangs(null); + setProjectFlockKandangMetadata(undefined); + setIsLoadingSearch(false); + } + }; + + const resetHandler = () => { + setProjectFlockKandangs(null); + setSelectedArea(null); + setSelectedLocation(null); + setSelectedProjectFlock(null); + setSelectedProjectFlockKandang(null); + // resetFilter(); + }; + + return ( +
+ +
+

+ Laporan Hasil Produksi +

+
+ + {/* Filters */} +
+
+ + + + + + + +
+ +
+
+ + + + + Export{' '} + + + } + > + + + + +
+
+
+
+ +
+ {isLoadingSearch && ( + + )} + + {!isLoadingSearch && !projectFlockKandangs && ( +

+ Silakan pilih filter dan klik tombol Cari untuk menampilkan data. +

+ )} + + {!isLoadingSearch && projectFlockKandangs?.length === 0 && ( +

+ Tidak ada data kandang project flock yang dapat ditampilkan. +

+ )} + + {!isLoadingSearch && projectFlockKandangs && ( + + {projectFlockKandangs.map((projectFlockKandang) => ( + + ))} + +
+ + setPage((currPage) => + currPage > 1 ? currPage - 1 : currPage + ) + } + onNextPage={() => + setPage((currPage) => + projectFlockKandangMetadata?.total_pages && + currPage < projectFlockKandangMetadata.total_pages + ? currPage + 1 + : currPage + ) + } + onPageChange={(pageNumber) => setPage(pageNumber)} + rowOptions={[10, 20, 50, 100]} + onRowChange={setPageSize} + /> +
+
+ )} +
+
+ ); +}; + +export default ProductionResultContent;