Skip to content
Closed
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
20 changes: 16 additions & 4 deletions app/views/SidebarView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class Sidebar extends Component {
super(props);
this.state = {
showStatus: false,
isAdmin: false
isAdmin: false,
isSetAdminNeedCall: true
};
}

Expand All @@ -68,10 +69,11 @@ class Sidebar extends Component {
}

shouldComponentUpdate(nextProps, nextState) {
const { showStatus, isAdmin } = this.state;
const { showStatus, isAdmin, isSetAdminNeedCall } = this.state;
const {
Site_Name, user, baseUrl, state, isMasterDetail, useRealName, theme
} = this.props;

// Drawer navigation state
if (state?.index !== nextProps.state?.index) {
return true;
Expand Down Expand Up @@ -103,9 +105,16 @@ class Sidebar extends Component {
if (nextState.isAdmin !== isAdmin) {
return true;
}
if (nextProps.state !== state && isSetAdminNeedCall === true) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As you can see at the beginning of the block, state comes from react-navigation (it's track current state of the navigation) and it's an object.
nextProps.state !== state is an object comparison, so it'll never be equal.
image

return true;
}
return false;
}

componentDidUpdate() {
this.setIsAdmin();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not a good practice to call something on componentDidUpdate without testing in what cases it should run.
componentDidUpdate runs every time the component updates: when state changes, when props change or when the component is forced updated.

Even if shouldComponentUpdate returns false, this.setIsAdmin is being called.
Add a console.log at the start of setIsAdmin and you'll see how many times it's called.

}

async setIsAdmin() {
const db = database.active;
const { user } = this.props;
Expand All @@ -114,8 +123,11 @@ class Sidebar extends Component {
if (roles) {
const permissionsCollection = db.collections.get('permissions');
const permissionsFiltered = await permissionsCollection.query(Q.where('id', Q.oneOf(permissions))).fetch();
const isAdmin = permissionsFiltered.reduce((result, permission) => (
result || permission.roles.some(r => roles.indexOf(r) !== -1)),
const isAdmin = permissionsFiltered.reduce((result, permission) => {
this.setState({ isSetAdminNeedCall: false });

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not a good practice to change the state inside of an iteration.
It's going to set the state for every permissions on the list.
Even if sCU prevents the re-render to happen, you're calling unnecessary resources.

return (
result || permission.roles.some(r => roles.indexOf(r) !== -1));
},
false);
this.setState({ isAdmin });
}
Expand Down