-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: auto setting active linked enterprise
- Loading branch information
1 parent
c67c5e4
commit 66066a3
Showing
4 changed files
with
83 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { default as useEnterpriseCuration } from './useEnterpriseCuration'; | ||
export { default as useEnterpriseCurationContext } from './useEnterpriseCurationContext'; | ||
export { default as useUpdateActiveEnterpriseForUser } from './useUpdateActiveEnterpriseForUser'; |
45 changes: 45 additions & 0 deletions
45
src/components/EnterpriseApp/data/hooks/useUpdateActiveEnterpriseForUser.js
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 { useState, useEffect, useCallback } from 'react'; | ||
import { logError } from '@edx/frontend-platform/logging'; | ||
|
||
import LmsApiService from '../../../../data/services/LmsApiService'; | ||
|
||
const useUpdateActiveEnterpriseForUser = ({ | ||
enterpriseId, | ||
user, | ||
}) => { | ||
const [isLoading, setIsLoading] = useState(false); | ||
|
||
const updateActiveEnterpriseAndRefreshJWT = useCallback(async () => { | ||
setIsLoading(true); | ||
try { | ||
await LmsApiService.updateUserActiveEnterprise(enterpriseId); | ||
await LmsApiService.loginRefresh(); | ||
} catch (error) { | ||
logError(error); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}, [enterpriseId]); | ||
|
||
useEffect(() => { | ||
if (!(enterpriseId && user)) { | ||
return; | ||
} | ||
|
||
const { roles } = user; | ||
// The first learner role corresponds to the currently active enterprise for the user | ||
const activeLearnerRole = roles.find(role => role.split(':')[0] === 'enterprise_learner'); | ||
|
||
if (activeLearnerRole) { | ||
const currentActiveEnterpriseId = activeLearnerRole.split(':')[1]; | ||
if (currentActiveEnterpriseId !== '*' && currentActiveEnterpriseId !== enterpriseId) { | ||
updateActiveEnterpriseAndRefreshJWT(); | ||
} | ||
} | ||
}, [enterpriseId, user, updateActiveEnterpriseAndRefreshJWT]); | ||
return { | ||
isLoading, | ||
}; | ||
}; | ||
|
||
export default useUpdateActiveEnterpriseForUser; |
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