-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
82 additions
and
49 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { ISelectMenuItem } from '@/components/form/SelectFormField'; | ||
import { Roles } from '@/constants/roles'; | ||
import { useContext, useEffect, useMemo } from 'react'; | ||
import useDataLoader from '../useDataLoader'; | ||
import { AuthContext } from '@/contexts/authContext'; | ||
import usePimsApi from '../usePimsApi'; | ||
import useGroupedAgenciesApi from './useGroupedAgenciesApi'; | ||
|
||
const useUserAgencies = () => { | ||
const userContext = useContext(AuthContext); | ||
const { ungroupedAgencies, agencyOptions } = useGroupedAgenciesApi(); | ||
const api = usePimsApi(); | ||
const isAdmin = userContext.keycloak.hasRoles([Roles.ADMIN]); | ||
const { data: userAgencies, refreshData: refreshUserAgencies } = useDataLoader(() => | ||
api.users.getUsersAgencyIds(userContext.keycloak.user.preferred_username), | ||
); | ||
|
||
useEffect(() => { | ||
refreshUserAgencies(); | ||
}, [userContext.keycloak]); | ||
|
||
const userAgencyObjects = useMemo(() => { | ||
if (ungroupedAgencies && userAgencies) { | ||
return ungroupedAgencies.filter((a) => userAgencies.includes(a.Id)); | ||
} else { | ||
return []; | ||
} | ||
}, [ungroupedAgencies, userAgencies]); | ||
|
||
const menuItems: ISelectMenuItem[] = useMemo(() => { | ||
if (isAdmin) { | ||
return agencyOptions; | ||
} else if (userAgencyObjects) { | ||
return agencyOptions.filter((agc) => | ||
userAgencyObjects.some((useragc) => useragc.Id === agc.value), | ||
); | ||
} else { | ||
return []; | ||
} | ||
}, [agencyOptions, userAgencyObjects, isAdmin]); | ||
|
||
return { menuItems, userAgencies: userAgencyObjects }; | ||
}; | ||
|
||
export default useUserAgencies; |