Skip to content

Commit

Permalink
Prefer setState over modifying state directly
Browse files Browse the repository at this point in the history
Also fix collection type. JS Array has no `size` property.
  • Loading branch information
maiwald committed Nov 5, 2024
1 parent 7e02ed6 commit ddec9b3
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions app/packs/src/apps/mydb/elements/list/ElementsList.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ export default class ElementsList extends React.Component {

onChangeUI(state) {
const { totalCheckedElements } = this.state;
let forceUpdate = false;
// const genericNames = (genericEls && genericEls.map(el => el.name)) || [];
let genericKlasses = [];
const currentUser = (UserStore.getState() && UserStore.getState().currentUser) || {};
Expand All @@ -138,22 +137,25 @@ export default class ElementsList extends React.Component {
}
const elNames = ['sample', 'reaction', 'screen', 'wellplate', 'research_plan', 'cell_line'].concat(genericKlasses);

const newTotalCheckedElements = {};
let needsUpdate = false;
elNames.forEach((type) => {
const elementUI = state[type] || {
checkedAll: false, checkedIds: [], uncheckedIds: [], currentId: null
checkedAll: false,
checkedIds: Immutable.List(),
uncheckedIds: Immutable.List(),
};
const element = ElementStore.getState().elements[`${type}s`];
const nextCount = elementUI.checkedAll
? (element.totalElements - elementUI.uncheckedIds.size)
: elementUI.checkedIds.size;
if (!forceUpdate && nextCount !== (totalCheckedElements[type] || 0)) { forceUpdate = true; }
totalCheckedElements[type] = nextCount;
needsUpdate = needsUpdate || nextCount !== totalCheckedElements[type];
newTotalCheckedElements[type] = nextCount
});

this.setState((previousState) => ({ ...previousState, totalCheckedElements }));
// could not use shouldComponentUpdate because state.totalCheckedElements
// has already changed independently of setstate
if (forceUpdate) { this.forceUpdate(); }
if (needsUpdate) {
this.setState({ totalCheckedElements: newTotalCheckedElements });
}
}

handleRemoveSearchResult(searchStore) {
Expand Down

0 comments on commit ddec9b3

Please sign in to comment.