我有一组表示用户权限的切换。以下是切换列表:
Regular permissions
[x] writeArticles
Administrative permissions
[x] accessDash
[ ] manageBlog
[ ] manageSpecialPages
[x] manageAccounts
[ ] manageAccountPermissions
事情是他们彼此依赖,或者......其中一些,至少,以下列方式:
manageAccountPermissions
为on
,manageAccounts
将为on
accessDash
将为on
accessDash
已启用,则writeArticles
将为on
。现在事情是相互有效的,所以:
manageAccounts
关闭,则manageAccountPermissions
关闭accessDash
关闭,则所有管理权限都将关闭writeArticles
关闭,则accessDash
会关闭(隐式地,所有管理权限都会关闭)到目前为止,这是我的代码。这是完全废话,似乎只适用于某些情况,而且就我看来也很慢:
handleToggle = ( event, toggled ) => {
const targetId = event.target.id;
const { permissions } = this.state;
let newPermissions = {
...permissions,
[targetId]: toggled,
};
if ( newPermissions.writeArticles === false ) {
newPermissions.accessDash = false;
}
const activeAdminPerm =
filter( newPermissions, ( o, k ) => o === true && k !== 'writeArticles' );
if ( Object.size( activeAdminPerm ) &&
targetId !== 'accessDash' &&
targetId !== 'writeArticles' ) {
newPermissions.writeArticles = true;
newPermissions.accessDash = true;
}
if ( newPermissions.accessDash === false || !newPermissions.writeArticles ) {
for ( let key in permissions ) {
if ( permissions.hasOwnProperty( key ) ) {
if ( key !== 'writeArticles' ) {
newPermissions[key] = false;
}
}
}
}
this.setState( {
permissions: newPermissions,
} );
};
这里最简单,最合乎逻辑的方法是什么?非常感谢你!
我写了a pen来说明上面的代码(几乎没有改编)