From e0d66d30830e8dd05993b4965701f955b3e0e455 Mon Sep 17 00:00:00 2001 From: Matiss Janis Aboltins Date: Fri, 5 Jan 2024 19:06:32 +0000 Subject: [PATCH] :bug: (rules) fix filtering in the rules page (#2141) --- .../src/components/ManageRules.tsx | 61 ++++++++----------- .../src/components/rules/SimpleTable.tsx | 28 ++++----- upcoming-release-notes/2141.md | 6 ++ 3 files changed, 41 insertions(+), 54 deletions(-) create mode 100644 upcoming-release-notes/2141.md diff --git a/packages/desktop-client/src/components/ManageRules.tsx b/packages/desktop-client/src/components/ManageRules.tsx index 36928d65520..e9fe97548d6 100644 --- a/packages/desktop-client/src/components/ManageRules.tsx +++ b/packages/desktop-client/src/components/ManageRules.tsx @@ -95,8 +95,8 @@ function ManageRulesContent({ payeeId, setLoading, }: ManageRulesContentProps) { - const [allRules, setAllRules] = useState(null); - const [rules, setRules] = useState(null); + const [allRules, setAllRules] = useState([]); + const [page, setPage] = useState(0); const [filter, setFilter] = useState(''); const dispatch = useDispatch(); @@ -117,18 +117,27 @@ function ManageRulesContent({ const filteredRules = useMemo( () => - filter === '' || !rules - ? rules - : rules.filter(rule => + (filter === '' + ? allRules + : allRules.filter(rule => ruleToString(rule, filterData) .toLowerCase() .includes(filter.toLowerCase()), - ), - [rules, filter, filterData], + ) + ).slice(0, 100 + page * 50), + [allRules, filter, filterData, page], ); const selectedInst = useSelected('manage-rules', allRules, []); const [hoveredRule, setHoveredRule] = useState(null); + const onSearchChange = useCallback( + (value: string) => { + setFilter(value); + setPage(0); + }, + [setFilter], + ); + async function loadRules() { setLoading(true); @@ -147,8 +156,7 @@ function ManageRulesContent({ useEffect(() => { async function loadData() { - const loadedRules = await loadRules(); - setRules(loadedRules.slice(0, 100)); + await loadRules(); setLoading(false); await dispatch(initiallyLoadPayees()); @@ -166,7 +174,7 @@ function ManageRulesContent({ }, []); function loadMore() { - setRules(rules.concat(allRules.slice(rules.length, rules.length + 50))); + setPage(page => page + 1); } async function onDeleteSelected() { @@ -179,10 +187,7 @@ function ManageRulesContent({ alert('Some rules were not deleted because they are linked to schedules'); } - const newRules = await loadRules(); - setRules(rules => { - return newRules.slice(0, rules.length); - }); + await loadRules(); selectedInst.dispatch({ type: 'select-none' }); setLoading(false); } @@ -191,19 +196,8 @@ function ManageRulesContent({ dispatch( pushModal('edit-rule', { rule, - onSave: async newRule => { - const newRules = await loadRules(); - - setRules(rules => { - const newIdx = newRules.findIndex(rule => rule.id === newRule.id); - - if (newIdx > rules.length) { - return newRules.slice(0, newIdx + 75); - } else { - return newRules.slice(0, rules.length); - } - }); - + onSave: async () => { + await loadRules(); setLoading(false); }, }), @@ -236,13 +230,7 @@ function ManageRulesContent({ pushModal('edit-rule', { rule, onSave: async newRule => { - const newRules = await loadRules(); - - setRules(rules => { - const newIdx = newRules.findIndex(rule => rule.id === newRule.id); - return newRules.slice(0, newIdx + 75); - }); - + await loadRules(); setLoading(false); }, }), @@ -253,7 +241,7 @@ function ManageRulesContent({ setHoveredRule(id); }, []); - if (rules === null) { + if (allRules.length === 0) { return null; } @@ -290,13 +278,12 @@ function ManageRulesContent({ void; style?: CSSProperties; onHoverLeave?: () => void; @@ -12,32 +11,27 @@ type SimpleTableProps = { }; export default function SimpleTable({ - data, loadMore, style, onHoverLeave, children, }: SimpleTableProps) { const contentRef = useRef(); - const contentHeight = useRef(); const scrollRef = useRef(); - function onScroll(e) { - if (contentHeight.current != null) { - if (loadMore && e.target.scrollTop > contentHeight.current - 750) { - loadMore(); - } + function onScroll(e: UIEvent) { + if ( + loadMore && + Math.abs( + e.currentTarget.scrollHeight - + e.currentTarget.clientHeight - + e.currentTarget.scrollTop, + ) < 1 + ) { + loadMore(); } } - useEffect(() => { - if (contentRef.current) { - contentHeight.current = contentRef.current.getBoundingClientRect().height; - } else { - contentHeight.current = null; - } - }, [contentRef.current, data]); - return (