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

fix(ClusterRules): Open rows memorization #251

Merged
merged 6 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 10 additions & 5 deletions src/Components/ClusterRules/ClusterRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,15 @@ const ClusterRules = ({ cluster }) => {
const { isError, isUninitialized, isFetching, isSuccess, data, error } =
cluster;
const reports = data?.report?.data || [];

const [filteredRows, setFilteredRows] = useState([]);
const [displayedRows, setDisplayedRows] = useState([]);
const [isAllExpanded, setIsAllExpanded] = useState(false);
const [expandFirst, setExpandFirst] = useState(true);
const [firstRule, setFirstRule] = useState(''); // show a particular rule first
const results = filteredRows.length;
const { search } = useLocation();
// helps to distinguish the state when the API data received but not yet filtered
const [rowsFiltered, setRowsFiltered] = useState(false);
const [recordExpanded, setRecordExpanded] = useState([]);
const loadingState = isUninitialized || isFetching || !rowsFiltered;
const errorState = isError;
const successState = isSuccess;
Expand Down Expand Up @@ -116,6 +115,9 @@ const ClusterRules = ({ cluster }) => {
const handleOnCollapse = (_e, rowId, isOpen) => {
const collapseRows = [...displayedRows];
collapseRows[rowId] = { ...collapseRows[rowId], isOpen };
if (!recordExpanded.includes(collapseRows[rowId].rule.rule_id)) {
setRecordExpanded([...recordExpanded, collapseRows[rowId].rule.rule_id]);
}
setDisplayedRows(collapseRows);
};

Expand All @@ -125,7 +127,11 @@ const ClusterRules = ({ cluster }) => {
.map((value, key) => [
{
rule: value,
isOpen: isAllExpanded,
isOpen: isAllExpanded
? true
: value?.rule_id === recordExpanded.includes(value?.rule_id)
? true
: false,
Fewwy marked this conversation as resolved.
Show resolved Hide resolved
cells: [
{
title: (
Expand Down Expand Up @@ -218,7 +224,7 @@ const ClusterRules = ({ cluster }) => {
}
return sortingRows.flatMap((row, index) => {
const updatedRow = [...row];
if (expandFirst && index === 0) {
if (recordExpanded.includes(row[0].rule.rule_id)) {
row[0].isOpen = true;
}
row[1].parent = index * 2;
Expand All @@ -243,7 +249,6 @@ const ClusterRules = ({ cluster }) => {

// TODO: update URL when filters changed
const addFilterParam = (param, values) => {
setExpandFirst(false);
setFirstRule('');
return values.length > 0
? updateFilters({ ...filters, offset: 0, ...{ [param]: values } })
Expand Down
14 changes: 12 additions & 2 deletions src/Components/ClusterRules/ClusterRules.spec.ct.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ describe('cluster rules table', () => {
});

describe('defaults', () => {
it('only first item expanded', () => {
Fewwy marked this conversation as resolved.
Show resolved Hide resolved
it('no expanded rows', () => {
cy.get('#expanded-content1').should('have.length', 1);
cy.get(EXPANDABLES).should('have.length', 1);
cy.get(EXPANDABLES).should('have.length', 0);
});
it('no chips are displayed by default', () => {
cy.get(CHIP_GROUP).should('not.exist');
Expand All @@ -207,6 +207,16 @@ describe('cluster rules table', () => {
cy.get(EXPANDABLES).should('have.length', 0);
});

it.only('expand one row then sort', () => {
Fewwy marked this conversation as resolved.
Show resolved Hide resolved
cy.get(ROOT).find('#expandable-toggle0').click();
cy.get(ROOT)
.find('th[data-label=Description]')
.find('button')
.click()
.click();
cy.get('table').find('tr').find('td[id="expanded-content11"]');
Fewwy marked this conversation as resolved.
Show resolved Hide resolved
});

describe('sorting', () => {
// all tables must preserve original ordering
_.zip(['description', 'created_at', 'total_risk'], TABLE_HEADERS).forEach(
Expand Down