diff --git a/src/components/pages/report/finance/tab/CustomerPaymentTab.tsx b/src/components/pages/report/finance/tab/CustomerPaymentTab.tsx index 1269affc..b04ce96a 100644 --- a/src/components/pages/report/finance/tab/CustomerPaymentTab.tsx +++ b/src/components/pages/report/finance/tab/CustomerPaymentTab.tsx @@ -1,4 +1,4 @@ -import { useState, useMemo, useCallback, useEffect } from 'react'; +import { useState, useMemo, useCallback, useEffect, useRef } from 'react'; import useSWR from 'swr'; import { Icon } from '@iconify/react'; import Card from '@/components/Card'; @@ -66,6 +66,8 @@ const CustomerPaymentTab = ({ tabId }: CustomerPaymentTabProps) => { const [dateErrorShown, setDateErrorShown] = useState(false); const [hasDateError, setHasDateError] = useState(false); + const handleFilterModalOpenRef = useRef(() => {}); + const filterModal = useModal(); const dataTypeOptions = useMemo( @@ -83,11 +85,6 @@ const CustomerPaymentTab = ({ tabId }: CustomerPaymentTabProps) => { loadMore: loadMoreCustomers, } = useSelect(CustomerApi.basePath, 'id', 'name', 'search'); - const handleFilterModalOpen = () => { - filterModal.openModal(); - formik.validateForm(); - }; - // ===== FORMIK SETUP ===== const formik = useFormik({ 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 normalizedValue = notes.toLowerCase(); @@ -212,7 +215,7 @@ const CustomerPaymentTab = ({ tabId }: CustomerPaymentTabProps) => { dataTypeOptions.find((opt) => opt.value === formik.values.filter_by) || null ); - }, [formik.values.filter_by]); + }, [formik.values.filter_by, dataTypeOptions]); // ===== DATA FETCHING ===== const { data: customerPayment, isLoading } = useSWR( @@ -349,90 +352,107 @@ const CustomerPaymentTab = ({ tabId }: CustomerPaymentTabProps) => { } }, [customerPaymentExport, filterParams, customerOptions]); - // ===== REGISTER TAB ACTIONS TO STORE ===== - const setTabActions = useTabActionsStore((state) => state.setTabActions); - const clearTabActions = useTabActionsStore((state) => state.clearTabActions); + // ===== TAB ACTIONS COMPONENT ===== + const TabActions = useMemo(() => { + return function TabActionsComponent() { + const setTabActions = useTabActionsStore((state) => state.setTabActions); + const clearTabActions = useTabActionsStore( + (state) => state.clearTabActions + ); - useEffect(() => { - setTabActions( - tabId, -
- + const formikValuesRef = useRef(formik.values); + formikValuesRef.current = formik.values; - { + setTabActions( + tabId, +
+ handleFilterModalOpenRef.current()} 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' + className='px-3 py-2.5' + /> + + +
+ + + Export + +
+ + +
+ + } > -
- + + + +
+ ); + }, [setTabActions]); - Export + useEffect(() => { + return () => { + clearTabActions(tabId); + }; + }, [clearTabActions]); -
- - -
- - } - > - - - -
- ); + return null; + }; }, [ tabId, - formik.values, isAnyExportLoading, handleExportExcel, handleExportPdf, - filterModal.open, - setTabActions, + isExcelExportLoading, + isPdfExportLoading, + formik.values, ]); - useEffect(() => { - return () => { - clearTabActions(tabId); - }; - }, [tabId, clearTabActions]); + const TabActionsElement = useMemo(() => , [TabActions]); const getTableColumns = ( summary: CustomerPaymentSummary @@ -682,6 +702,7 @@ const CustomerPaymentTab = ({ tabId }: CustomerPaymentTabProps) => { return ( <> + {TabActionsElement}
{!isSubmitted ? (