diff --git a/src/app/master-data/product/page.tsx b/src/app/master-data/product/page.tsx index a385d411..5a4aafa9 100644 --- a/src/app/master-data/product/page.tsx +++ b/src/app/master-data/product/page.tsx @@ -1,11 +1,7 @@ import ProductsTable from '@/components/pages/master-data/product/ProductTable'; const Product = () => { - return ( -
- -
- ); + return ; }; export default Product; diff --git a/src/components/pages/master-data/product/ProductTable.tsx b/src/components/pages/master-data/product/ProductTable.tsx index f2398065..9953158d 100644 --- a/src/components/pages/master-data/product/ProductTable.tsx +++ b/src/components/pages/master-data/product/ProductTable.tsx @@ -1,13 +1,8 @@ 'use client'; -import { ChangeEventHandler, useCallback, useEffect, useState } from 'react'; +import { ChangeEventHandler, useMemo, useState } from 'react'; import useSWR from 'swr'; -import { - CellContext, - ColumnDef, - ColumnSort, - SortingState, -} from '@tanstack/react-table'; +import { CellContext, ColumnDef, SortingState } from '@tanstack/react-table'; import toast from 'react-hot-toast'; import { Icon } from '@iconify/react'; @@ -16,69 +11,94 @@ import DebouncedTextInput from '@/components/input/DebouncedTextInput'; import Button from '@/components/Button'; import { useModal } from '@/components/Modal'; import ConfirmationModal from '@/components/modal/ConfirmationModal'; -import SelectInput, { OptionType } from '@/components/input/SelectInput'; -import RowDropdownOptions from '@/components/table/RowDropdownOptions'; -import RowCollapseOptions from '@/components/table/RowCollapseOptions'; -import RowOptionsMenuWrapper from '@/components/table/RowOptionsMenuWrapper'; import RequirePermission from '@/components/helper/RequirePermission'; +import PopoverButton from '@/components/popover/PopoverButton'; +import PopoverContent from '@/components/popover/PopoverContent'; import { Product } from '@/types/api/master-data/product'; import { ProductApi } from '@/services/api/master-data'; import { cn, formatCurrency } from '@/lib/helper'; import { isResponseError, isResponseSuccess } from '@/lib/api-helper'; import { useTableFilter } from '@/services/hooks/useTableFilter'; -import { ROWS_OPTIONS } from '@/config/constant'; const RowOptionsMenu = ({ - type = 'dropdown', + popoverPosition = 'bottom', props, deleteClickHandler, }: { - type: 'dropdown' | 'collapse'; + popoverPosition: 'bottom' | 'top'; props: CellContext; deleteClickHandler: () => void; -}) => ( - - - - - - - - - - - -); +
+ + + + + + + + + +
+ + + ); +}; const ProductsTable = () => { const { @@ -90,21 +110,15 @@ const ProductsTable = () => { } = useTableFilter({ initial: { search: '', - nameSort: '', - skuSort: '', - brandSort: '', - categorySort: '', }, paramMap: { page: 'page', pageSize: 'limit', - nameSort: 'sort_name', - skuSort: 'sort_sku', - brandSort: 'sort_brand', - categorySort: 'sort_category', }, }); + const [sorting, setSorting] = useState([]); + const { data: products, isLoading, @@ -119,124 +133,10 @@ const ProductsTable = () => { undefined ); const [isDeleteLoading, setIsDeleteLoading] = useState(false); - const [sorting, setSorting] = useState([]); - const productsColumns: ColumnDef[] = [ - { - header: '#', - cell: (props) => - tableFilterState.pageSize * (tableFilterState.page - 1) + - props.row.index + - 1, - }, - { - accessorKey: 'name', - header: 'Nama', - }, - { - accessorKey: 'sku', - header: 'SKU', - }, - { - accessorKey: 'brand', - header: 'Merek', - }, - { - accessorKey: 'product_category', - header: 'Kategori', - cell: (props) => props.row.original.product_category?.name ?? '-', - }, - { - accessorKey: 'uom', - header: 'Satuan', - cell: (props) => props.row.original.uom?.name ?? '-', - }, - { - accessorKey: 'product_price', - header: 'Harga Produk', - cell: (props) => - props.row.original.product_price - ? formatCurrency(props.row.original.product_price) - : '-', - }, - { - accessorKey: 'selling_price', - header: 'Harga Jual', - cell: (props) => - props.row.original.selling_price - ? formatCurrency(props.row.original.selling_price) - : '-', - }, - { - accessorKey: 'tax', - header: 'Pajak (%)', - cell: (props) => props.row.original.tax ?? '-', - }, - { - accessorKey: 'expiry_period', - header: 'Kadaluarsa (hari)', - cell: (props) => props.row.original.expiry_period ?? '-', - }, - { - accessorKey: 'suppliers', - header: 'Supplier', - cell: (props) => - props.row.original.suppliers?.map((s) => s.name).join(', ') || '-', - }, - { - accessorKey: 'flag', - header: 'Flag', - cell: (props) => - props.row.original.flag ? props.row.original.flag : '-', - }, - { - accessorKey: 'subs_flags', - header: 'Kategori Flags', - cell: (props) => - props.row.original.sub_flags?.length - ? props.row.original.sub_flags.join(', ') - : '-', - }, - { - 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 = () => { - setSelectedProduct(props.row.original); - deleteModal.openModal(); - }; - - return ( - <> - {currentPageSize > 2 && ( - - - - )} - {currentPageSize <= 2 && ( - - - - )} - - ); - }, - }, - ]; + const searchChangeHandler: ChangeEventHandler = (e) => { + updateFilter('search', e.target.value); + }; const confirmationModalDeleteClickHandler = async () => { setIsDeleteLoading(true); @@ -258,110 +158,175 @@ const ProductsTable = () => { setIsDeleteLoading(false); }; - const searchChangeHandler: ChangeEventHandler = (e) => { - updateFilter('search', e.target.value); - }; + const productsColumns: ColumnDef[] = useMemo( + () => [ + { + header: 'No', + cell: (props) => + tableFilterState.pageSize * (tableFilterState.page - 1) + + props.row.index + + 1, + }, + { + accessorKey: 'name', + header: 'Nama', + }, + { + accessorKey: 'sku', + header: 'SKU', + }, + { + accessorKey: 'brand', + header: 'Merek', + }, + { + accessorFn: (row) => row.product_category?.name ?? '-', + header: 'Kategori', + }, + { + accessorFn: (row) => row.uom?.name ?? '-', + header: 'Satuan', + }, + { + accessorKey: 'product_price', + header: 'Harga Produk', + cell: (props) => + props.row.original.product_price + ? formatCurrency(props.row.original.product_price) + : '-', + }, + { + accessorKey: 'selling_price', + header: 'Harga Jual', + cell: (props) => + props.row.original.selling_price + ? formatCurrency(props.row.original.selling_price) + : '-', + }, + { + accessorKey: 'tax', + header: 'Pajak (%)', + cell: (props) => props.row.original.tax ?? '-', + }, + { + accessorKey: 'expiry_period', + header: 'Kadaluarsa (hari)', + cell: (props) => props.row.original.expiry_period ?? '-', + }, + { + accessorFn: (row) => + row.suppliers?.map((s) => s.name).join(', ') || '-', + header: 'Supplier', + }, + { + accessorKey: 'flag', + header: 'Flag', + cell: (props) => + props.row.original.flag ? props.row.original.flag : '-', + }, + { + accessorFn: (row) => + row.sub_flags?.length ? row.sub_flags.join(', ') : '-', + header: 'Kategori Flags', + }, + { + 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 pageSizeChangeHandler = (val: OptionType | OptionType[] | null) => { - const newVal = val as OptionType; - setPageSize(newVal.value as number); - }; + const isLast2Rows = currentRowRelativeIndex > currentPageSize - 2; - const updateSortingFilter = useCallback( - ( - sortName: Exclude, - sortFilter: ColumnSort | undefined - ) => { - if (!sortFilter) { - updateFilter(sortName, ''); - } else { - updateFilter(sortName, sortFilter.desc ? 'desc' : 'asc'); - } - }, - [updateFilter] + const deleteClickHandler = () => { + setSelectedProduct(props.row.original); + deleteModal.openModal(); + }; + + return ( + + ); + }, + }, + ], + [tableFilterState.pageSize, tableFilterState.page, deleteModal] ); - useEffect(() => { - const nameSortFilter = sorting.find((sortItem) => sortItem.id === 'name'); - const skuSortFilter = sorting.find((sortItem) => sortItem.id === 'sku'); - const brandSortFilter = sorting.find((sortItem) => sortItem.id === 'brand'); - const categorySortFilter = sorting.find( - (sortItem) => sortItem.id === 'product_category' - ); - - updateSortingFilter('nameSort', nameSortFilter); - updateSortingFilter('skuSort', skuSortFilter); - updateSortingFilter('brandSort', brandSortFilter); - updateSortingFilter('categorySort', categorySortFilter); - }, [sorting, updateSortingFilter]); - return ( <> -
-
-
-
- - - -
+
+ {/* Header Section */} +
+ {/* Action Buttons */} +
+ + + +
+ + {/* Search */} +
-
-
- + } + className={{ + wrapper: 'w-full min-w-24 max-w-3xs', + inputWrapper: 'rounded-xl! shadow-button-soft', + input: + 'placeholder:font-semibold placeholder:text-base-content/50', }} - onChange={pageSizeChangeHandler} - className={{ wrapper: 'max-w-28' }} />
- - data={isResponseSuccess(products) ? products?.data : []} - columns={productsColumns} - pageSize={tableFilterState.pageSize} - page={isResponseSuccess(products) ? products?.meta?.page : 0} - totalItems={ - isResponseSuccess(products) ? products?.meta?.total_results : 0 - } - onPageChange={setPage} - isLoading={isLoading} - sorting={sorting} - setSorting={setSorting} - className={{ - containerClassName: cn({ - 'mb-20': - isResponseSuccess(products) && products?.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', - }} - /> + + {/* Table Section */} +
+ + data={isResponseSuccess(products) ? products?.data : []} + columns={productsColumns} + pageSize={tableFilterState.pageSize} + page={isResponseSuccess(products) ? products?.meta?.page : 0} + totalItems={ + isResponseSuccess(products) ? products?.meta?.total_results : 0 + } + onPageChange={setPage} + onPageSizeChange={setPageSize} + isLoading={isLoading} + sorting={sorting} + setSorting={setSorting} + className={{ + containerClassName: cn('p-3 mb-0', { + 'w-full': + isResponseSuccess(products) && products?.data?.length === 0, + }), + headerColumnClassName: 'text-nowrap', + }} + /> +
+