Skip to content

Commit

Permalink
Eliminate duplicate fetches, functional component listens to props
Browse files Browse the repository at this point in the history
  • Loading branch information
AllenBW committed Aug 5, 2019
1 parent e549b64 commit 41b0890
Showing 1 changed file with 5 additions and 23 deletions.
28 changes: 5 additions & 23 deletions src/PresentationalComponents/RulesTable/RulesTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,10 @@ const RulesTable = (props) => {
5: 'playbook_count'
};
const orderParam = `${direction === 'asc' ? '' : '-'}${attrIndex[index]}`;

setSortBy({ index, direction });
setSort(orderParam);
setOffset(0);

fetchRules({
...filters,
offset: 0,
limit,
impacting,
sort: orderParam
});
}, [fetchRules, filters, impacting, limit]);
}, [setSort, setSortBy, setOffset]);

const onSetPage = (_event, pageNumber) => {
const newOffset = pageNumber * limit - limit;
Expand All @@ -73,19 +64,12 @@ const RulesTable = (props) => {

const onPerPageSelect = (_event, limit) => {
setLimit(limit);
fetchRules({ ...filters, offset, limit, impacting, sort });
};

const toggleRulesWithHits = (impacting) => {
setFilters({ ...filters, impacting });
setImpacting(impacting);
setOffset(0);
fetchRules({
...filters,
offset: 0,
limit,
impacting
});
};

const handleOnCollapse = (event, rowId, isOpen) => {
Expand Down Expand Up @@ -137,13 +121,11 @@ const RulesTable = (props) => {
}];
};

const fetchAction = useCallback((filters) => {
const fetchAction = useCallback(() => {
setOffset(0);
fetchRules({ ...filters, limit, offset: 0, impacting, sort });
}, [fetchRules, impacting, limit, sort]);
}, []);

useEffect(() => {
setOffset(0);
fetchRules({
...filters,
offset,
Expand All @@ -157,7 +139,7 @@ const RulesTable = (props) => {
if (!rows.length) {
onSort(null, 2, 'desc');
}
}, [onSort, rows]);
}, [onSort, rows.length]);

useEffect(() => {
if (rules.data) {
Expand All @@ -170,7 +152,7 @@ const RulesTable = (props) => {
${ filters.reports_shown ? 'enabled rules.' : 'any known rules.'}`}>
{filters.reports_shown && <Button variant="link" style={{ paddingTop: 24 }} onClick={() => {
setFilters({ ...filters, reports_shown: undefined });
fetchAction({ ...filters, reports_shown: undefined });
fetchAction();
}}>
Include disabled rules
</Button>}
Expand Down

0 comments on commit 41b0890

Please sign in to comment.