From 1480507c353009d2d7238c97f6eaf09fcfddb3ab Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Sun, 28 May 2023 02:18:37 +0200 Subject: [PATCH] Fix/source options filters state (#320) * Prevent mutating passed object Causes the options to not get reset correctly and/or to get set without "submitting" them * Always use local filter state instead of requested one The local filter state sometimes got overwritten by the state from the server response. This resulted in showing an incorrect filter state while still sending the correct one to the server when filtering * Update text filter faster The timeout (2.5s) is too long and can cause the filter state to not have been updated by the time the filters are getting submitted. Thus, causing a request with outdated filters and requiring a "re-submit" for the actual filters to get used. --- src/components/source/SourceOptions.tsx | 10 ++++---- src/components/source/filters/TextFilter.tsx | 24 +++++++------------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/src/components/source/SourceOptions.tsx b/src/components/source/SourceOptions.tsx index 736ac0da..2d4355ee 100644 --- a/src/components/source/SourceOptions.tsx +++ b/src/components/source/SourceOptions.tsx @@ -56,7 +56,7 @@ export function Options({ sourceFilter, group, updateFilterValue, update }: IFil = (props) => { const { state, name, position, group, updateFilterValue, update } = props; const [Search, setsearch] = React.useState(state || ''); - let typingTimer: NodeJS.Timeout; + const inputText = useDebounce(Search, 500); - function doneTyping(e: React.ChangeEvent) { + useEffect(() => { const upd = update.filter( (el: { position: number; group: number | undefined }) => !(position === el.position && group === el.group), ); - updateFilterValue([...upd, { position, state: e.target.value, group }]); - } - - function handleChange(e: React.ChangeEvent) { - setsearch(e.target.value); - - clearTimeout(typingTimer); - typingTimer = setTimeout(() => { - doneTyping(e); - }, 2500); - } + updateFilterValue([...upd, { position, state: inputText, group }]); + }, [inputText]); if (state !== undefined) { return ( @@ -46,8 +38,8 @@ const TextFilter: React.FC = (props) => { {name} setsearch(value)} endAdornment={