From e52ba7b3940201744408b6cfa4e4603a0ad5d5fe Mon Sep 17 00:00:00 2001 From: ValdiANS Date: Thu, 30 Apr 2026 15:01:11 +0700 Subject: [PATCH] fix: search input value and change handler --- src/components/pages/finance/FinanceTable.tsx | 99 ++++++------------- 1 file changed, 31 insertions(+), 68 deletions(-) diff --git a/src/components/pages/finance/FinanceTable.tsx b/src/components/pages/finance/FinanceTable.tsx index 5173a618..998193e7 100644 --- a/src/components/pages/finance/FinanceTable.tsx +++ b/src/components/pages/finance/FinanceTable.tsx @@ -1,12 +1,6 @@ 'use client'; -import React, { - useCallback, - useEffect, - useMemo, - useRef, - useState, -} from 'react'; +import React, { useEffect, useMemo, useState } from 'react'; import { CellContext, ColumnDef } from '@tanstack/react-table'; import useSWR from 'swr'; import { Icon } from '@iconify/react'; @@ -40,7 +34,6 @@ import ConfirmationModal from '@/components/modal/ConfirmationModal'; import toast from 'react-hot-toast'; import RequirePermission from '@/components/helper/RequirePermission'; import ButtonFilter from '@/components/helper/ButtonFilter'; -import { useUiStore } from '@/stores/ui/ui.store'; import { FinanceTableFilterSchema, FinanceTableFilterValues, @@ -177,9 +170,6 @@ const RowOptionsMenu = ({ }; const FinanceTable = () => { - const { searchValue, setSearchValue, resetSearchValue } = useUiStore(); - const previousPathRef = useRef(null); - const { state: tableFilterState, updateFilter, @@ -188,7 +178,7 @@ const FinanceTable = () => { toQueryString: getTableFilterQueryString, } = useTableFilter({ initial: { - search: searchValue, + search: '', transactionTypes: '', bankIds: '', customerIds: '', @@ -242,7 +232,7 @@ const FinanceTable = () => { // ===== Formik for Filter ===== const filterFormik = useFormik({ initialValues: { - search: searchValue, + search: tableFilterState.search || '', transaction_types: '', bank_ids: '', customer_ids: '', @@ -252,17 +242,15 @@ const FinanceTable = () => { end_date: '', }, validationSchema: FinanceTableFilterSchema, - enableReinitialize: true, - onSubmit: (values) => { - updateFilter('search', values.search); - setSearchValue(values.search); - updateFilter('transactionTypes', values.transaction_types); - updateFilter('bankIds', values.bank_ids); - updateFilter('customerIds', values.customer_ids); - updateFilter('supplierIds', values.supplier_ids); - updateFilter('sortBy', values.sort_by); - updateFilter('startDate', values.start_date); - updateFilter('endDate', values.end_date); + onSubmit: (values, { setSubmitting }) => { + updateFilter('search', values.search, true); + updateFilter('transactionTypes', values.transaction_types, true); + updateFilter('bankIds', values.bank_ids, true); + updateFilter('customerIds', values.customer_ids, true); + updateFilter('supplierIds', values.supplier_ids, true); + updateFilter('sortBy', values.sort_by, true); + updateFilter('startDate', values.start_date, true); + updateFilter('endDate', values.end_date, true); // Save display names for restoration on modal reopen const toNames = (val: OptionType | OptionType[] | null) => val @@ -270,10 +258,12 @@ const FinanceTable = () => { .map((o) => String(o.label)) .join(',') : ''; - updateFilter('bankNames', toNames(selectedBank)); - updateFilter('customerNames', toNames(selectedCustomerId)); - updateFilter('supplierNames', toNames(selectedSupplierId)); + updateFilter('bankNames', toNames(selectedBank), true); + updateFilter('customerNames', toNames(selectedCustomerId), true); + updateFilter('supplierNames', toNames(selectedSupplierId), true); filterModal.closeModal(); + + setSubmitting(false); }, onReset: () => { setSelectedTransactionType(null); @@ -281,18 +271,17 @@ const FinanceTable = () => { setSelectedCustomerId(null); setSelectedSupplierId(null); setSelectedSortBy(null); - updateFilter('search', ''); - resetSearchValue(); - updateFilter('transactionTypes', ''); - updateFilter('bankIds', ''); - updateFilter('customerIds', ''); - updateFilter('supplierIds', ''); - updateFilter('sortBy', ''); - updateFilter('startDate', ''); - updateFilter('endDate', ''); - updateFilter('bankNames', ''); - updateFilter('customerNames', ''); - updateFilter('supplierNames', ''); + updateFilter('search', '', true); + updateFilter('transactionTypes', '', true); + updateFilter('bankIds', '', true); + updateFilter('customerIds', '', true); + updateFilter('supplierIds', '', true); + updateFilter('sortBy', '', true); + updateFilter('startDate', '', true); + updateFilter('endDate', '', true); + updateFilter('bankNames', '', true); + updateFilter('customerNames', '', true); + updateFilter('supplierNames', '', true); filterModal.closeModal(); }, }); @@ -347,14 +336,9 @@ const FinanceTable = () => { }, [bankOptions, bankRawData]); // ===== Handler ===== - const searchChangeHandler = useCallback( - (e: React.ChangeEvent) => { - updateFilter('search', e.target.value); - setSearchValue(e.target.value); - setPage(1); - }, - [updateFilter, setSearchValue, setPage] - ); + const searchChangeHandler = (e: React.ChangeEvent) => { + updateFilter('search', e.target.value, true); + }; const transactionTypeChangeHandler = ( val: OptionType | OptionType[] | null @@ -656,27 +640,6 @@ const FinanceTable = () => { }; }, [dateErrorShown]); - useEffect(() => { - previousPathRef.current = window.location.pathname; - - return () => { - const currentPath = window.location.pathname; - - const isCurrentPathFinance = currentPath.includes('/finance'); - const isPreviousPathFinance = - previousPathRef.current?.includes('/finance'); - - if (isPreviousPathFinance && !isCurrentPathFinance) { - resetSearchValue(); - } - - if (dateErrorShown) { - toast.dismiss(); - setDateErrorShown(false); - } - }; - }, [resetSearchValue, dateErrorShown]); - return ( <>