'use client'; import { useCallback, useEffect, useMemo, useState } from 'react'; import { usePathname } from 'next/navigation'; import useSWR from 'swr'; import { CellContext, ColumnDef, SortingState } from '@tanstack/react-table'; import toast from 'react-hot-toast'; import { useFormik } from 'formik'; import { Icon } from '@iconify/react'; import Table from '@/components/Table'; import Button from '@/components/Button'; import Modal, { useModal } from '@/components/Modal'; import ConfirmationModal from '@/components/modal/ConfirmationModal'; import RequirePermission from '@/components/helper/RequirePermission'; import PopoverButton from '@/components/popover/PopoverButton'; import PopoverContent from '@/components/popover/PopoverContent'; import ProductionStandardTableSkeleton from '@/components/pages/master-data/production-standard/skeleton/ProductionStandardTableSkeleton'; import { OptionType } from '@/components/input/SelectInput'; import ButtonFilter from '@/components/helper/ButtonFilter'; import { ProductionStandard } from '@/types/api/master-data/production-standard'; import { ProductionStandardApi } from '@/services/api/master-data'; import { isResponseError, isResponseSuccess } from '@/lib/api-helper'; import { useTableFilter } from '@/services/hooks/useTableFilter'; import { useUiStore } from '@/stores/ui/ui.store'; import { ProductionStandardFilterSchema, ProductionStandardFilterType, } from '@/components/pages/master-data/production-standard/filter/ProductionStandardFilter'; import SelectInputRadio from '@/components/input/SelectInputRadio'; const RowOptionsMenu = ({ popoverPosition = 'bottom', props, deleteClickHandler, }: { popoverPosition: 'bottom' | 'top'; props: CellContext; deleteClickHandler: () => void; }) => { const popoverId = `production-standard#${props.row.original.id}`; const popoverAnchorName = `--anchor-production-standard#${props.row.original.id}`; const closePopover = () => { document.getElementById(popoverId)?.hidePopover(); }; return (
); }; const ProductionStandardTable = () => { const { setTableState } = useUiStore(); const pathname = usePathname(); const { state: tableFilterState, updateFilter, setPage, setPageSize, toQueryString: getTableFilterQueryString, } = useTableFilter({ initial: { projectCategoryFilter: '', }, paramMap: { page: 'page', pageSize: 'limit', projectCategoryFilter: 'project_category', }, }); // ===== FILTER MODAL STATE ===== const filterModal = useModal(); // ===== FORMIK SETUP ===== const formik = useFormik({ initialValues: { project_category: null, }, validationSchema: ProductionStandardFilterSchema, onSubmit: (values, { setSubmitting }) => { updateFilter('projectCategoryFilter', values.project_category || ''); filterModal.closeModal(); setSubmitting(false); }, onReset: () => { updateFilter('projectCategoryFilter', ''); }, }); // ===== PROJECT CATEGORY OPTIONS (GROWING or LAYING) ===== const projectCategoryOptions = useMemo( () => [ { value: 'GROWING', label: 'Growing' }, { value: 'LAYING', label: 'Laying' }, ], [] ); // ===== FILTER HANDLERS ===== const handleFilterProjectCategoryChange = useCallback( (val: OptionType | OptionType[] | null) => { const option = val as OptionType | null; const category = option?.value ? String(option.value) : null; formik.setFieldValue('project_category', category); }, [formik] ); // ===== FILTER HELPERS ===== const projectCategoryValue = useMemo(() => { if (!formik.values.project_category) return null; return ( projectCategoryOptions.find( (opt) => opt.value === formik.values.project_category ) || null ); }, [formik.values.project_category, projectCategoryOptions]); // ===== HANDLE FILTER MODAL OPEN ===== const handleFilterModalOpen = () => { filterModal.openModal(); formik.validateForm(); }; useEffect(() => { setTableState('production-standard-table', pathname); }, [pathname, setTableState]); const [sorting, setSorting] = useState([]); const { data: productionStandards, isLoading, mutate: refreshProductionStandards, } = useSWR( `${ProductionStandardApi.basePath}${getTableFilterQueryString()}`, ProductionStandardApi.getAllFetcher ); const deleteModal = useModal(); const [selectedProductionStandard, setSelectedProductionStandard] = useState< ProductionStandard | undefined >(undefined); const [isDeleteLoading, setIsDeleteLoading] = useState(false); const confirmationModalDeleteClickHandler = async () => { setIsDeleteLoading(true); const deleteResponse = await ProductionStandardApi.delete( selectedProductionStandard?.id as number ); if (isResponseError(deleteResponse)) { toast.error(deleteResponse.message); setIsDeleteLoading(false); return; } refreshProductionStandards(); deleteModal.closeModal(); toast.success('Successfully delete Production Standard!'); setIsDeleteLoading(false); }; const productionStandardColumns: ColumnDef[] = useMemo( () => [ { header: 'No', cell: (props) => tableFilterState.pageSize * (tableFilterState.page - 1) + props.row.index + 1, }, { accessorKey: 'name', header: 'Nama', }, { accessorFn: (row) => row.project_category ?? '-', header: 'Kategori', }, { header: 'Aksi', cell: (props: CellContext) => { const currentPageSize = props.table.getPaginationRowModel().rows.length; const currentPageRows = props.table.getPaginationRowModel().flatRows; const currentRowRelativeIndex = currentPageRows.findIndex((r) => r.id === props.row.id) + 1; const isLast2Rows = currentRowRelativeIndex > currentPageSize - 2; const deleteClickHandler = () => { setSelectedProductionStandard(props.row.original); deleteModal.openModal(); }; return ( ); }, }, ], [tableFilterState.pageSize, tableFilterState.page, deleteModal] ); return ( <>
{/* Header Section */}
{/* Action Buttons */}
{/* Filter */}
{/* Table Section */}
{isLoading ? (
) : !isResponseSuccess(productionStandards) || productionStandards.data?.length === 0 ? (
} />
) : ( data={productionStandards.data} columns={productionStandardColumns} pageSize={tableFilterState.pageSize} page={productionStandards?.meta?.page ?? 0} totalItems={productionStandards?.meta?.total_results ?? 0} onPageChange={setPage} onPageSizeChange={setPageSize} isLoading={false} sorting={sorting} setSorting={setSorting} className={{ containerClassName: 'p-3 mb-0', headerColumnClassName: 'text-nowrap', }} /> )}
{/* Filter Modal */} {/* Modal Header */}

Filter Data

{/* Modal Footer */}
); }; export default ProductionStandardTable;