diff --git a/src/app/inventory/adjustment/page.tsx b/src/app/inventory/adjustment/page.tsx index 518fd0bf..44782aa7 100644 --- a/src/app/inventory/adjustment/page.tsx +++ b/src/app/inventory/adjustment/page.tsx @@ -2,7 +2,7 @@ import InventoryAdjustmentTable from '@/components/pages/inventory/adjustment/In const InventoryAdjustment = () => { return ( -
+
); diff --git a/src/components/pages/inventory/adjustment/InventoryAdjustmentTable.tsx b/src/components/pages/inventory/adjustment/InventoryAdjustmentTable.tsx index 1bd47caf..38c9e76a 100644 --- a/src/components/pages/inventory/adjustment/InventoryAdjustmentTable.tsx +++ b/src/components/pages/inventory/adjustment/InventoryAdjustmentTable.tsx @@ -1,20 +1,18 @@ 'use client'; -import Badge from '@/components/Badge'; +import { useCallback, useEffect, useMemo, useState } from 'react'; +import useSWR from 'swr'; +import { Icon } from '@iconify/react'; +import { ColumnDef, ColumnSort, SortingState } from '@tanstack/react-table'; import Button from '@/components/Button'; -import SelectInput, { OptionType } from '@/components/input/SelectInput'; import Table from '@/components/Table'; import RequirePermission from '@/components/helper/RequirePermission'; -import { ROWS_OPTIONS } from '@/config/constant'; import { isResponseSuccess } from '@/lib/api-helper'; -import { cn } from '@/lib/helper'; +import { cn, formatNumber, formatDate } from '@/lib/helper'; import { InventoryAdjustmentApi } from '@/services/api/inventory'; import { useTableFilter } from '@/services/hooks/useTableFilter'; import { InventoryAdjustment } from '@/types/api/inventory/adjustment'; -import { Icon } from '@iconify/react'; -import { ColumnDef, ColumnSort, SortingState } from '@tanstack/react-table'; -import { useCallback, useEffect, useState } from 'react'; -import useSWR from 'swr'; +import StatusBadge from '@/components/helper/StatusBadge'; const InventoryAdjustmentTable = () => { const { @@ -41,80 +39,74 @@ const InventoryAdjustmentTable = () => { }, }); - // Fetch Data const { data: inventoryAdjustments, isLoading } = useSWR( `${InventoryAdjustmentApi.basePath}${getTableFilterQueryString()}`, InventoryAdjustmentApi.getAllFetcher ); - // State const [sorting, setSorting] = useState([]); - // Columns - const inventoryAdjustmentsColumns: ColumnDef[] = [ - { - header: '#', - cell: (props) => - tableFilterState.pageSize * (tableFilterState.page - 1) + - props.row.index + - 1, - }, - { - id: 'product_name', - header: 'Nama Produk', - accessorFn: (row) => row.product_warehouse?.product?.name ?? '-', - }, - { - id: 'warehouse_name', - header: 'Gudang', - accessorFn: (row) => row.product_warehouse?.warehouse?.name ?? '-', - }, - { - id: 'created_at', - header: 'Tanggal', - accessorFn: (row) => - new Date(row.created_at).toLocaleDateString('id-ID', { - day: '2-digit', - month: 'short', - year: 'numeric', - }), - }, - { - id: 'quantity', - header: 'Kuantitas', - accessorFn: (row) => formatNumber(String(row.increase + row.decrease)), - }, - { - id: 'transaction_type', - header: 'Tipe Transaksi', - accessorFn: (row) => { - if (row.increase > 0) return 'Peningkatan'; - if (row.decrease > 0) return 'Penurunan'; - return '-'; + const inventoryAdjustmentsColumns: ColumnDef[] = useMemo( + () => [ + { + header: 'No', + cell: (props) => + tableFilterState.pageSize * (tableFilterState.page - 1) + + props.row.index + + 1, }, - cell: (props) => { - const type = props.row.original.increase; - const label = type > 0 ? 'Peningkatan' : type <= 0 ? 'Penurunan' : '-'; - - return ( - 0 ? 'success' : 'error'}> - {label} - - ); + { + id: 'product_name', + header: 'Nama Produk', + accessorFn: (row) => row.product_warehouse?.product?.name ?? '-', }, - }, - { - id: 'created_by', - header: 'Oleh', - accessorFn: (row) => row.created_user?.name ?? '-', - }, - ]; + { + id: 'warehouse_name', + header: 'Gudang', + accessorFn: (row) => row.product_warehouse?.warehouse?.name ?? '-', + }, + { + id: 'created_at', + header: 'Tanggal', + accessorFn: (row) => + row.created_at ? formatDate(row.created_at, 'DD MMM YYYY') : '-', + }, + { + id: 'quantity', + header: 'Kuantitas', + cell: (props) => { + const value = + props.row.original.increase + props.row.original.decrease; + return
{formatNumber(value)}
; + }, + }, + { + id: 'transaction_type', + header: 'Tipe Transaksi', + cell: (props) => { + const type = props.row.original.increase; + const label = + type > 0 ? 'Peningkatan' : type <= 0 ? 'Penurunan' : '-'; - // Handler - const pageSizeChangeHandler = (val: OptionType | OptionType[] | null) => { - const newVal = val as OptionType; - setPageSize(newVal.value as number); - }; + return ( + 0 ? 'success' : type <= 0 ? 'error' : 'neutral'} + text={label} + className={{ + badge: 'whitespace-nowrap', + }} + /> + ); + }, + }, + { + id: 'created_by', + header: 'Oleh', + accessorFn: (row) => row.created_user?.name ?? '-', + }, + ], + [tableFilterState.pageSize, tableFilterState.page] + ); const updateSortingFilter = useCallback( ( @@ -130,7 +122,6 @@ const InventoryAdjustmentTable = () => { [updateFilter] ); - // Effect useEffect(() => { const productCategorySortFilter = sorting.find( (sortItem) => sortItem.id === 'productCategory' @@ -149,89 +140,60 @@ const InventoryAdjustmentTable = () => { updateSortingFilter('stockSort', stockSortFilter); }, [sorting, updateSortingFilter]); - // Utils Function - const formatNumber = (value: string) => { - const numericValue = value.replace(/[^0-9.]/g, ''); - const [integer, decimal] = numericValue.split('.'); - const formattedInteger = integer.replace(/\B(?=(\d{3})+(?!\d))/g, ','); - return decimal ? `${formattedInteger}.${decimal}` : formattedInteger; - }; - - // Render return ( - <> -
-
-
-
- - - -
- -
- -
-
- - - data={ - isResponseSuccess(inventoryAdjustments) - ? inventoryAdjustments?.data - : [] - } - columns={inventoryAdjustmentsColumns} - pageSize={tableFilterState.pageSize} - page={ - isResponseSuccess(inventoryAdjustments) - ? inventoryAdjustments?.meta?.page - : 0 - } - totalItems={ - isResponseSuccess(inventoryAdjustments) - ? inventoryAdjustments?.meta?.total_results - : 0 - } - onPageChange={setPage} - isLoading={isLoading} - sorting={sorting} - setSorting={setSorting} - className={{ - containerClassName: cn({ - 'mb-20': - isResponseSuccess(inventoryAdjustments) && - inventoryAdjustments?.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', - }} - /> +
+ {/* Header Section */} +
+
+ + +
- + + {/* Table Section */} +
+ + data={ + isResponseSuccess(inventoryAdjustments) + ? inventoryAdjustments?.data + : [] + } + columns={inventoryAdjustmentsColumns} + pageSize={tableFilterState.pageSize} + page={ + isResponseSuccess(inventoryAdjustments) + ? inventoryAdjustments?.meta?.page + : 0 + } + totalItems={ + isResponseSuccess(inventoryAdjustments) + ? inventoryAdjustments?.meta?.total_results + : 0 + } + onPageChange={setPage} + onPageSizeChange={setPageSize} + isLoading={isLoading} + sorting={sorting} + setSorting={setSorting} + className={{ + containerClassName: cn('p-3 mb-0', { + 'w-full': + isResponseSuccess(inventoryAdjustments) && + inventoryAdjustments?.data?.length === 0, + }), + headerColumnClassName: 'text-nowrap', + }} + /> +
+
); };