From 7db6ae4077f9231bba33afd1897dd99f0bd7b1dd Mon Sep 17 00:00:00 2001 From: rstubryan Date: Mon, 2 Mar 2026 10:47:17 +0700 Subject: [PATCH] refactor(FE): Refactor NonstocksTable to use Popover for row actions --- .../master-data/nonstock/NonstocksTable.tsx | 409 ++++++++---------- 1 file changed, 184 insertions(+), 225 deletions(-) diff --git a/src/components/pages/master-data/nonstock/NonstocksTable.tsx b/src/components/pages/master-data/nonstock/NonstocksTable.tsx index 6aeb3f99..1a4cb358 100644 --- a/src/components/pages/master-data/nonstock/NonstocksTable.tsx +++ b/src/components/pages/master-data/nonstock/NonstocksTable.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,71 +11,92 @@ 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 { Nonstock } from '@/types/api/master-data/nonstock'; import { NonstockApi } from '@/services/api/master-data'; import { cn } 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 popoverId = `nonstock#${props.row.original.id}`; + const popoverAnchorName = `--anchor-nonstock#${props.row.original.id}`; + + const closePopover = () => { + document.getElementById(popoverId)?.hidePopover(); + }; + return ( - - - - +
+ + + - - - - - - - - + +
+ + + + + + + + + +
+
+
); }; @@ -92,16 +108,17 @@ const NonstocksTable = () => { setPageSize, toQueryString: getTableFilterQueryString, } = useTableFilter({ - initial: { search: '', nameSort: '', locationSort: '', picSort: '' }, + initial: { + search: '', + }, paramMap: { page: 'page', pageSize: 'limit', - nameSort: 'sort_name', - locationSort: 'sort_location', - picSort: ' sort_pic', }, }); + const [sorting, setSorting] = useState([]); + const { data: nonstocks, isLoading, @@ -112,88 +129,14 @@ const NonstocksTable = () => { ); const deleteModal = useModal(); - const [selectedNonstock, setSelectedNonstock] = useState< Nonstock | undefined >(undefined); const [isDeleteLoading, setIsDeleteLoading] = useState(false); - const [sorting, setSorting] = useState([]); - - const nonstocksColumns: ColumnDef[] = [ - { - header: '#', - cell: (props) => - tableFilterState.pageSize * (tableFilterState.page - 1) + - props.row.index + - 1, - }, - { - accessorKey: 'name', - header: 'Nama', - }, - { - accessorKey: 'uom', - header: 'UOM', - cell: (props) => props.row.original.uom.name, - }, - { - accessorKey: 'suppliers', - header: 'Supplier', - cell: (props) => { - const supplierNames = props.row.original.suppliers.map( - (supplier) => supplier.name - ); - - return supplierNames.join(', ') || '-'; - }, - }, - { - accessorKey: 'flags', - header: 'Flag', - cell: (props) => props.row.original.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 = () => { - setSelectedNonstock(props.row.original); - deleteModal.openModal(); - }; - - return ( - <> - {currentPageSize > 2 && ( - - - - )} - - {currentPageSize <= 2 && ( - - - - )} - - ); - }, - }, - ]; + const searchChangeHandler: ChangeEventHandler = (e) => { + updateFilter('search', e.target.value); + }; const confirmationModalDeleteClickHandler = async () => { setIsDeleteLoading(true); @@ -215,112 +158,128 @@ const NonstocksTable = () => { setIsDeleteLoading(false); }; - const searchChangeHandler: ChangeEventHandler = (e) => { - updateFilter('search', e.target.value); - }; + const nonstocksColumns: ColumnDef[] = useMemo( + () => [ + { + header: 'No', + cell: (props) => + tableFilterState.pageSize * (tableFilterState.page - 1) + + props.row.index + + 1, + }, + { + accessorKey: 'name', + header: 'Nama', + }, + { + accessorFn: (row) => row.uom?.name ?? '-', + header: 'UOM', + }, + { + accessorFn: (row) => + row.suppliers?.map((supplier) => supplier.name).join(', ') || '-', + header: 'Supplier', + }, + { + accessorFn: (row) => row.flags?.join(', ') || '-', + header: 'Flag', + }, + { + 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; + const isLast2Rows = currentRowRelativeIndex > currentPageSize - 2; - setPageSize(newVal.value as number); - }; + const deleteClickHandler = () => { + setSelectedNonstock(props.row.original); + deleteModal.openModal(); + }; - const updateSortingFilter = useCallback( - ( - sortName: Exclude, - sortFilter: ColumnSort | undefined - ) => { - if (!sortFilter) { - updateFilter(sortName, ''); - } else { - updateFilter(sortName, sortFilter.desc ? 'desc' : 'asc'); - } - }, - [updateFilter] + return ( + + ); + }, + }, + ], + [tableFilterState.pageSize, tableFilterState.page, deleteModal] ); - // track sorting - useEffect(() => { - const nameSortFilter = sorting.find((sortItem) => sortItem.id === 'name'); - const locationSortFilter = sorting.find( - (sortItem) => sortItem.id === 'location' - ); - const picSortFilter = sorting.find((sortItem) => sortItem.id === 'pic'); - - updateSortingFilter('nameSort', nameSortFilter); - updateSortingFilter('locationSort', locationSortFilter); - updateSortingFilter('picSort', picSortFilter); - }, [sorting]); - 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(nonstocks) ? nonstocks?.data : []} - columns={nonstocksColumns} - pageSize={tableFilterState.pageSize} - page={isResponseSuccess(nonstocks) ? nonstocks?.meta?.page : 0} - totalItems={ - isResponseSuccess(nonstocks) ? nonstocks?.meta?.total_results : 0 - } - onPageChange={setPage} - isLoading={isLoading} - sorting={sorting} - setSorting={setSorting} - className={{ - containerClassName: cn({ - 'mb-20': - isResponseSuccess(nonstocks) && nonstocks?.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(nonstocks) ? nonstocks?.data : []} + columns={nonstocksColumns} + pageSize={tableFilterState.pageSize} + page={isResponseSuccess(nonstocks) ? nonstocks?.meta?.page : 0} + totalItems={ + isResponseSuccess(nonstocks) ? nonstocks?.meta?.total_results : 0 + } + onPageChange={setPage} + onPageSizeChange={setPageSize} + isLoading={isLoading} + sorting={sorting} + setSorting={setSorting} + className={{ + containerClassName: cn('p-3 mb-0', { + 'w-full': + isResponseSuccess(nonstocks) && nonstocks?.data?.length === 0, + }), + headerColumnClassName: 'text-nowrap', + }} + /> +