mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 13:32:00 +00:00
336 lines
10 KiB
TypeScript
336 lines
10 KiB
TypeScript
'use client';
|
|
|
|
import { ChangeEventHandler, useEffect, useMemo, useState } from 'react';
|
|
import useSWR from 'swr';
|
|
import { CellContext, ColumnDef, SortingState } from '@tanstack/react-table';
|
|
import toast from 'react-hot-toast';
|
|
|
|
import { Icon } from '@iconify/react';
|
|
import Table from '@/components/Table';
|
|
import DebouncedTextInput from '@/components/input/DebouncedTextInput';
|
|
import Button from '@/components/Button';
|
|
import { useModal } from '@/components/Modal';
|
|
import ConfirmationModal from '@/components/modal/ConfirmationModal';
|
|
import RequirePermission from '@/components/helper/RequirePermission';
|
|
import PopoverButton from '@/components/popover/PopoverButton';
|
|
import PopoverContent from '@/components/popover/PopoverContent';
|
|
import CustomerTableSkeleton from '@/components/pages/master-data/customer/skeleton/CustomerTableSkeleton';
|
|
|
|
import { Customer } from '@/types/api/master-data/customer';
|
|
import { CustomerApi } from '@/services/api/master-data';
|
|
import { isResponseError, isResponseSuccess } from '@/lib/api-helper';
|
|
import { useTableFilter } from '@/services/hooks/useTableFilter';
|
|
import { usePathname } from 'next/navigation';
|
|
import { useUiStore } from '@/stores/ui/ui.store';
|
|
|
|
const RowOptionsMenu = ({
|
|
popoverPosition = 'bottom',
|
|
props,
|
|
deleteClickHandler,
|
|
}: {
|
|
popoverPosition: 'bottom' | 'top';
|
|
props: CellContext<Customer, unknown>;
|
|
deleteClickHandler: () => void;
|
|
}) => {
|
|
const popoverId = `customer#${props.row.original.id}`;
|
|
const popoverAnchorName = `--anchor-customer#${props.row.original.id}`;
|
|
|
|
const closePopover = () => {
|
|
document.getElementById(popoverId)?.hidePopover();
|
|
};
|
|
|
|
return (
|
|
<div className='relative'>
|
|
<PopoverButton
|
|
tabIndex={0}
|
|
variant='ghost'
|
|
color='none'
|
|
popoverTarget={popoverId}
|
|
anchorName={popoverAnchorName}
|
|
>
|
|
<Icon icon='material-symbols:more-vert' width={16} height={16} />
|
|
</PopoverButton>
|
|
|
|
<PopoverContent
|
|
id={popoverId}
|
|
anchorName={popoverAnchorName}
|
|
position={popoverPosition === 'bottom' ? 'bottom-start' : 'left'}
|
|
className='w-full max-w-40 rounded-xl border border-base-content/5 shadow-sm'
|
|
>
|
|
<div className='flex flex-col bg-base-100 rounded-xl'>
|
|
<RequirePermission permissions='lti.master.customer.detail'>
|
|
<Button
|
|
href={`/master-data/customer/detail/?customerId=${props.row.original.id}`}
|
|
variant='ghost'
|
|
color='none'
|
|
className='p-3 justify-start text-sm font-semibold w-full'
|
|
onClick={closePopover}
|
|
>
|
|
<Icon icon='heroicons:eye' width={20} height={20} />
|
|
Detail
|
|
</Button>
|
|
</RequirePermission>
|
|
<RequirePermission permissions='lti.master.customer.update'>
|
|
<Button
|
|
href={`/master-data/customer/detail/edit/?customerId=${props.row.original.id}`}
|
|
variant='ghost'
|
|
color='none'
|
|
className='p-3 justify-start text-sm font-semibold w-full'
|
|
onClick={closePopover}
|
|
>
|
|
<Icon icon='heroicons:pencil-square' width={20} height={20} />
|
|
Edit
|
|
</Button>
|
|
</RequirePermission>
|
|
<RequirePermission permissions='lti.master.customer.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>
|
|
);
|
|
};
|
|
|
|
const CustomersTable = () => {
|
|
const { searchValue, setSearchValue, setTableState } = useUiStore();
|
|
const pathname = usePathname();
|
|
|
|
const {
|
|
state: tableFilterState,
|
|
updateFilter,
|
|
setPage,
|
|
setPageSize,
|
|
toQueryString: getTableFilterQueryString,
|
|
} = useTableFilter({
|
|
initial: {
|
|
search: searchValue,
|
|
},
|
|
paramMap: {
|
|
page: 'page',
|
|
pageSize: 'limit',
|
|
},
|
|
});
|
|
|
|
const [sorting, setSorting] = useState<SortingState>([]);
|
|
|
|
const {
|
|
data: customers,
|
|
isLoading,
|
|
mutate: refreshCustomers,
|
|
} = useSWR(
|
|
`${CustomerApi.basePath}${getTableFilterQueryString()}`,
|
|
CustomerApi.getAllFetcher
|
|
);
|
|
|
|
const deleteModal = useModal();
|
|
const [selectedCustomer, setSelectedCustomer] = useState<
|
|
Customer | undefined
|
|
>(undefined);
|
|
const [isDeleteLoading, setIsDeleteLoading] = useState(false);
|
|
|
|
useEffect(() => {
|
|
updateFilter('search', searchValue);
|
|
}, [searchValue, updateFilter]);
|
|
|
|
useEffect(() => {
|
|
setTableState('customers-table', pathname);
|
|
}, [pathname, setTableState]);
|
|
|
|
const searchChangeHandler: ChangeEventHandler<HTMLInputElement> = (e) => {
|
|
setSearchValue(e.target.value);
|
|
updateFilter('search', e.target.value);
|
|
};
|
|
|
|
const confirmationModalDeleteClickHandler = async () => {
|
|
setIsDeleteLoading(true);
|
|
|
|
const deleteResponse = await CustomerApi.delete(
|
|
selectedCustomer?.id as number
|
|
);
|
|
|
|
if (isResponseError(deleteResponse)) {
|
|
toast.error(deleteResponse.message);
|
|
setIsDeleteLoading(false);
|
|
return;
|
|
}
|
|
|
|
refreshCustomers();
|
|
|
|
deleteModal.closeModal();
|
|
toast.success('Successfully delete Customer!');
|
|
setIsDeleteLoading(false);
|
|
};
|
|
|
|
const customersColumns: ColumnDef<Customer>[] = useMemo(
|
|
() => [
|
|
{
|
|
header: 'No',
|
|
cell: (props) =>
|
|
tableFilterState.pageSize * (tableFilterState.page - 1) +
|
|
props.row.index +
|
|
1,
|
|
},
|
|
{
|
|
accessorKey: 'name',
|
|
header: 'Nama',
|
|
},
|
|
{
|
|
accessorFn: (row) => row.pic?.name ?? '-',
|
|
header: 'PIC',
|
|
},
|
|
{
|
|
accessorKey: 'type',
|
|
header: 'Type',
|
|
},
|
|
{
|
|
accessorKey: 'phone',
|
|
header: 'Phone',
|
|
},
|
|
{
|
|
accessorKey: 'email',
|
|
header: 'Email',
|
|
},
|
|
{
|
|
header: 'Aksi',
|
|
cell: (props: CellContext<Customer, 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 isLast2Rows = currentRowRelativeIndex > currentPageSize - 2;
|
|
|
|
const deleteClickHandler = () => {
|
|
setSelectedCustomer(props.row.original);
|
|
deleteModal.openModal();
|
|
};
|
|
|
|
return (
|
|
<RowOptionsMenu
|
|
props={props}
|
|
popoverPosition={isLast2Rows ? 'top' : 'bottom'}
|
|
deleteClickHandler={deleteClickHandler}
|
|
/>
|
|
);
|
|
},
|
|
},
|
|
],
|
|
[tableFilterState.pageSize, tableFilterState.page, deleteModal]
|
|
);
|
|
|
|
return (
|
|
<>
|
|
<div className='w-full'>
|
|
{/* Header Section */}
|
|
<div className='w-full p-3 flex flex-row justify-between gap-3 flex-wrap border-b border-base-content/10'>
|
|
{/* Action Buttons */}
|
|
<div className='w-fit flex flex-row gap-3 flex-wrap'>
|
|
<RequirePermission permissions='lti.master.customer.create'>
|
|
<Button
|
|
href='/master-data/customer/add'
|
|
color='primary'
|
|
className='px-3 py-2.5 w-fit text-sm text-base-100 rounded-lg shadow-sm'
|
|
>
|
|
<Icon icon='heroicons:plus' width={20} height={20} />
|
|
Add Customer
|
|
</Button>
|
|
</RequirePermission>
|
|
</div>
|
|
|
|
{/* Search */}
|
|
<div className='flex flex-1 flex-row justify-start sm:justify-end items-center gap-3 flex-wrap'>
|
|
<DebouncedTextInput
|
|
name='search'
|
|
placeholder='Search'
|
|
value={tableFilterState.search ?? ''}
|
|
onChange={searchChangeHandler}
|
|
startAdornment={
|
|
<Icon
|
|
icon='heroicons:magnifying-glass'
|
|
width={20}
|
|
height={20}
|
|
/>
|
|
}
|
|
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',
|
|
}}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Table Section */}
|
|
<div className='flex flex-col mb-4'>
|
|
{isLoading ? (
|
|
<div className='w-full flex flex-row justify-center items-center p-4'>
|
|
<span className='loading loading-spinner loading-xl' />
|
|
</div>
|
|
) : !isResponseSuccess(customers) || customers.data?.length === 0 ? (
|
|
<div className='p-3'>
|
|
<CustomerTableSkeleton
|
|
columns={customersColumns}
|
|
icon={
|
|
<Icon
|
|
icon='heroicons:document-text'
|
|
className='text-white'
|
|
width={20}
|
|
height={20}
|
|
/>
|
|
}
|
|
/>
|
|
</div>
|
|
) : (
|
|
<Table<Customer>
|
|
data={customers?.data}
|
|
columns={customersColumns}
|
|
pageSize={tableFilterState.pageSize}
|
|
page={customers?.meta?.page ?? 0}
|
|
totalItems={customers?.meta?.total_results ?? 0}
|
|
onPageChange={setPage}
|
|
onPageSizeChange={setPageSize}
|
|
isLoading={false}
|
|
sorting={sorting}
|
|
setSorting={setSorting}
|
|
className={{
|
|
containerClassName: 'p-3 mb-0',
|
|
headerColumnClassName: 'text-nowrap',
|
|
}}
|
|
/>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
<ConfirmationModal
|
|
ref={deleteModal.ref}
|
|
type='error'
|
|
text={`Apakah anda yakin ingin menghapus data Customer ini (${selectedCustomer?.name})?`}
|
|
secondaryButton={{
|
|
text: 'Tidak',
|
|
}}
|
|
primaryButton={{
|
|
text: 'Ya',
|
|
color: 'error',
|
|
isLoading: isDeleteLoading,
|
|
onClick: confirmationModalDeleteClickHandler,
|
|
}}
|
|
/>
|
|
</>
|
|
);
|
|
};
|
|
|
|
export default CustomersTable;
|