-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨(frontend) add OpenEdx profile page
We want to display OpenEdx read-only informations about the user. To edit theses informations, a link open a new tab with OpenEdx profile form page.
- Loading branch information
1 parent
c2763af
commit c39d8a9
Showing
20 changed files
with
875 additions
and
34 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 |
---|---|---|
|
@@ -6,6 +6,12 @@ import { UnknownEnrollment, OpenEdXEnrollment } from 'types'; | |
import { location } from 'utils/indirection/window'; | ||
import { CURRENT_JOANIE_DEV_DEMO_USER, RICHIE_USER_TOKEN } from 'settings'; | ||
import { base64Decode } from 'utils/base64Parser'; | ||
import { | ||
OpenEdxGender, | ||
OpenEdxLanguageIsoCode, | ||
OpenEdxLevelOfEducation, | ||
OpenEdxApiProfile, | ||
} from 'types/openEdx'; | ||
|
||
type JWTPayload = { | ||
email: string; | ||
|
@@ -89,6 +95,22 @@ const API = (APIConf: LMSBackend | AuthenticationBackend): APILms => { | |
localStorage.removeItem(RICHIE_DUMMY_IS_LOGGED_IN); | ||
}, | ||
accessToken: () => sessionStorage.getItem(RICHIE_USER_TOKEN), | ||
account: { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
get: (username: string): Promise<OpenEdxApiProfile> => { | ||
return Promise.resolve({ | ||
username: 'j_do', | ||
name: 'John Do', | ||
email: '[email protected]', | ||
country: 'fr', | ||
level_of_education: OpenEdxLevelOfEducation.MASTER_OR_PROFESSIONNAL_DEGREE, | ||
gender: OpenEdxGender.MALE, | ||
year_of_birth: '1971', | ||
'pref-lang': OpenEdxLanguageIsoCode.ENGLISH, | ||
language_proficiencies: [{ code: OpenEdxLanguageIsoCode.ENGLISH }], | ||
} as OpenEdxApiProfile); | ||
}, | ||
}, | ||
}, | ||
enrollment: { | ||
get: async (url: string, user: Nullable<User>) => | ||
|
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 { useCallback, useState } from 'react'; | ||
import { defineMessages, useIntl } from 'react-intl'; | ||
import { AuthenticationApi } from 'api/authentication'; | ||
import { useSessionQuery } from 'utils/react-query/useSessionQuery'; | ||
import { OpenEdxProfile, parseOpenEdxApiProfile } from './utils'; | ||
|
||
const messages = defineMessages({ | ||
errorGet: { | ||
id: 'hooks.useOpenEdxProfile.errorGet', | ||
description: 'Error message shown to the user when openEdx profile fetch request fails.', | ||
defaultMessage: 'An error occurred while fetching your profile. Please retry later.', | ||
}, | ||
}); | ||
|
||
interface UseOpenEdxProfileProps { | ||
username: string; | ||
} | ||
|
||
const useOpenEdxProfile = ({ username }: UseOpenEdxProfileProps) => { | ||
if (!AuthenticationApi) { | ||
throw new Error('AuthenticationApi is not defined'); | ||
} | ||
|
||
if (!AuthenticationApi!.account) { | ||
throw new Error('Current AuthenticationApi does not support account request'); | ||
} | ||
|
||
const intl = useIntl(); | ||
const [error, setError] = useState<string>(); | ||
|
||
const queryFn: () => Promise<OpenEdxProfile> = useCallback(async () => { | ||
try { | ||
const openEdxApiProfile = await AuthenticationApi!.account!.get(username); | ||
return parseOpenEdxApiProfile(intl, openEdxApiProfile); | ||
} catch { | ||
setError(intl.formatMessage(messages.errorGet)); | ||
} | ||
return Promise.reject(); | ||
}, [username, AuthenticationApi]); | ||
|
||
const [queryHandler] = useSessionQuery<OpenEdxProfile>(['open-edx-profile'], queryFn); | ||
return { data: queryHandler.data, error }; | ||
}; | ||
|
||
export default useOpenEdxProfile; |
69 changes: 69 additions & 0 deletions
69
src/frontend/js/hooks/useOpenEdxProfile/utils/index.spec.ts
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,69 @@ | ||
import { createIntl } from 'react-intl'; | ||
import { faker } from '@faker-js/faker'; | ||
import { OpenEdxApiProfileFactory } from 'utils/test/factories/openEdx'; | ||
import { OpenEdxGender, OpenEdxLanguageIsoCode, OpenEdxLevelOfEducation } from 'types/openEdx'; | ||
import { parseOpenEdxApiProfile } from '.'; | ||
|
||
describe('useOpenEdxProfile > utils', () => { | ||
it('parseOpenEdxApiProfile should format values', () => { | ||
const profile = parseOpenEdxApiProfile( | ||
createIntl({ locale: 'en' }), | ||
OpenEdxApiProfileFactory({ | ||
username: 'John', | ||
name: 'Do', | ||
email: '[email protected]', | ||
'pref-lang': OpenEdxLanguageIsoCode.FRENCH, | ||
country: 'fr', | ||
level_of_education: OpenEdxLevelOfEducation.MASTER_OR_PROFESSIONNAL_DEGREE, | ||
gender: OpenEdxGender.MALE, | ||
year_of_birth: '01/01/1970', | ||
language_proficiencies: [{ code: OpenEdxLanguageIsoCode.ENGLISH }], | ||
date_joined: '01/01/1970', | ||
}).one(), | ||
); | ||
|
||
expect(profile).toStrictEqual({ | ||
username: 'John', | ||
name: 'Do', | ||
email: '[email protected]', | ||
language: 'French', | ||
country: 'France', | ||
levelOfEducation: 'Master', | ||
gender: 'Male', | ||
yearOfBirth: '01/01/1970', | ||
favoriteLanguage: 'English', | ||
dateJoined: '01/01/1970', | ||
}); | ||
}); | ||
|
||
it('parseOpenEdxApiProfile should format default values', () => { | ||
const profile = parseOpenEdxApiProfile( | ||
createIntl({ locale: 'en' }), | ||
OpenEdxApiProfileFactory({ | ||
username: faker.internet.userName(), | ||
name: '', | ||
email: faker.internet.email(), | ||
'pref-lang': undefined, | ||
country: null, | ||
level_of_education: null, | ||
gender: null, | ||
year_of_birth: null, | ||
language_proficiencies: [], | ||
date_joined: Date.toString(), | ||
}).one(), | ||
); | ||
|
||
expect(profile).toStrictEqual({ | ||
username: profile.username, | ||
name: ' - ', | ||
email: profile.email, | ||
language: ' - ', | ||
country: ' - ', | ||
levelOfEducation: ' - ', | ||
gender: ' - ', | ||
yearOfBirth: ' - ', | ||
favoriteLanguage: ' - ', | ||
dateJoined: profile.dateJoined, | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.