mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-24 15:25:46 +00:00
refactor(FE): Refactor NonstocksTable to use Popover for row actions
This commit is contained in:
@@ -1,13 +1,8 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { ChangeEventHandler, useCallback, useEffect, useState } from 'react';
|
import { ChangeEventHandler, useMemo, useState } from 'react';
|
||||||
import useSWR from 'swr';
|
import useSWR from 'swr';
|
||||||
import {
|
import { CellContext, ColumnDef, SortingState } from '@tanstack/react-table';
|
||||||
CellContext,
|
|
||||||
ColumnDef,
|
|
||||||
ColumnSort,
|
|
||||||
SortingState,
|
|
||||||
} from '@tanstack/react-table';
|
|
||||||
import toast from 'react-hot-toast';
|
import toast from 'react-hot-toast';
|
||||||
|
|
||||||
import { Icon } from '@iconify/react';
|
import { Icon } from '@iconify/react';
|
||||||
@@ -16,71 +11,92 @@ import DebouncedTextInput from '@/components/input/DebouncedTextInput';
|
|||||||
import Button from '@/components/Button';
|
import Button from '@/components/Button';
|
||||||
import { useModal } from '@/components/Modal';
|
import { useModal } from '@/components/Modal';
|
||||||
import ConfirmationModal from '@/components/modal/ConfirmationModal';
|
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 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 { Nonstock } from '@/types/api/master-data/nonstock';
|
||||||
import { NonstockApi } from '@/services/api/master-data';
|
import { NonstockApi } from '@/services/api/master-data';
|
||||||
import { cn } from '@/lib/helper';
|
import { cn } from '@/lib/helper';
|
||||||
import { isResponseError, isResponseSuccess } from '@/lib/api-helper';
|
import { isResponseError, isResponseSuccess } from '@/lib/api-helper';
|
||||||
import { useTableFilter } from '@/services/hooks/useTableFilter';
|
import { useTableFilter } from '@/services/hooks/useTableFilter';
|
||||||
import { ROWS_OPTIONS } from '@/config/constant';
|
|
||||||
|
|
||||||
const RowOptionsMenu = ({
|
const RowOptionsMenu = ({
|
||||||
type = 'dropdown',
|
popoverPosition = 'bottom',
|
||||||
props,
|
props,
|
||||||
deleteClickHandler,
|
deleteClickHandler,
|
||||||
}: {
|
}: {
|
||||||
type: 'dropdown' | 'collapse';
|
popoverPosition: 'bottom' | 'top';
|
||||||
props: CellContext<Nonstock, unknown>;
|
props: CellContext<Nonstock, unknown>;
|
||||||
deleteClickHandler: () => void;
|
deleteClickHandler: () => void;
|
||||||
}) => {
|
}) => {
|
||||||
|
const popoverId = `nonstock#${props.row.original.id}`;
|
||||||
|
const popoverAnchorName = `--anchor-nonstock#${props.row.original.id}`;
|
||||||
|
|
||||||
|
const closePopover = () => {
|
||||||
|
document.getElementById(popoverId)?.hidePopover();
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<RowOptionsMenuWrapper type={type}>
|
<div className='relative'>
|
||||||
<RequirePermission permissions='lti.master.nonstocks.detail'>
|
<PopoverButton
|
||||||
<Button
|
tabIndex={0}
|
||||||
href={`/master-data/nonstock/detail/?nonstockId=${props.row.original.id}`}
|
variant='ghost'
|
||||||
variant='ghost'
|
color='none'
|
||||||
color='primary'
|
popoverTarget={popoverId}
|
||||||
className='justify-start text-sm'
|
anchorName={popoverAnchorName}
|
||||||
>
|
>
|
||||||
<Icon icon='mdi:eye-outline' width={16} height={16} />
|
<Icon icon='material-symbols:more-vert' width={16} height={16} />
|
||||||
Detail
|
</PopoverButton>
|
||||||
</Button>
|
|
||||||
</RequirePermission>
|
|
||||||
|
|
||||||
<RequirePermission permissions='lti.master.nonstocks.update'>
|
<PopoverContent
|
||||||
<Button
|
id={popoverId}
|
||||||
href={`/master-data/nonstock/detail/edit/?nonstockId=${props.row.original.id}`}
|
anchorName={popoverAnchorName}
|
||||||
variant='ghost'
|
position={popoverPosition === 'bottom' ? 'bottom-start' : 'left'}
|
||||||
color='warning'
|
className='w-full max-w-40 rounded-xl border border-base-content/5 shadow-sm'
|
||||||
className='justify-start text-sm'
|
>
|
||||||
>
|
<div className='flex flex-col bg-base-100 rounded-xl'>
|
||||||
<Icon icon='material-symbols:edit-outline' width={16} height={16} />
|
<RequirePermission permissions='lti.master.nonstocks.detail'>
|
||||||
Edit
|
<Button
|
||||||
</Button>
|
href={`/master-data/nonstock/detail/?nonstockId=${props.row.original.id}`}
|
||||||
</RequirePermission>
|
variant='ghost'
|
||||||
|
color='none'
|
||||||
<RequirePermission permissions='lti.master.nonstocks.delete'>
|
className='p-3 justify-start text-sm font-semibold w-full'
|
||||||
<Button
|
onClick={closePopover}
|
||||||
onClick={deleteClickHandler}
|
>
|
||||||
variant='ghost'
|
<Icon icon='heroicons:eye' width={20} height={20} />
|
||||||
color='error'
|
Detail
|
||||||
className='justify-start text-sm text-error focus-visible:text-error-content hover:text-error-content'
|
</Button>
|
||||||
>
|
</RequirePermission>
|
||||||
<Icon
|
<RequirePermission permissions='lti.master.nonstocks.update'>
|
||||||
icon='material-symbols:delete-outline-rounded'
|
<Button
|
||||||
width={16}
|
href={`/master-data/nonstock/detail/edit/?nonstockId=${props.row.original.id}`}
|
||||||
height={16}
|
variant='ghost'
|
||||||
className='justify-start text-sm'
|
color='none'
|
||||||
/>
|
className='p-3 justify-start text-sm font-semibold w-full'
|
||||||
Delete
|
onClick={closePopover}
|
||||||
</Button>
|
>
|
||||||
</RequirePermission>
|
<Icon icon='heroicons:pencil-square' width={20} height={20} />
|
||||||
</RowOptionsMenuWrapper>
|
Edit
|
||||||
|
</Button>
|
||||||
|
</RequirePermission>
|
||||||
|
<RequirePermission permissions='lti.master.nonstocks.delete'>
|
||||||
|
<Button
|
||||||
|
onClick={() => {
|
||||||
|
deleteClickHandler();
|
||||||
|
closePopover();
|
||||||
|
}}
|
||||||
|
variant='ghost'
|
||||||
|
color='none'
|
||||||
|
className='p-3 justify-start text-sm font-semibold w-full text-error hover:text-error'
|
||||||
|
>
|
||||||
|
<Icon icon='heroicons:trash' width={20} height={20} />
|
||||||
|
Delete
|
||||||
|
</Button>
|
||||||
|
</RequirePermission>
|
||||||
|
</div>
|
||||||
|
</PopoverContent>
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -92,16 +108,17 @@ const NonstocksTable = () => {
|
|||||||
setPageSize,
|
setPageSize,
|
||||||
toQueryString: getTableFilterQueryString,
|
toQueryString: getTableFilterQueryString,
|
||||||
} = useTableFilter({
|
} = useTableFilter({
|
||||||
initial: { search: '', nameSort: '', locationSort: '', picSort: '' },
|
initial: {
|
||||||
|
search: '',
|
||||||
|
},
|
||||||
paramMap: {
|
paramMap: {
|
||||||
page: 'page',
|
page: 'page',
|
||||||
pageSize: 'limit',
|
pageSize: 'limit',
|
||||||
nameSort: 'sort_name',
|
|
||||||
locationSort: 'sort_location',
|
|
||||||
picSort: ' sort_pic',
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const [sorting, setSorting] = useState<SortingState>([]);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
data: nonstocks,
|
data: nonstocks,
|
||||||
isLoading,
|
isLoading,
|
||||||
@@ -112,88 +129,14 @@ const NonstocksTable = () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const deleteModal = useModal();
|
const deleteModal = useModal();
|
||||||
|
|
||||||
const [selectedNonstock, setSelectedNonstock] = useState<
|
const [selectedNonstock, setSelectedNonstock] = useState<
|
||||||
Nonstock | undefined
|
Nonstock | undefined
|
||||||
>(undefined);
|
>(undefined);
|
||||||
const [isDeleteLoading, setIsDeleteLoading] = useState(false);
|
const [isDeleteLoading, setIsDeleteLoading] = useState(false);
|
||||||
|
|
||||||
const [sorting, setSorting] = useState<SortingState>([]);
|
const searchChangeHandler: ChangeEventHandler<HTMLInputElement> = (e) => {
|
||||||
|
updateFilter('search', e.target.value);
|
||||||
const nonstocksColumns: ColumnDef<Nonstock>[] = [
|
};
|
||||||
{
|
|
||||||
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 && (
|
|
||||||
<RowDropdownOptions isLast2Rows={isLast2Rows}>
|
|
||||||
<RowOptionsMenu
|
|
||||||
type='dropdown'
|
|
||||||
props={props}
|
|
||||||
deleteClickHandler={deleteClickHandler}
|
|
||||||
/>
|
|
||||||
</RowDropdownOptions>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{currentPageSize <= 2 && (
|
|
||||||
<RowCollapseOptions>
|
|
||||||
<RowOptionsMenu
|
|
||||||
type='collapse'
|
|
||||||
props={props}
|
|
||||||
deleteClickHandler={deleteClickHandler}
|
|
||||||
/>
|
|
||||||
</RowCollapseOptions>
|
|
||||||
)}
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
const confirmationModalDeleteClickHandler = async () => {
|
const confirmationModalDeleteClickHandler = async () => {
|
||||||
setIsDeleteLoading(true);
|
setIsDeleteLoading(true);
|
||||||
@@ -215,112 +158,128 @@ const NonstocksTable = () => {
|
|||||||
setIsDeleteLoading(false);
|
setIsDeleteLoading(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
const searchChangeHandler: ChangeEventHandler<HTMLInputElement> = (e) => {
|
const nonstocksColumns: ColumnDef<Nonstock>[] = useMemo(
|
||||||
updateFilter('search', e.target.value);
|
() => [
|
||||||
};
|
{
|
||||||
|
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<Nonstock, unknown>) => {
|
||||||
|
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 isLast2Rows = currentRowRelativeIndex > currentPageSize - 2;
|
||||||
const newVal = val as OptionType;
|
|
||||||
|
|
||||||
setPageSize(newVal.value as number);
|
const deleteClickHandler = () => {
|
||||||
};
|
setSelectedNonstock(props.row.original);
|
||||||
|
deleteModal.openModal();
|
||||||
|
};
|
||||||
|
|
||||||
const updateSortingFilter = useCallback(
|
return (
|
||||||
(
|
<RowOptionsMenu
|
||||||
sortName: Exclude<keyof typeof tableFilterState, 'page' | 'pageSize'>,
|
props={props}
|
||||||
sortFilter: ColumnSort | undefined
|
popoverPosition={isLast2Rows ? 'top' : 'bottom'}
|
||||||
) => {
|
deleteClickHandler={deleteClickHandler}
|
||||||
if (!sortFilter) {
|
/>
|
||||||
updateFilter(sortName, '');
|
);
|
||||||
} else {
|
},
|
||||||
updateFilter(sortName, sortFilter.desc ? 'desc' : 'asc');
|
},
|
||||||
}
|
],
|
||||||
},
|
[tableFilterState.pageSize, tableFilterState.page, deleteModal]
|
||||||
[updateFilter]
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// 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 (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className='w-full p-0 sm:p-4'>
|
<div className='w-full'>
|
||||||
<div className='flex flex-col gap-2 mb-4'>
|
{/* Header Section */}
|
||||||
<div className='w-full flex flex-col sm:flex-row justify-between items-end sm:items-center gap-2'>
|
<div className='w-full p-3 flex flex-row justify-between gap-3 flex-wrap border-b border-base-content/10'>
|
||||||
<div className='w-full flex flex-row'>
|
{/* Action Buttons */}
|
||||||
<RequirePermission permissions='lti.master.nonstocks.create'>
|
<div className='w-fit flex flex-row gap-3 flex-wrap'>
|
||||||
<Button
|
<RequirePermission permissions='lti.master.nonstocks.create'>
|
||||||
href='/master-data/nonstock/add'
|
<Button
|
||||||
variant='outline'
|
href='/master-data/nonstock/add'
|
||||||
color='primary'
|
color='primary'
|
||||||
className='w-full sm:w-fit'
|
className='px-3 py-2.5 w-fit text-sm text-base-100 rounded-lg shadow-sm'
|
||||||
>
|
>
|
||||||
<Icon icon='ic:round-plus' width={24} height={24} />
|
<Icon icon='heroicons:plus' width={20} height={20} />
|
||||||
Tambah
|
Tambah Nonstock
|
||||||
</Button>
|
</Button>
|
||||||
</RequirePermission>
|
</RequirePermission>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{/* Search */}
|
||||||
|
<div className='flex flex-1 flex-row justify-start sm:justify-end items-center gap-3 flex-wrap'>
|
||||||
<DebouncedTextInput
|
<DebouncedTextInput
|
||||||
name='search'
|
name='search'
|
||||||
placeholder='Cari Nonstock'
|
placeholder='Cari Nonstock'
|
||||||
value={tableFilterState.search}
|
value={tableFilterState.search ?? ''}
|
||||||
onChange={searchChangeHandler}
|
onChange={searchChangeHandler}
|
||||||
className={{ wrapper: 'sm:max-w-3xs' }}
|
startAdornment={
|
||||||
/>
|
<Icon
|
||||||
</div>
|
icon='heroicons:magnifying-glass'
|
||||||
|
width={20}
|
||||||
<div className='flex flex-row justify-end'>
|
height={20}
|
||||||
<SelectInput
|
/>
|
||||||
label='Baris'
|
}
|
||||||
options={ROWS_OPTIONS}
|
className={{
|
||||||
value={{
|
wrapper: 'w-full min-w-24 max-w-3xs',
|
||||||
label: String(tableFilterState.pageSize),
|
inputWrapper: 'rounded-xl! shadow-button-soft',
|
||||||
value: tableFilterState.pageSize,
|
input:
|
||||||
|
'placeholder:font-semibold placeholder:text-base-content/50',
|
||||||
}}
|
}}
|
||||||
onChange={pageSizeChangeHandler}
|
|
||||||
className={{ wrapper: 'max-w-28' }}
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Table<Nonstock>
|
{/* Table Section */}
|
||||||
data={isResponseSuccess(nonstocks) ? nonstocks?.data : []}
|
<div className='flex flex-col mb-4'>
|
||||||
columns={nonstocksColumns}
|
<Table<Nonstock>
|
||||||
pageSize={tableFilterState.pageSize}
|
data={isResponseSuccess(nonstocks) ? nonstocks?.data : []}
|
||||||
page={isResponseSuccess(nonstocks) ? nonstocks?.meta?.page : 0}
|
columns={nonstocksColumns}
|
||||||
totalItems={
|
pageSize={tableFilterState.pageSize}
|
||||||
isResponseSuccess(nonstocks) ? nonstocks?.meta?.total_results : 0
|
page={isResponseSuccess(nonstocks) ? nonstocks?.meta?.page : 0}
|
||||||
}
|
totalItems={
|
||||||
onPageChange={setPage}
|
isResponseSuccess(nonstocks) ? nonstocks?.meta?.total_results : 0
|
||||||
isLoading={isLoading}
|
}
|
||||||
sorting={sorting}
|
onPageChange={setPage}
|
||||||
setSorting={setSorting}
|
onPageSizeChange={setPageSize}
|
||||||
className={{
|
isLoading={isLoading}
|
||||||
containerClassName: cn({
|
sorting={sorting}
|
||||||
'mb-20':
|
setSorting={setSorting}
|
||||||
isResponseSuccess(nonstocks) && nonstocks?.data?.length === 0,
|
className={{
|
||||||
}),
|
containerClassName: cn('p-3 mb-0', {
|
||||||
tableWrapperClassName: 'overflow-x-auto min-h-full!',
|
'w-full':
|
||||||
tableClassName: 'font-inter w-full table-auto min-h-full!',
|
isResponseSuccess(nonstocks) && nonstocks?.data?.length === 0,
|
||||||
headerRowClassName: 'border-b border-b-gray-200',
|
}),
|
||||||
headerColumnClassName:
|
headerColumnClassName: 'text-nowrap',
|
||||||
'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:
|
</div>
|
||||||
'px-6 py-3 last:flex last:flex-row last:justify-end',
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<ConfirmationModal
|
<ConfirmationModal
|
||||||
|
|||||||
Reference in New Issue
Block a user