'use client'; import { ChangeEventHandler, useState } from 'react'; import useSWR from 'swr'; import { SortingState, CellContext, ColumnDef } from '@tanstack/react-table'; import Table from '@/components/Table'; import { Icon } from '@iconify/react'; import { Movement } from '@/types/api/inventory/movement'; import { MovementApi } from '@/services/api/inventory'; import { cn } from '@/lib/helper'; import { Product } from '@/types/api/master-data/product'; import { Warehouse } from '@/types/api/master-data/warehouse'; import { isResponseSuccess } from '@/lib/api-helper'; import { useTableFilter } from '@/services/hooks/useTableFilter'; import { ROWS_OPTIONS } from '@/config/constant'; import { OptionType, useSelect } from '@/components/input/SelectInput'; import Button from '@/components/Button'; import DebouncedTextInput from '@/components/input/DebouncedTextInput'; import SelectInput from '@/components/input/SelectInput'; import RowDropdownOptions from '@/components/table/RowDropdownOptions'; import RowCollapseOptions from '@/components/table/RowCollapseOptions'; const RowOptionsMenu = ({ type = 'dropdown', props, }: { type: 'dropdown' | 'collapse'; props: CellContext; }) => { return (
); }; const MovementTable = () => { const { state: tableFilterState, updateFilter, setPage, setPageSize, toQueryString: getTableFilterQueryString, } = useTableFilter({ initial: { search: '', product: '', warehouse: '', }, paramMap: { page: 'page', pageSize: 'limit', product: 'product_id', warehouse: 'warehouse_id', }, }); const [sorting, setSorting] = useState([]); const { setInputValue: setProductInputValue, options: productOptions, isLoadingOptions: isLoadingProductOptions, } = useSelect('/products', 'id', 'name'); const { setInputValue: setWarehouseInputValue, options: warehouseOptions, isLoadingOptions: isLoadingWarehouseOptions, } = useSelect('/warehouses', 'id', 'name'); const [selectedProduct, setSelectedProduct] = useState( null ); const [selectedWarehouse, setSelectedWarehouse] = useState( null ); const { data: movements, isLoading } = useSWR( `${MovementApi.basePath}${getTableFilterQueryString()}`, MovementApi.getAllFetcher ); const searchChangeHandler: ChangeEventHandler = (e) => { updateFilter('search', e.target.value); }; const pageSizeChangeHandler = (val: OptionType | OptionType[] | null) => { const newVal = val as OptionType; setPageSize(newVal.value as number); setPage(1); }; const productChangeHandler = (val: OptionType | OptionType[] | null) => { setSelectedProduct(val as OptionType); updateFilter('product', val ? ((val as OptionType).value as string) : ''); }; const warehouseChangeHandler = (val: OptionType | OptionType[] | null) => { setSelectedWarehouse(val as OptionType); updateFilter('warehouse', val ? ((val as OptionType).value as string) : ''); }; const movementColumns: ColumnDef[] = [ { header: '#', cell: (props) => tableFilterState.pageSize * (tableFilterState.page - 1) + props.row.index + 1, }, { accessorFn: (row) => row.source_warehouse?.name, header: 'Gudang Asal', }, { accessorFn: (row) => row.destination_warehouse?.name, header: 'Gudang Tujuan', }, { accessorKey: 'transfer_reason', header: 'Catatan', }, { accessorKey: 'transfer_date', header: 'Tanggal', cell: (props) => new Date(props.row.original.transfer_date).toLocaleDateString('id-ID'), }, { accessorFn: (row) => { const totalCost = row.deliveries?.reduce( (sum, d) => sum + (d.shipping_cost_total || 0), 0 ); return totalCost?.toLocaleString('id-ID'); }, header: 'Biaya Pengiriman', }, { 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; return ( <> {currentPageSize > 2 && ( )} {currentPageSize <= 2 && ( )} ); }, }, ]; return ( <>
data={isResponseSuccess(movements) ? movements?.data : []} columns={movementColumns} pageSize={tableFilterState.pageSize} page={isResponseSuccess(movements) ? movements?.meta?.page : 0} totalItems={ isResponseSuccess(movements) ? movements?.meta?.total_results : 0 } onPageChange={setPage} isLoading={isLoading} sorting={sorting} setSorting={setSorting} className={{ containerClassName: cn({ 'mb-20': isResponseSuccess(movements) && movements?.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', }} />
); }; export default MovementTable;