This commit is contained in:
@@ -73,22 +73,25 @@ const UpdateEmployee = () => {
|
||||
return { parsePermission, viewByPairId, editsByPairId };
|
||||
}, []);
|
||||
|
||||
const enforcePermissionPairRules = (permissionList = []) => {
|
||||
const applyPairRulesForToggle = (permissionList = [], permissionKey, isChecking) => {
|
||||
const nextSet = new Set(permissionList);
|
||||
const nextList = Array.from(nextSet);
|
||||
nextList.forEach((permissionKey) => {
|
||||
const parsed = permissionPairMaps.parsePermission(permissionKey);
|
||||
if (!parsed || parsed.type !== 'edit') return;
|
||||
const parsed = permissionPairMaps.parsePermission(permissionKey);
|
||||
if (!parsed) return Array.from(nextSet);
|
||||
|
||||
// Checking an edit permission should also check its paired view permission.
|
||||
if (isChecking && parsed.type === 'edit') {
|
||||
const pairedView = permissionPairMaps.viewByPairId.get(parsed.pairId);
|
||||
if (pairedView) {
|
||||
nextSet.add(pairedView);
|
||||
}
|
||||
});
|
||||
permissionPairMaps.viewByPairId.forEach((viewPermission, pairId) => {
|
||||
if (nextSet.has(viewPermission)) return;
|
||||
const editPermissions = permissionPairMaps.editsByPairId.get(pairId) || [];
|
||||
editPermissions.forEach((editPermission) => nextSet.delete(editPermission));
|
||||
});
|
||||
}
|
||||
|
||||
// Unchecking a view permission should also uncheck its paired edit permission(s).
|
||||
if (!isChecking && parsed.type === 'view') {
|
||||
const pairedEdits = permissionPairMaps.editsByPairId.get(parsed.pairId) || [];
|
||||
pairedEdits.forEach((editPermission) => nextSet.delete(editPermission));
|
||||
}
|
||||
|
||||
return Array.from(nextSet);
|
||||
};
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
@@ -165,9 +168,10 @@ const UpdateEmployee = () => {
|
||||
if (isSuperPermissionLocked) return;
|
||||
setPermissions((prevPermissions) => {
|
||||
if (prevPermissions.includes(permissionKey)) {
|
||||
return enforcePermissionPairRules(prevPermissions.filter((permission) => permission !== permissionKey));
|
||||
const removed = prevPermissions.filter((permission) => permission !== permissionKey);
|
||||
return applyPairRulesForToggle(removed, permissionKey, false);
|
||||
}
|
||||
return enforcePermissionPairRules([...prevPermissions, permissionKey]);
|
||||
return applyPairRulesForToggle([...prevPermissions, permissionKey], permissionKey, true);
|
||||
});
|
||||
};
|
||||
|
||||
@@ -176,11 +180,22 @@ const UpdateEmployee = () => {
|
||||
|
||||
const togglePermissionGroup = (permissionItems = [], checked) => {
|
||||
if (isSuperPermissionLocked) return;
|
||||
if (checked) {
|
||||
setPermissions((prevPermissions) => enforcePermissionPairRules(Array.from(new Set([...prevPermissions, ...permissionItems]))));
|
||||
return;
|
||||
}
|
||||
setPermissions((prevPermissions) => enforcePermissionPairRules(prevPermissions.filter((permission) => !permissionItems.includes(permission))));
|
||||
setPermissions((prevPermissions) => {
|
||||
let nextPermissions = [...prevPermissions];
|
||||
|
||||
permissionItems.forEach((permissionKey) => {
|
||||
const hasPermission = nextPermissions.includes(permissionKey);
|
||||
if (checked && !hasPermission) {
|
||||
nextPermissions = applyPairRulesForToggle([...nextPermissions, permissionKey], permissionKey, true);
|
||||
return;
|
||||
}
|
||||
if (!checked && hasPermission) {
|
||||
nextPermissions = applyPairRulesForToggle(nextPermissions.filter((permission) => permission !== permissionKey), permissionKey, false);
|
||||
}
|
||||
});
|
||||
|
||||
return Array.from(new Set(nextPermissions));
|
||||
});
|
||||
};
|
||||
|
||||
const triggerShowDeleteModal = () => {
|
||||
|
||||
Reference in New Issue
Block a user