'use client'; import Button from '@/components/Button'; import DebouncedTextInput from '@/components/input/DebouncedTextInput'; import { OptionType } from '@/components/input/SelectInput'; import Modal, { useModal } from '@/components/Modal'; import ConfirmationModal from '@/components/modal/ConfirmationModal'; import Table from '@/components/Table'; import RowCollapseOptions from '@/components/table/RowCollapseOptions'; import RowDropdownOptions from '@/components/table/RowDropdownOptions'; import RowOptionsMenuWrapper from '@/components/table/RowOptionsMenuWrapper'; 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 { useTableFilter } from '@/services/hooks/useTableFilter'; import { Chickin } from '@/types/api/production/chickin'; import { Icon } from '@iconify/react'; import { CellContext, SortingState } from '@tanstack/react-table'; import { useState } from 'react'; import useSWR from 'swr'; import ChickinForm from './form/ChickinForm'; const ChickinTable = () => { const { state: tableFilterState, updateFilter, setPage, setPageSize, toQueryString: getTableFilterQueryString, } = useTableFilter({ initial: { search: '', }, paramMap: { page: 'page', pageSize: 'limit', search: 'search', }, }); const [sorting, setSorting] = useState([]); const [selectedChickin, setSelectedChickin] = useState( undefined ); const [isDeleteLoading, setIsDeleteLoading] = useState(false); const deleteModal = useModal(); const chickinModal = useModal(); // Data Fetching const { data: chickins, isLoading, mutate: refreshChickins, } = useSWR( `${ChickinApi.basePath}${getTableFilterQueryString()}`, ChickinApi.getAllFetcher ); const searchChangeHandler = (event: React.ChangeEvent) => { updateFilter('search', event.target.value); setPage(1); }; const pageSizeChangeHandler = (val: OptionType | OptionType[] | null) => { const newVal = val as OptionType; setPageSize(newVal.value as number); setPage(1); }; const confirmationModalDeleteClickHandler = async () => { setIsDeleteLoading(true); try { await ChickinApi.delete(selectedChickin?.id as number); refreshChickins(); deleteModal.closeModal(); } finally { setIsDeleteLoading(false); } }; return ( <>
data={isResponseSuccess(chickins) ? chickins?.data : []} columns={[ { header: '#', cell: (props) => tableFilterState.pageSize * (tableFilterState.page - 1) + props.row.index + 1, }, { accessorFn: (row) => row.project_flock_kandang?.kandang.name, header: 'Kandang', }, { accessorFn: (row) => row.quantity, header: 'Jumlah Chickin', cell: (props) => { if (props.row.original.quantity) { return formatNumber(props.row.original.quantity); } else { return '-'; } }, }, { accessorFn: (row) => row.chick_in_date, header: 'Tanggal Chickin', cell: (props) => { if (props.row.original.chick_in_date) { return new Date( props.row.original.chick_in_date ).toLocaleDateString('id-ID'); } else { return '-'; } }, }, { accessorFn: (row) => row.note, header: 'Catatan', }, { header: 'Aksi', cell: (props) => { 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 = () => { setSelectedChickin(props.row.original); deleteModal.openModal(); }; const editClickHandler = () => { setSelectedChickin(props.row.original); chickinModal.openModal(); }; return ( <> {currentPageSize > 2 && ( )} {currentPageSize <= 2 && ( )} ); }, }, ]} pageSize={tableFilterState.pageSize} page={isResponseSuccess(chickins) ? chickins?.meta?.page : 0} totalItems={ isResponseSuccess(chickins) ? chickins?.meta?.total_results : 0 } onPageChange={setPage} isLoading={isLoading} sorting={sorting} setSorting={setSorting} className={{ containerClassName: cn({ 'mb-20': isResponseSuccess(chickins) && chickins?.data?.length === 0, }), 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', }} />

Chickin Kandang -{' '} {selectedChickin?.project_flock_kandang && selectedChickin?.project_flock_kandang.kandang?.name}

{ refreshChickins(); chickinModal.closeModal(); }} />
); }; const RowOptionsMenu = ({ type = 'dropdown', props, editClickHandler, deleteClickHandler, }: { type: 'dropdown' | 'collapse'; props: CellContext; editClickHandler: () => void; deleteClickHandler: () => void; }) => { return ( ); }; export default ChickinTable;