Created
October 14, 2021 07:43
-
-
Save akkikumar72/e825450bf20e6ae5033e79bc13e38eca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useState } from 'react'; | |
import { useSelector } from 'react-redux'; | |
import { getUserName } from '../../../../../utils/user-info'; | |
// Virt doesn't have any group | |
const checkIfVirtHasGroup = (virtGroups, deploymentID) => { | |
const findMatchingGroupAsPerVirt = virtGroups.filter((p) => p.deployments.includes(Number(deploymentID))); | |
return findMatchingGroupAsPerVirt.length === 0; | |
}; | |
const checkForNormalUser = (virtGroups) => { | |
return virtGroups?.every((p) => p.users.includes(getUserName())); | |
}; | |
// this case when virt belongs to logged-in user but its grouped and that group doesn't belong to user. | |
const checkIfVirtAssignedGroupHasUser = (virtGroups, deploymentID) => { | |
const findMatchingGroupAsPerVirt = virtGroups?.filter((p) => p.deployments.includes(Number(deploymentID))); | |
return findMatchingGroupAsPerVirt?.some(value => value.users.includes(getUserName())); | |
} | |
const checkIfUserIdFromVirtIsSameAsLoggedIn = (userId) => userId === getUserName(); | |
const useDisableTableAction = (userId, deploymentID) => { | |
const [showDisableAction, setShowDisableAction] = useState(false); | |
const { virtGroups } = useSelector((state) => state.virtGroups); | |
const { isAdmin } = useSelector((state) => state.userInfo); | |
useEffect(() => { | |
// For Admin, its all active | |
if (isAdmin) { | |
setShowDisableAction(false); | |
} else { | |
// For virt who doesn't have any group | |
// It all active too | |
if (checkIfVirtHasGroup(virtGroups, deploymentID)) { | |
setShowDisableAction(false); | |
// For virt which has groups assigned and are owner of virt too | |
} else if(!checkIfVirtHasGroup(virtGroups, deploymentID) && checkIfUserIdFromVirtIsSameAsLoggedIn(userId)) { | |
setShowDisableAction(!checkIfVirtAssignedGroupHasUser(virtGroups,deploymentID)) | |
} | |
// For Virt which has group assigned and are not owner but member of group | |
else if(!checkIfVirtHasGroup(virtGroups, deploymentID) && checkIfVirtAssignedGroupHasUser(virtGroups,deploymentID)){ | |
setShowDisableAction(false) | |
} | |
// For normal users | |
else if(checkForNormalUser(virtGroups)) { | |
setShowDisableAction(false); | |
} | |
// Otherwise disabled | |
else { | |
setShowDisableAction(true); | |
} | |
} | |
}, [deploymentID, userId, virtGroups]); | |
return showDisableAction; | |
}; | |
export default useDisableTableAction; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment