mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-24 23:35:45 +00:00
feat(FE-43): create NonstocksTable component
This commit is contained in:
@@ -1,20 +1,31 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { ChangeEventHandler, useState } from 'react';
|
import { ChangeEventHandler, useCallback, useEffect, useState } from 'react';
|
||||||
import useSWR from 'swr';
|
import useSWR from 'swr';
|
||||||
import { CellContext, ColumnDef } from '@tanstack/react-table';
|
import {
|
||||||
|
CellContext,
|
||||||
|
ColumnDef,
|
||||||
|
ColumnSort,
|
||||||
|
SortingState,
|
||||||
|
} from '@tanstack/react-table';
|
||||||
|
import toast from 'react-hot-toast';
|
||||||
|
|
||||||
import { Icon } from '@iconify/react';
|
import { Icon } from '@iconify/react';
|
||||||
import Table from '@/components/Table';
|
import Table from '@/components/Table';
|
||||||
import TextInput from '@/components/input/TextInput';
|
import DebouncedTextInput from '@/components/input/DebouncedTextInput';
|
||||||
import Button from '@/components/Button';
|
import Button from '@/components/Button';
|
||||||
import Collapse from '@/components/Collapse';
|
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 { httpClientFetcher } from '@/services/http/client';
|
import { Nonstock } from '@/types/api/master-data/nonstock';
|
||||||
import { Nonstock, NonstocksResponse } from '@/types/api/master-data/nonstock';
|
import { NonstockApi } from '@/services/api/master-data';
|
||||||
import { cn } from '@/lib/helper';
|
import { cn } from '@/lib/helper';
|
||||||
import { deleteNonstock } from '@/services/api/master-data/nonstock';
|
|
||||||
import { isResponseSuccess } from '@/lib/api-helper';
|
import { isResponseSuccess } from '@/lib/api-helper';
|
||||||
|
import { useTableFilter } from '@/services/hooks/useTableFilter';
|
||||||
|
import { ROWS_OPTIONS } from '@/config/constant';
|
||||||
|
|
||||||
const RowOptionsMenu = ({
|
const RowOptionsMenu = ({
|
||||||
type = 'dropdown',
|
type = 'dropdown',
|
||||||
@@ -23,7 +34,7 @@ const RowOptionsMenu = ({
|
|||||||
}: {
|
}: {
|
||||||
type: 'dropdown' | 'collapse';
|
type: 'dropdown' | 'collapse';
|
||||||
props: CellContext<Nonstock, unknown>;
|
props: CellContext<Nonstock, unknown>;
|
||||||
deleteClickHandler: () => Promise<void>;
|
deleteClickHandler: () => void;
|
||||||
}) => {
|
}) => {
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
@@ -74,78 +85,74 @@ const RowOptionsMenu = ({
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const RowDropdownOptions = ({
|
|
||||||
props,
|
|
||||||
isLast2Rows,
|
|
||||||
deleteClickHandler,
|
|
||||||
}: {
|
|
||||||
props: CellContext<Nonstock, unknown>;
|
|
||||||
isLast2Rows: boolean;
|
|
||||||
deleteClickHandler: () => Promise<void>;
|
|
||||||
}) => {
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
className={cn('dropdown dropdown-left', {
|
|
||||||
'dropdown-start': !isLast2Rows,
|
|
||||||
'dropdown-end': isLast2Rows,
|
|
||||||
})}
|
|
||||||
>
|
|
||||||
<Button tabIndex={0}>
|
|
||||||
<Icon icon='material-symbols:more-vert' width={16} height={16} />
|
|
||||||
</Button>
|
|
||||||
|
|
||||||
<RowOptionsMenu
|
|
||||||
type='dropdown'
|
|
||||||
props={props}
|
|
||||||
deleteClickHandler={deleteClickHandler}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const RowCollapseOptions = ({
|
|
||||||
props,
|
|
||||||
deleteClickHandler,
|
|
||||||
}: {
|
|
||||||
props: CellContext<Nonstock, unknown>;
|
|
||||||
deleteClickHandler: () => Promise<void>;
|
|
||||||
}) => {
|
|
||||||
return (
|
|
||||||
<Collapse
|
|
||||||
title={
|
|
||||||
<Button>
|
|
||||||
<Icon icon='material-symbols:more-vert' width={16} height={16} />
|
|
||||||
</Button>
|
|
||||||
}
|
|
||||||
className='w-fit'
|
|
||||||
titleClassName='p-0! justify-self-end'
|
|
||||||
>
|
|
||||||
<RowOptionsMenu
|
|
||||||
type='collapse'
|
|
||||||
props={props}
|
|
||||||
deleteClickHandler={deleteClickHandler}
|
|
||||||
/>
|
|
||||||
</Collapse>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const NonstocksTable = () => {
|
const NonstocksTable = () => {
|
||||||
|
const {
|
||||||
|
state: tableFilterState,
|
||||||
|
updateFilter,
|
||||||
|
setPage,
|
||||||
|
setPageSize,
|
||||||
|
toQueryString: getTableFilterQueryString,
|
||||||
|
} = useTableFilter({
|
||||||
|
initial: { search: '', nameSort: '', locationSort: '', picSort: '' },
|
||||||
|
paramMap: {
|
||||||
|
page: 'page',
|
||||||
|
pageSize: 'limit',
|
||||||
|
nameSort: 'sort_name',
|
||||||
|
locationSort: 'sort_location',
|
||||||
|
picSort: ' sort_pic',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const {
|
const {
|
||||||
data: nonstocks,
|
data: nonstocks,
|
||||||
isLoading,
|
isLoading,
|
||||||
mutate: refreshNonstocks,
|
mutate: refreshNonstocks,
|
||||||
} = useSWR<NonstocksResponse>('/master-data/nonstocks', httpClientFetcher);
|
} = useSWR(
|
||||||
|
`${NonstockApi.basePath}${getTableFilterQueryString()}`,
|
||||||
|
NonstockApi.getAllFetcher
|
||||||
|
);
|
||||||
|
|
||||||
const [searchValue, setSearchValue] = useState('');
|
const deleteModal = useModal();
|
||||||
|
|
||||||
|
const [selectedNonstock, setSelectedNonstock] = useState<
|
||||||
|
Nonstock | undefined
|
||||||
|
>(undefined);
|
||||||
|
const [isDeleteLoading, setIsDeleteLoading] = useState(false);
|
||||||
|
|
||||||
|
const [sorting, setSorting] = useState<SortingState>([]);
|
||||||
|
|
||||||
const nonstocksColumns: ColumnDef<Nonstock>[] = [
|
const nonstocksColumns: ColumnDef<Nonstock>[] = [
|
||||||
{
|
{
|
||||||
header: '#',
|
header: '#',
|
||||||
cell: (props) => props.row.index + 1,
|
cell: (props) =>
|
||||||
|
tableFilterState.pageSize * (tableFilterState.page - 1) +
|
||||||
|
props.row.index +
|
||||||
|
1,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
header: 'Nama',
|
|
||||||
accessorKey: 'name',
|
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',
|
header: 'Aksi',
|
||||||
@@ -157,33 +164,31 @@ const NonstocksTable = () => {
|
|||||||
|
|
||||||
const isLast2Rows = currentRowRelativeIndex > currentPageSize - 2;
|
const isLast2Rows = currentRowRelativeIndex > currentPageSize - 2;
|
||||||
|
|
||||||
const deleteClickHandler = async () => {
|
const deleteClickHandler = () => {
|
||||||
const confirmation = confirm(
|
setSelectedNonstock(props.row.original);
|
||||||
'Apakah anda yakin untuk menghapus non stock ini?'
|
deleteModal.openModal();
|
||||||
);
|
|
||||||
|
|
||||||
if (confirmation) {
|
|
||||||
await deleteNonstock(props.row.original.id);
|
|
||||||
refreshNonstocks();
|
|
||||||
alert('Nonstock berhasil dihapus!');
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{currentPageSize > 2 && (
|
{currentPageSize > 2 && (
|
||||||
<RowDropdownOptions
|
<RowDropdownOptions isLast2Rows={isLast2Rows}>
|
||||||
props={props}
|
<RowOptionsMenu
|
||||||
isLast2Rows={isLast2Rows}
|
type='dropdown'
|
||||||
deleteClickHandler={deleteClickHandler}
|
props={props}
|
||||||
/>
|
deleteClickHandler={deleteClickHandler}
|
||||||
|
/>
|
||||||
|
</RowDropdownOptions>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{currentPageSize <= 2 && (
|
{currentPageSize <= 2 && (
|
||||||
<RowCollapseOptions
|
<RowCollapseOptions>
|
||||||
props={props}
|
<RowOptionsMenu
|
||||||
deleteClickHandler={deleteClickHandler}
|
type='dropdown'
|
||||||
/>
|
props={props}
|
||||||
|
deleteClickHandler={deleteClickHandler}
|
||||||
|
/>
|
||||||
|
</RowCollapseOptions>
|
||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
@@ -191,52 +196,133 @@ const NonstocksTable = () => {
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
const searchChangeHandler: ChangeEventHandler<HTMLInputElement> = (e) => {
|
const confirmationModalDeleteClickHandler = async () => {
|
||||||
setSearchValue(e.target.value);
|
setIsDeleteLoading(true);
|
||||||
|
|
||||||
|
await NonstockApi.delete(selectedNonstock?.id as number);
|
||||||
|
refreshNonstocks();
|
||||||
|
|
||||||
|
deleteModal.closeModal();
|
||||||
|
toast.success('Successfully delete Nonstock!');
|
||||||
|
setIsDeleteLoading(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const searchChangeHandler: ChangeEventHandler<HTMLInputElement> = (e) => {
|
||||||
|
updateFilter('search', e.target.value);
|
||||||
|
};
|
||||||
|
|
||||||
|
const pageSizeChangeHandler = (val: OptionType | OptionType[] | null) => {
|
||||||
|
const newVal = val as OptionType;
|
||||||
|
|
||||||
|
setPageSize(newVal.value as number);
|
||||||
|
};
|
||||||
|
|
||||||
|
const updateSortingFilter = useCallback(
|
||||||
|
(
|
||||||
|
sortName: Exclude<keyof typeof tableFilterState, 'page' | 'pageSize'>,
|
||||||
|
sortFilter: ColumnSort | undefined
|
||||||
|
) => {
|
||||||
|
if (!sortFilter) {
|
||||||
|
updateFilter(sortName, '');
|
||||||
|
} else {
|
||||||
|
updateFilter(sortName, sortFilter.desc ? 'desc' : 'asc');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[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-4'>
|
<>
|
||||||
<div className='w-full mb-4 flex flex-col sm:flex-row justify-between items-end sm:items-center gap-2'>
|
<div className='w-full p-0 sm:p-4'>
|
||||||
<div className='flex flex-row'>
|
<div className='flex flex-col gap-2 mb-4'>
|
||||||
<Button href='/master-data/nonstock/add' color='primary'>
|
<div className='w-full flex flex-col sm:flex-row justify-between items-end sm:items-center gap-2'>
|
||||||
<Icon icon='ic:round-plus' width={24} height={24} />
|
<div className='flex flex-row'>
|
||||||
Tambah Nonstock
|
<Button href='/master-data/nonstock/add' color='primary'>
|
||||||
</Button>
|
<Icon icon='ic:round-plus' width={24} height={24} />
|
||||||
|
Tambah Nonstock
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<DebouncedTextInput
|
||||||
|
name='search'
|
||||||
|
placeholder='Cari Nonstock'
|
||||||
|
value={tableFilterState.search}
|
||||||
|
onChange={searchChangeHandler}
|
||||||
|
className={{ wrapper: 'sm:max-w-3xs' }}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className='flex flex-row justify-end'>
|
||||||
|
<SelectInput
|
||||||
|
label='Baris'
|
||||||
|
options={ROWS_OPTIONS}
|
||||||
|
value={{
|
||||||
|
label: String(tableFilterState.pageSize),
|
||||||
|
value: tableFilterState.pageSize,
|
||||||
|
}}
|
||||||
|
onChange={pageSizeChangeHandler}
|
||||||
|
className={{ wrapper: 'max-w-28' }}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<TextInput
|
<Table<Nonstock>
|
||||||
name='search'
|
data={isResponseSuccess(nonstocks) ? nonstocks?.data : []}
|
||||||
placeholder='Cari Non Stock'
|
columns={nonstocksColumns}
|
||||||
value={searchValue}
|
pageSize={tableFilterState.pageSize}
|
||||||
onChange={searchChangeHandler}
|
page={isResponseSuccess(nonstocks) ? nonstocks?.meta?.page : 0}
|
||||||
className={{ wrapper: 'sm:max-w-3xs' }}
|
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',
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Table<Nonstock>
|
<ConfirmationModal
|
||||||
data={isResponseSuccess(nonstocks) ? nonstocks?.data : []}
|
ref={deleteModal.ref}
|
||||||
columns={nonstocksColumns}
|
type='error'
|
||||||
pageSize={10}
|
text={`Apakah anda yakin ingin menghapus data Nonstock ini (${selectedNonstock?.name})?`}
|
||||||
fuzzySearchValue={searchValue}
|
secondaryButton={{
|
||||||
onFuzzySearchValueChange={setSearchValue}
|
text: 'Tidak',
|
||||||
isLoading={isLoading}
|
}}
|
||||||
className={{
|
primaryButton={{
|
||||||
containerClassName: cn({
|
text: 'Ya',
|
||||||
'mb-20':
|
color: 'error',
|
||||||
isResponseSuccess(nonstocks) && nonstocks?.data?.length === 0,
|
isLoading: isDeleteLoading,
|
||||||
}),
|
onClick: confirmationModalDeleteClickHandler,
|
||||||
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',
|
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user