Skip to content

Commit

Permalink
clean up setState
Browse files Browse the repository at this point in the history
  • Loading branch information
Corbin Robb authored and Corbin Robb committed Feb 11, 2022
1 parent 63ba054 commit dda6bbf
Showing 1 changed file with 25 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,30 +296,31 @@ export default class FilterableTable extends PureComponent<
sortBy: string;
sortDirection: SortDirectionType;
}) {
this.setState(
({
sortBy: prevSortBy,
sortDirection: prevSortDirection,
displayedList: prevDisplayedList,
}) => {
const shouldClearSort =
prevSortDirection === SortDirection.DESC && prevSortBy === sortBy;
if (shouldClearSort)
return {
sortBy: undefined,
sortDirection: undefined,
displayedList: [...this.list],
};

return {
sortBy,
sortDirection,
displayedList: prevDisplayedList.sort(
this.sortResults(sortBy, sortDirection === SortDirection.DESC),
),
};
},
);
let updatedState: FilterableTableState;

const shouldClearSort =
this.state.sortDirection === SortDirection.DESC &&
this.state.sortBy === sortBy;

if (shouldClearSort) {
updatedState = {
...this.state,
sortBy: undefined,
sortDirection: undefined,
displayedList: [...this.list],
};
} else {
updatedState = {
...this.state,
sortBy,
sortDirection,
displayedList: [...this.list].sort(
this.sortResults(sortBy, sortDirection === SortDirection.DESC),
),
};
}

this.setState(updatedState);
}

fitTableToWidthIfNeeded() {
Expand Down

0 comments on commit dda6bbf

Please sign in to comment.