mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-25 15:55:48 +00:00
refactor(FE): Refactor tab actions to use memoized component
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { useState, useMemo, useCallback, useEffect } from 'react';
|
import { useState, useMemo, useCallback, useEffect, useRef } from 'react';
|
||||||
import useSWR from 'swr';
|
import useSWR from 'swr';
|
||||||
import { Icon } from '@iconify/react';
|
import { Icon } from '@iconify/react';
|
||||||
import Card from '@/components/Card';
|
import Card from '@/components/Card';
|
||||||
@@ -66,6 +66,8 @@ const CustomerPaymentTab = ({ tabId }: CustomerPaymentTabProps) => {
|
|||||||
const [dateErrorShown, setDateErrorShown] = useState(false);
|
const [dateErrorShown, setDateErrorShown] = useState(false);
|
||||||
const [hasDateError, setHasDateError] = useState(false);
|
const [hasDateError, setHasDateError] = useState(false);
|
||||||
|
|
||||||
|
const handleFilterModalOpenRef = useRef(() => {});
|
||||||
|
|
||||||
const filterModal = useModal();
|
const filterModal = useModal();
|
||||||
|
|
||||||
const dataTypeOptions = useMemo(
|
const dataTypeOptions = useMemo(
|
||||||
@@ -83,11 +85,6 @@ const CustomerPaymentTab = ({ tabId }: CustomerPaymentTabProps) => {
|
|||||||
loadMore: loadMoreCustomers,
|
loadMore: loadMoreCustomers,
|
||||||
} = useSelect(CustomerApi.basePath, 'id', 'name', 'search');
|
} = useSelect(CustomerApi.basePath, 'id', 'name', 'search');
|
||||||
|
|
||||||
const handleFilterModalOpen = () => {
|
|
||||||
filterModal.openModal();
|
|
||||||
formik.validateForm();
|
|
||||||
};
|
|
||||||
|
|
||||||
// ===== FORMIK SETUP =====
|
// ===== FORMIK SETUP =====
|
||||||
const formik = useFormik<CustomerPaymentFilterType>({
|
const formik = useFormik<CustomerPaymentFilterType>({
|
||||||
initialValues: {
|
initialValues: {
|
||||||
@@ -122,6 +119,12 @@ const CustomerPaymentTab = ({ tabId }: CustomerPaymentTabProps) => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Set the ref callback after formik is initialized
|
||||||
|
handleFilterModalOpenRef.current = () => {
|
||||||
|
filterModal.openModal();
|
||||||
|
formik.validateForm();
|
||||||
|
};
|
||||||
|
|
||||||
const getPaymentStatusBadgeColor = (notes: string): Color => {
|
const getPaymentStatusBadgeColor = (notes: string): Color => {
|
||||||
const normalizedValue = notes.toLowerCase();
|
const normalizedValue = notes.toLowerCase();
|
||||||
|
|
||||||
@@ -212,7 +215,7 @@ const CustomerPaymentTab = ({ tabId }: CustomerPaymentTabProps) => {
|
|||||||
dataTypeOptions.find((opt) => opt.value === formik.values.filter_by) ||
|
dataTypeOptions.find((opt) => opt.value === formik.values.filter_by) ||
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
}, [formik.values.filter_by]);
|
}, [formik.values.filter_by, dataTypeOptions]);
|
||||||
|
|
||||||
// ===== DATA FETCHING =====
|
// ===== DATA FETCHING =====
|
||||||
const { data: customerPayment, isLoading } = useSWR(
|
const { data: customerPayment, isLoading } = useSWR(
|
||||||
@@ -349,90 +352,107 @@ const CustomerPaymentTab = ({ tabId }: CustomerPaymentTabProps) => {
|
|||||||
}
|
}
|
||||||
}, [customerPaymentExport, filterParams, customerOptions]);
|
}, [customerPaymentExport, filterParams, customerOptions]);
|
||||||
|
|
||||||
// ===== REGISTER TAB ACTIONS TO STORE =====
|
// ===== TAB ACTIONS COMPONENT =====
|
||||||
const setTabActions = useTabActionsStore((state) => state.setTabActions);
|
const TabActions = useMemo(() => {
|
||||||
const clearTabActions = useTabActionsStore((state) => state.clearTabActions);
|
return function TabActionsComponent() {
|
||||||
|
const setTabActions = useTabActionsStore((state) => state.setTabActions);
|
||||||
|
const clearTabActions = useTabActionsStore(
|
||||||
|
(state) => state.clearTabActions
|
||||||
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
const formikValuesRef = useRef(formik.values);
|
||||||
setTabActions(
|
formikValuesRef.current = formik.values;
|
||||||
tabId,
|
|
||||||
<div className='flex flex-row gap-3'>
|
|
||||||
<ButtonFilter
|
|
||||||
values={formik.values}
|
|
||||||
fieldGroups={[['start_date', 'end_date']]}
|
|
||||||
onClick={handleFilterModalOpen}
|
|
||||||
variant='outline'
|
|
||||||
className='px-3 py-2.5'
|
|
||||||
/>
|
|
||||||
|
|
||||||
<Dropdown
|
useEffect(() => {
|
||||||
align='end'
|
setTabActions(
|
||||||
direction='bottom'
|
tabId,
|
||||||
className={{
|
<div className='flex flex-row gap-3'>
|
||||||
content:
|
<ButtonFilter
|
||||||
'mt-1 rounded-xl border border-base-content/5 shadow-sm overflow-hidden',
|
values={formikValuesRef.current}
|
||||||
}}
|
fieldGroups={[['start_date', 'end_date']]}
|
||||||
trigger={
|
onClick={() => handleFilterModalOpenRef.current()}
|
||||||
<Button
|
|
||||||
variant='outline'
|
variant='outline'
|
||||||
color='none'
|
className='px-3 py-2.5'
|
||||||
isLoading={isAnyExportLoading}
|
/>
|
||||||
className='px-3 py-2.5 text-sm text-base-content/50 border border-base-content/10 rounded-xl shadow-button-soft'
|
|
||||||
|
<Dropdown
|
||||||
|
align='end'
|
||||||
|
direction='bottom'
|
||||||
|
className={{
|
||||||
|
content:
|
||||||
|
'mt-1 rounded-xl border border-base-content/5 shadow-sm overflow-hidden',
|
||||||
|
}}
|
||||||
|
trigger={
|
||||||
|
<Button
|
||||||
|
variant='outline'
|
||||||
|
color='none'
|
||||||
|
isLoading={isAnyExportLoading}
|
||||||
|
className='px-3 py-2.5 text-sm text-base-content/50 border border-base-content/10 rounded-xl shadow-button-soft'
|
||||||
|
>
|
||||||
|
<div className='flex flex-row items-center gap-1.5'>
|
||||||
|
<Icon
|
||||||
|
icon='heroicons:cloud-arrow-down'
|
||||||
|
width={20}
|
||||||
|
height={20}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<span>Export</span>
|
||||||
|
|
||||||
|
<div className='w-px self-stretch bg-base-content/10' />
|
||||||
|
|
||||||
|
<Icon
|
||||||
|
icon='heroicons:chevron-down'
|
||||||
|
width={14}
|
||||||
|
height={14}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</Button>
|
||||||
|
}
|
||||||
>
|
>
|
||||||
<div className='flex flex-row items-center gap-1.5'>
|
<Button
|
||||||
<Icon
|
variant='ghost'
|
||||||
icon='heroicons:cloud-arrow-down'
|
color='none'
|
||||||
width={20}
|
onClick={handleExportExcel}
|
||||||
height={20}
|
isLoading={isExcelExportLoading}
|
||||||
/>
|
className='w-full p-3 justify-start text-sm text-base-content/50 font-semibold text-nowrap'
|
||||||
|
>
|
||||||
|
<Icon icon='heroicons:table-cells' width={20} height={20} />
|
||||||
|
Export to Excel
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant='ghost'
|
||||||
|
color='none'
|
||||||
|
onClick={handleExportPdf}
|
||||||
|
isLoading={isPdfExportLoading}
|
||||||
|
className='w-full p-3 justify-start text-sm text-base-content/50 font-semibold text-nowrap'
|
||||||
|
>
|
||||||
|
<Icon icon='heroicons:document' width={20} height={20} />
|
||||||
|
Export to PDF
|
||||||
|
</Button>
|
||||||
|
</Dropdown>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}, [setTabActions]);
|
||||||
|
|
||||||
<span>Export</span>
|
useEffect(() => {
|
||||||
|
return () => {
|
||||||
|
clearTabActions(tabId);
|
||||||
|
};
|
||||||
|
}, [clearTabActions]);
|
||||||
|
|
||||||
<div className='w-px self-stretch bg-base-content/10' />
|
return null;
|
||||||
|
};
|
||||||
<Icon icon='heroicons:chevron-down' width={14} height={14} />
|
|
||||||
</div>
|
|
||||||
</Button>
|
|
||||||
}
|
|
||||||
>
|
|
||||||
<Button
|
|
||||||
variant='ghost'
|
|
||||||
color='none'
|
|
||||||
onClick={handleExportExcel}
|
|
||||||
isLoading={isExcelExportLoading}
|
|
||||||
className='w-full p-3 justify-start text-sm text-base-content/50 font-semibold text-nowrap'
|
|
||||||
>
|
|
||||||
<Icon icon='heroicons:table-cells' width={20} height={20} />
|
|
||||||
Export to Excel
|
|
||||||
</Button>
|
|
||||||
<Button
|
|
||||||
variant='ghost'
|
|
||||||
color='none'
|
|
||||||
onClick={handleExportPdf}
|
|
||||||
isLoading={isPdfExportLoading}
|
|
||||||
className='w-full p-3 justify-start text-sm text-base-content/50 font-semibold text-nowrap'
|
|
||||||
>
|
|
||||||
<Icon icon='heroicons:document' width={20} height={20} />
|
|
||||||
Export to PDF
|
|
||||||
</Button>
|
|
||||||
</Dropdown>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}, [
|
}, [
|
||||||
tabId,
|
tabId,
|
||||||
formik.values,
|
|
||||||
isAnyExportLoading,
|
isAnyExportLoading,
|
||||||
handleExportExcel,
|
handleExportExcel,
|
||||||
handleExportPdf,
|
handleExportPdf,
|
||||||
filterModal.open,
|
isExcelExportLoading,
|
||||||
setTabActions,
|
isPdfExportLoading,
|
||||||
|
formik.values,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
useEffect(() => {
|
const TabActionsElement = useMemo(() => <TabActions />, [TabActions]);
|
||||||
return () => {
|
|
||||||
clearTabActions(tabId);
|
|
||||||
};
|
|
||||||
}, [tabId, clearTabActions]);
|
|
||||||
|
|
||||||
const getTableColumns = (
|
const getTableColumns = (
|
||||||
summary: CustomerPaymentSummary
|
summary: CustomerPaymentSummary
|
||||||
@@ -682,6 +702,7 @@ const CustomerPaymentTab = ({ tabId }: CustomerPaymentTabProps) => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
{TabActionsElement}
|
||||||
<div className='w-full p-0 sm:p-3 flex flex-col gap-3'>
|
<div className='w-full p-0 sm:p-3 flex flex-col gap-3'>
|
||||||
{!isSubmitted ? (
|
{!isSubmitted ? (
|
||||||
<CustomerSupplierSkeleton
|
<CustomerSupplierSkeleton
|
||||||
|
|||||||
Reference in New Issue
Block a user