feat(FE): Add advanced filtering capabilities to master data tables

This commit is contained in:
rstubryan
2026-03-03 14:03:29 +07:00
parent 741884ac29
commit 5e5400f56b
6 changed files with 1083 additions and 39 deletions
@@ -1,25 +1,34 @@
'use client';
import { useEffect, useMemo, useState } from 'react';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { usePathname } from 'next/navigation';
import useSWR from 'swr';
import { CellContext, ColumnDef, SortingState } from '@tanstack/react-table';
import toast from 'react-hot-toast';
import { useFormik } from 'formik';
import { Icon } from '@iconify/react';
import Table from '@/components/Table';
import Button from '@/components/Button';
import { useModal } from '@/components/Modal';
import Modal, { 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 ProductionStandardTableSkeleton from '@/components/pages/master-data/production-standard/skeleton/ProductionStandardTableSkeleton';
import { OptionType } from '@/components/input/SelectInput';
import ButtonFilter from '@/components/helper/ButtonFilter';
import { ProductionStandard } from '@/types/api/master-data/production-standard';
import { ProductionStandardApi } from '@/services/api/master-data';
import { isResponseError, isResponseSuccess } from '@/lib/api-helper';
import { usePathname } from 'next/navigation';
import { useTableFilter } from '@/services/hooks/useTableFilter';
import { useUiStore } from '@/stores/ui/ui.store';
import {
ProductionStandardFilterSchema,
ProductionStandardFilterType,
} from '@/components/pages/master-data/production-standard/filter/ProductionStandardFilter';
import SelectInputRadio from '@/components/input/SelectInputRadio';
const RowOptionsMenu = ({
popoverPosition = 'bottom',
@@ -104,9 +113,81 @@ const ProductionStandardTable = () => {
const { setTableState } = useUiStore();
const pathname = usePathname();
const {
state: tableFilterState,
updateFilter,
setPage,
setPageSize,
toQueryString: getTableFilterQueryString,
} = useTableFilter({
initial: {
projectCategoryFilter: '',
},
paramMap: {
page: 'page',
pageSize: 'limit',
projectCategoryFilter: 'project_category',
},
});
// ===== FILTER MODAL STATE =====
const filterModal = useModal();
// ===== FORMIK SETUP =====
const formik = useFormik<ProductionStandardFilterType>({
initialValues: {
project_category: null,
},
validationSchema: ProductionStandardFilterSchema,
onSubmit: (values, { setSubmitting }) => {
updateFilter('projectCategoryFilter', values.project_category || '');
filterModal.closeModal();
setSubmitting(false);
},
onReset: () => {
updateFilter('projectCategoryFilter', '');
},
});
// ===== PROJECT CATEGORY OPTIONS (GROWING or LAYING) =====
const projectCategoryOptions = useMemo(
() => [
{ value: 'GROWING', label: 'Growing' },
{ value: 'LAYING', label: 'Laying' },
],
[]
);
// ===== FILTER HANDLERS =====
const handleFilterProjectCategoryChange = useCallback(
(val: OptionType | OptionType[] | null) => {
const option = val as OptionType | null;
const category = option?.value ? String(option.value) : null;
formik.setFieldValue('project_category', category);
},
[formik]
);
// ===== FILTER HELPERS =====
const projectCategoryValue = useMemo(() => {
if (!formik.values.project_category) return null;
return (
projectCategoryOptions.find(
(opt) => opt.value === formik.values.project_category
) || null
);
}, [formik.values.project_category, projectCategoryOptions]);
// ===== HANDLE FILTER MODAL OPEN =====
const handleFilterModalOpen = () => {
filterModal.openModal();
formik.validateForm();
};
useEffect(() => {
setTableState('production-standard-table', pathname);
}, [pathname]);
}, [pathname, setTableState]);
const [sorting, setSorting] = useState<SortingState>([]);
@@ -115,7 +196,7 @@ const ProductionStandardTable = () => {
isLoading,
mutate: refreshProductionStandards,
} = useSWR(
`${ProductionStandardApi.basePath}`,
`${ProductionStandardApi.basePath}${getTableFilterQueryString()}`,
ProductionStandardApi.getAllFetcher
);
@@ -149,7 +230,10 @@ const ProductionStandardTable = () => {
() => [
{
header: 'No',
cell: (props) => props.row.index + 1,
cell: (props) =>
tableFilterState.pageSize * (tableFilterState.page - 1) +
props.row.index +
1,
},
{
accessorKey: 'name',
@@ -185,7 +269,7 @@ const ProductionStandardTable = () => {
},
},
],
[deleteModal]
[tableFilterState.pageSize, tableFilterState.page, deleteModal]
);
return (
@@ -206,6 +290,16 @@ const ProductionStandardTable = () => {
</Button>
</RequirePermission>
</div>
{/* Filter */}
<div className='flex flex-1 flex-row justify-start sm:justify-end items-center gap-3 flex-wrap'>
<ButtonFilter
values={tableFilterState}
excludeFields={['page', 'pageSize']}
onClick={handleFilterModalOpen}
className='px-3 py-2.5'
/>
</div>
</div>
{/* Table Section */}
@@ -233,6 +327,11 @@ const ProductionStandardTable = () => {
<Table<ProductionStandard>
data={productionStandards.data}
columns={productionStandardColumns}
pageSize={tableFilterState.pageSize}
page={productionStandards?.meta?.page ?? 0}
totalItems={productionStandards?.meta?.total_results ?? 0}
onPageChange={setPage}
onPageSizeChange={setPageSize}
isLoading={false}
sorting={sorting}
setSorting={setSorting}
@@ -259,6 +358,65 @@ const ProductionStandardTable = () => {
onClick: confirmationModalDeleteClickHandler,
}}
/>
{/* Filter Modal */}
<Modal
ref={filterModal.ref}
className={{
modal: 'p-0',
modalBox: 'p-0 rounded-[0.875rem] xl:max-w-4/12 max-w-sm',
}}
>
{/* Modal Header */}
<div className='flex items-center justify-between gap-2 border-b border-base-content/10 p-4'>
<div className='flex items-center gap-2 text-primary'>
<Icon icon='heroicons:funnel' width={20} height={20} />
<h3 className='font-medium text-sm'>Filter Data</h3>
</div>
<Button
variant='link'
onClick={filterModal.closeModal}
className='text-base-content/50 hover:text-base-content transition-colors cursor-pointer'
>
<Icon icon='heroicons:x-mark' width={20} height={20} />
</Button>
</div>
<form onSubmit={formik.handleSubmit} onReset={formik.handleReset}>
<div className='p-4 flex flex-col gap-1.5'>
<SelectInputRadio
label='Kategori'
placeholder='Pilih Kategori'
options={projectCategoryOptions}
value={projectCategoryValue}
onChange={handleFilterProjectCategoryChange}
isClearable
className={{ wrapper: 'w-full' }}
/>
</div>
{/* Modal Footer */}
<div className='flex justify-between items-center gap-4 p-4 border-t border-base-content/10 bg-gray-50'>
<Button
type='button'
variant='soft'
className='rounded-lg text-base-content/65 bg-transparent border-none hover:bg-base-content/10 hover:text-base-content/65 transition-colors px-3 py-2'
onClick={() => {
formik.resetForm();
filterModal.closeModal();
}}
>
Reset Filter
</Button>
<Button
type='submit'
className='min-w-40 text-sm rounded-lg py-3 text-white font-semibold'
disabled={!formik.isValid || formik.isSubmitting}
>
Apply Filter
</Button>
</div>
</form>
</Modal>
</>
);
};