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 search toolbar clear all filters #11939

Merged
merged 1 commit into from
Mar 31, 2022
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
4 changes: 1 addition & 3 deletions awx/ui/src/components/DataListToolbar/DataListToolbar.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useEffect, useMemo, useState } from 'react';
import PropTypes from 'prop-types';
import styled from 'styled-components';
import { useLocation } from 'react-router-dom';
import { t } from '@lingui/macro';
import {
Button,
Expand Down Expand Up @@ -58,7 +57,6 @@ function DataListToolbar({
handleIsAnsibleFactsSelected,
isFilterCleared,
}) {
const { search } = useLocation();
const showExpandCollapse = onCompact && onExpand;
const [isKebabOpen, setIsKebabOpen] = useState(false);
const [isKebabModalOpen, setIsKebabModalOpen] = useState(false);
Expand Down Expand Up @@ -93,7 +91,7 @@ function DataListToolbar({
ouiaId={`${qsConfig.namespace}-list-toolbar`}
clearAllFilters={clearAllFilters}
collapseListedFiltersBreakpoint="lg"
clearFiltersButtonText={Boolean(search) && t`Clear all filters`}
clearFiltersButtonText={t`Clear all filters`}
>
<ToolbarContent>
{onExpandAll && (
Expand Down
8 changes: 0 additions & 8 deletions awx/ui/src/components/DataListToolbar/DataListToolbar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@ import { mountWithContexts } from '../../../testUtils/enzymeHelpers';
import DataListToolbar from './DataListToolbar';
import AddDropDownButton from '../AddDropDownButton/AddDropDownButton';

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useLocation: () => ({
pathname: '/organizations',
search: 'template.name__icontains=name',
}),
}));

describe('<DataListToolbar />', () => {
let toolbar;

Expand Down
34 changes: 22 additions & 12 deletions awx/ui/src/components/Search/Search.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'styled-components/macro';
import React, { useState } from 'react';
import React, { useState, useEffect } from 'react';
import PropTypes from 'prop-types';

import { t } from '@lingui/macro';
Expand Down Expand Up @@ -65,6 +65,26 @@ function Search({
const [searchValue, setSearchValue] = useState('');
const [isFilterDropdownOpen, setIsFilterDropdownOpen] = useState(false);

const params = parseQueryString(qsConfig, location.search);
if (params?.host_filter) {
params.ansible_facts = params.host_filter.substring(
'ansible_facts__'.length
);
delete params.host_filter;
}

const searchChips = getChipsByKey(params, columns, qsConfig);
const [chipsByKey, setChipsByKey] = useState(
JSON.parse(JSON.stringify(searchChips))
);
Comment on lines +76 to +79
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const searchChips = getChipsByKey(params, columns, qsConfig);
const [chipsByKey, setChipsByKey] = useState(
JSON.parse(JSON.stringify(searchChips))
);
const [chipsByKey, setChipsByKey] = useState(
getChipsByKey(params, columns, qsConfig)
);


useEffect(() => {
Object.keys(chipsByKey).forEach((el) => {
chipsByKey[el].chips = [];
});
setChipsByKey({ ...chipsByKey, ...searchChips });
}, [location.search]); // eslint-disable-line react-hooks/exhaustive-deps

const handleDropdownSelect = ({ target }) => {
const { key: actualSearchKey } = columns.find(
({ name }) => name === target.innerText
Expand Down Expand Up @@ -98,15 +118,6 @@ function Search({
}
};

const params = parseQueryString(qsConfig, location.search);
if (params?.host_filter) {
params.ansible_facts = params.host_filter.substring(
'ansible_facts__'.length
);
delete params.host_filter;
}
const chipsByKey = getChipsByKey(params, columns, qsConfig);

const { name: searchColumnName } = columns.find(
({ key }) => key === searchKey
);
Expand Down Expand Up @@ -179,7 +190,7 @@ function Search({
onSelect={(event, selection) =>
handleFilterDropdownSelect(key, event, selection)
}
selections={chipsByKey[key].chips.map((chip) => {
selections={chipsByKey[key]?.chips.map((chip) => {
const [, ...value] = chip.key.split(':');
return value.join(':');
})}
Expand Down Expand Up @@ -258,7 +269,6 @@ function Search({
{/* Add a ToolbarFilter for any key that doesn't have it's own
search column so the chips show up */}
{Object.keys(chipsByKey)
.filter((val) => chipsByKey[val].chips.length > 0)
.filter((val) => columns.map((val2) => val2.key).indexOf(val) === -1)
.map((leftoverKey) => (
<ToolbarFilter
Expand Down