Skip to content

Commit

Permalink
Merge pull request #1125 from nickgros/SWC-6800g
Browse files Browse the repository at this point in the history
  • Loading branch information
nickgros authored Aug 13, 2024
2 parents 67ff350 + 74769c4 commit 1274a96
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ export function AclEditor(props: AclEditorProps) {
return (
<Collapse key={resourceAccess.principalId}>
<ResourceAccessItem
key={resourceAccess.principalId}
resourceAccess={resourceAccess}
availablePermissionLevels={availablePermissionLevels}
canChangePermission={canChangePermission}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const PRINCIPAL_ALREADY_ADDED_ERROR_MESSAGE =
'User or team already has permissions.'

type UseUpdateAclOptions = {
initialResourceAccessList?: ResourceAccess[]
onChange?: (resourceAccessList: ResourceAccess[]) => void
onError?: (e: string) => void
}
Expand All @@ -38,14 +39,20 @@ type UseUpdateAclReturn = {
resetDirtyState: () => void
}

const EMPTY_ARRAY: ResourceAccess[] = []

export default function useUpdateAcl(
options: UseUpdateAclOptions = {},
): UseUpdateAclReturn {
const { onChange = noop, onError = noop } = options
const {
initialResourceAccessList = EMPTY_ARRAY,
onChange = noop,
onError = noop,
} = options
const [isDirty, setIsDirty] = useState(false)
const [resourceAccessList, setResourceAccessList] = useState<
ResourceAccess[]
>([])
>(initialResourceAccessList)
const [hasSorted, setHasSorted] = useState(false)

// While the form has not been edited, sort the resourceAccessList
Expand All @@ -67,6 +74,7 @@ export default function useUpdateAcl(

useEffect(() => {
onChange(resourceAccessList)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [resourceAccessList])

const addResourceAccessItem = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ const EntityAclEditor = React.forwardRef(function EntityAclEditor(
updateResourceAccessItem,
removeResourceAccessItem,
resetDirtyState,
} = useUpdateAcl()
} = useUpdateAcl({ initialResourceAccessList: originalResourceAccess })

// If `originalResourceAccess` changes, reset state
useEffect(() => {
if (originalResourceAccess) {
resetDirtyState()
setResourceAccessList(originalResourceAccess)
setResourceAccessList([...originalResourceAccess])
}
}, [originalResourceAccess, resetDirtyState, setResourceAccessList])

Expand Down

0 comments on commit 1274a96

Please sign in to comment.