Skip to content

Commit

Permalink
If a filter term is deleted, remove the category from the filter stat…
Browse files Browse the repository at this point in the history
…e instead of keeping it stored as an undefined value

Signed-off-by: Kristen Armes <karmes@lyft.com>
  • Loading branch information
kristenarmes committed Dec 22, 2021
1 parent 7467d0c commit b84387a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
2 changes: 1 addition & 1 deletion frontend/amundsen_application/static/.betterer.results
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ exports[`eslint`] = {
"js/pages/SearchPage/SearchFilter/FilterSection/index.tsx:2051916132": [
[20, 75, -647, "Expected to return a value at the end of arrow function.", "5381"]
],
"js/pages/SearchPage/SearchFilter/InputFilter/index.tsx:1977761678": [
"js/pages/SearchPage/SearchFilter/InputFilter/index.tsx:3333995805": [
[63, 6, 13, "Do not use setState in componentDidUpdate", "57229240"]
],
"js/pages/SearchPage/SearchPanel/tests/index.spec.tsx:3928473928": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,36 @@ export class InputFilter extends React.Component<
const showFilterOperationToggle = newValue.includes(',');
this.setState({ value: newValue, showFilterOperationToggle });

const currentFilter = getFilterObject(
filterState,
resourceType,
categoryId
);
const hasFilterOperation = currentFilter && currentFilter.filterOperation;
const newFilter = hasFilterOperation
? {
value: newValue || undefined,
filterOperation: currentFilter.filterOperation,
}
: { value: newValue || undefined };
let newFilters;
if (newValue) {
const currentFilter = getFilterObject(
filterState,
resourceType,
categoryId
);
const hasFilterOperation = currentFilter && currentFilter.filterOperation;
const newFilter = hasFilterOperation
? { value: newValue, filterOperation: currentFilter.filterOperation }
: { value: newValue };

newFilters = {
...filterState,
[resourceType]: {
...filterState[resourceType],
[categoryId]: newFilter,
},
};
} else {
// Remove the categoryId from the filters if the new value is empty
const updatedResourceFilters = { ...filterState[resourceType] };
delete updatedResourceFilters[categoryId];

newFilters = {
...filterState,
[resourceType]: updatedResourceFilters,
};
}

This comment has been minimized.

Copy link
@Golodhros

Golodhros Dec 22, 2021

Member

Another way of doing this on an immutable way would be:

      // Remove the categoryId from the filters if the new value is empty
      const {[categoryId]: remove, ...updatedResourceFilters} = filterState[resourceType];

      newFilters = {
         ...filterState,
         [resourceType]: updatedResourceFilters,
      };

This would avoid the 'delete', that is a mutating operation (although made on a copy of the object)


const newFilters = {
...filterState,
[resourceType]: {
...filterState[resourceType],
[categoryId]: newFilter,
},
};
updateFilterState(newFilters);
};

Expand Down

0 comments on commit b84387a

Please sign in to comment.