Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 (rules) fix filtering in the rules page #2141

Merged
merged 4 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 24 additions & 37 deletions packages/desktop-client/src/components/ManageRules.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);

Expand All @@ -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());
Expand All @@ -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() {
Expand All @@ -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);
}
Expand All @@ -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);
},
}),
Expand Down Expand Up @@ -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);
},
}),
Expand All @@ -253,7 +241,7 @@ function ManageRulesContent({
setHoveredRule(id);
}, []);

if (rules === null) {
if (allRules.length === 0) {
return null;
}

Expand Down Expand Up @@ -290,13 +278,12 @@ function ManageRulesContent({
<Search
placeholder="Filter rules..."
value={filter}
onChange={setFilter}
onChange={onSearchChange}
/>
</View>
<View style={{ flex: 1 }}>
<RulesHeader />
<SimpleTable
data={filteredRules}
loadMore={loadMore}
// Hide the last border of the item in the table
style={{ marginBottom: -1 }}
Expand Down
28 changes: 11 additions & 17 deletions packages/desktop-client/src/components/rules/SimpleTable.tsx
Original file line number Diff line number Diff line change
@@ -1,43 +1,37 @@
import React, { type ReactNode, useEffect, useRef } from 'react';
import React, { type ReactNode, type UIEvent, useRef } from 'react';

import { type CSSProperties } from '../../style';
import View from '../common/View';

type SimpleTableProps = {
data: unknown;
loadMore?: () => void;
style?: CSSProperties;
onHoverLeave?: () => void;
children: ReactNode;
};

export default function SimpleTable({
data,
loadMore,
style,
onHoverLeave,
children,
}: SimpleTableProps) {
const contentRef = useRef<HTMLDivElement>();
const contentHeight = useRef<number>();
const scrollRef = useRef<HTMLDivElement>();

function onScroll(e) {
if (contentHeight.current != null) {
if (loadMore && e.target.scrollTop > contentHeight.current - 750) {
loadMore();
}
function onScroll(e: UIEvent<HTMLElement>) {
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 (
<View
style={{
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/2141.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [MatissJanis, jasonmichalski]
---

Fix filtering in rules page: apply the filter on the full data set instead of the limited (paginated) data set.
Loading