JSFiddle - React, Tailwind, and code Playground
by igalst
JavaScript
if (prevProps === this.props || !this.props.currentVisitSummary || this.state.isOnlyOneSection) return;
const ALL_KEYS = ['drugs', 'referrals', 'approvals', 'tutorials'];
const map = Object.fromEntries(ALL_KEYS.map(key => [key, this.checkIsExists(key)]));
const isAllFalseExcept = key => map[key] && !Object.entries(map).filter(([innerKey]) => innerKey !== key).find(([_, value]) => value)
/* isAllFalseExcept('drugs') && this.setState({ drugsIsOpen: true, isOnlyOneSection: true });
isAllFalseExcept('referrals') && this.setState({ referralsIsOpen: true, isOnlyOneSection: true }); */
// .....
ALL_KEYS.forEach(key => isAllFalseExcept(key) && this.setState({ [`${key}IsOpen`]: true, isOnlyOneSection: true }))
/* let drugs = this.checkIsExists('drugs');
let referrals = this.checkIsExists('referrals');
let approvals = this.checkIsExists('approvals');
let tutorials = this.checkIsExists('tutorials');
drugs && !referrals && !approvals && !tutorials && this.setState({ drugsIsOpen: true, isOnlyOneSection: true });
!drugs && referrals && !approvals && !tutorials && this.setState({ referralsIsOpen: true, isOnlyOneSection: true });
!drugs && !referrals && approvals && !tutorials && this.setState({ approvalsIsOpen: true, isOnlyOneSection: true });
!drugs && !referrals && !approvals && tutorials && this.setState({ tutorialsInfoIsOpen: true, isOnlyOneSection: true }); */
}
checkIsExists(key) {
const { currentVisitSummary = [] } = this.props;
return currentVisitSummary[key] && currentVisitSummary[key].length;
}