diff --git a/src/components/pages/report/marketing/tab/DailyMarketingTab.tsx b/src/components/pages/report/marketing/tab/DailyMarketingTab.tsx index d30e9c25..27195f5b 100644 --- a/src/components/pages/report/marketing/tab/DailyMarketingTab.tsx +++ b/src/components/pages/report/marketing/tab/DailyMarketingTab.tsx @@ -1,4 +1,4 @@ -import { useState, useMemo, useCallback } from 'react'; +import { useState, useMemo, useCallback, useRef } from 'react'; import useSWR from 'swr'; import { useSelect } from '@/components/input/SelectInput'; import DateInput from '@/components/input/DateInput'; @@ -83,6 +83,8 @@ const DailyMarketingTab = ({ tabId }: DailyMarketingTabProps) => { const [dateErrorShown, setDateErrorShown] = useState(false); const [hasDateError, setHasDateError] = useState(false); + const handleFilterModalOpenRef = useRef(() => {}); + const filterModal = useModal(); // ===== OPTIONS ===== @@ -102,11 +104,6 @@ const DailyMarketingTab = ({ tabId }: DailyMarketingTabProps) => { const { options: customerOptions, isLoadingOptions: isLoadingCustomers } = useSelect(CustomerApi.basePath, 'id', 'name', 'search'); - const handleFilterModalOpen = () => { - filterModal.openModal(); - formik.validateForm(); - }; - // ===== FORMIK SETUP ===== const formik = useFormik({ initialValues: { @@ -353,121 +350,126 @@ const DailyMarketingTab = ({ tabId }: DailyMarketingTabProps) => { } }, [dailyMarketingsExport, summaryTotal]); - // ===== 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 + ); - useEffectHook(() => { - setTabActions( - tabId, -
- - } - className={{ - wrapper: 'w-full min-w-48 max-w-3xs', - inputWrapper: 'rounded-xl! shadow-button-soft', - input: 'placeholder:font-semibold placeholder:text-base-content/50', - }} - /> - - - - -
+ useEffectHook(() => { + setTabActions( + tabId, +
+ + } + className={{ + wrapper: 'w-full min-w-48 max-w-3xs', + inputWrapper: 'rounded-xl! shadow-button-soft', + input: + 'placeholder:font-semibold placeholder:text-base-content/50', + }} + /> - Export + handleFilterModalOpenRef.current()} + variant='outline' + className='px-3 py-2.5' + /> -
+ +
+ - -
- - } - > - - -
-
- ); + Export + +
+ + +
+ + } + > + + + +
+ ); + }, [setTabActions]); + + useEffectHook(() => { + return () => { + clearTabActions(tabId); + }; + }, [clearTabActions]); + + return null; + }; }, [ tabId, + filterParams, searchValue, - formik.values, isAnyExportLoading, - filterModal.open, - setTabActions, + handleExportExcel, + handleExportPDF, + isExcelExportLoading, + isPdfExportLoading, + searchChangeHandler, ]); - useEffectHook(() => { - return () => { - clearTabActions(tabId); - }; - }, [tabId, clearTabActions]); - - useEffectHook(() => { - return () => { - if (dateErrorShown) { - toast.dismiss(); - } - }; - }, [dateErrorShown]); - - useEffectHook(() => { - return () => { - if (dateErrorShown) { - toast.dismiss(); - setDateErrorShown(false); - } - }; - }, [filterModal.open, dateErrorShown]); + const TabActionsElement = useMemo(() => , [TabActions]); const getTableColumns = (): ColumnDef[] => { const tableColumns: ColumnDef[] = [ @@ -639,6 +641,7 @@ const DailyMarketingTab = ({ tabId }: DailyMarketingTabProps) => { return ( <> + {TabActionsElement}
{!isSubmitted ? ( { // ===== FILTER STATE ===== const [filterParams, setFilterParams] = useState({}); + const handleFilterModalOpenRef = useRef(() => {}); + const filterModal = useModal(); // ===== OPTIONS ===== @@ -95,11 +97,6 @@ const HppPerKandangTab = ({ tabId }: HppPerKandangTabProps) => { [] ); - const handleFilterModalOpen = () => { - filterModal.openModal(); - formik.validateForm(); - }; - // ===== FORMIK SETUP ===== const formik = useFormik({ initialValues: { @@ -140,6 +137,11 @@ const HppPerKandangTab = ({ tabId }: HppPerKandangTabProps) => { }, }); + handleFilterModalOpenRef.current = () => { + filterModal.openModal(); + formik.validateForm(); + }; + // ===== WEIGHT CHANGE HANDLERS ===== const handleWeightMinChange = useCallback( (e: React.ChangeEvent) => { @@ -442,87 +444,103 @@ const HppPerKandangTab = ({ tabId }: HppPerKandangTabProps) => { allDocSuppliers, ]); - // ===== 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 + ); - useEffectHook(() => { - setTabActions( - tabId, -
- - - { + 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 + useEffectHook(() => { + return () => { + clearTabActions(tabId); + }; + }, [clearTabActions]); -
- - -
- - } - > - - - -
- ); + return null; + }; }, [ tabId, - formik.values, + filterParams, isAnyExportLoading, - filterModal.open, - setTabActions, + handleExportExcel, + handleExportPDF, + isExcelExportLoading, + isPdfExportLoading, ]); - useEffectHook(() => { - return () => { - clearTabActions(tabId); - }; - }, [tabId, clearTabActions]); + const TabActionsElement = useMemo(() => , [TabActions]); const getTableColumns = (): ColumnDef[] => { const tableColumns: ColumnDef[] = [ @@ -767,6 +785,7 @@ const HppPerKandangTab = ({ tabId }: HppPerKandangTabProps) => { return ( <> + {TabActionsElement}
{!isSubmitted ? ( { const [page, setPage] = useState(1); const [pageSize, setPageSize] = useState(10); + const handleFilterModalOpenRef = useRef(() => {}); + const filterModal = useModal(); // ===== TABLE COLUMNS ===== @@ -242,6 +250,11 @@ const ProductionResultContent = ({ tabId }: ProductionResultTabProps) => { }, }); + handleFilterModalOpenRef.current = () => { + filterModal.openModal(); + formik.validateForm(); + }; + // ===== OPTIONS ===== const { setInputValue: setAreaInputValue, @@ -519,91 +532,108 @@ const ProductionResultContent = ({ tabId }: ProductionResultTabProps) => { setIsPdfExportLoading(false); }, [filterParams]); - // ===== 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, -
- filterModal.openModal()} - variant='outline' - className='px-3 py-2.5' - /> - - { + 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, filterParams, isAnyExportLoading, exportToExcelHandler, exportToPdfHandler, - setTabActions, + isExcelExportLoading, + isPdfExportLoading, ]); - useEffect(() => { - return () => { - clearTabActions(tabId); - }; - }, [tabId, clearTabActions]); + // Render the TabActions component + const TabActionsElement = useMemo(() => , [TabActions]); return ( <> + {TabActionsElement}
{!isSubmitted ? (