-
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
52663b8
commit 7bfeff5
Showing
13 changed files
with
529 additions
and
1 deletion.
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,7 @@ 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 { Gender, LanguageIsoCode, LevelOfEducation, OpenEdxApiProfile } from 'types/openEdx'; | ||
|
||
type JWTPayload = { | ||
email: string; | ||
|
@@ -89,6 +90,21 @@ 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) => { | ||
return Promise.resolve({ | ||
username: 'j_do', | ||
name: 'John Do', | ||
email: '[email protected]', | ||
country: 'fr', | ||
level_of_education: LevelOfEducation.MASTER_OR_PROFESSIONNAL_DEGREE, | ||
gender: Gender.MALE, | ||
year_of_birth: '1971', | ||
language_proficiencies: [{ code: LanguageIsoCode.ENGLISH }], | ||
} as unknown 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,8 @@ | |
flex-direction: column; | ||
} | ||
} | ||
|
||
&-footer { | ||
margin-top: $vertical-spacing; | ||
} | ||
} |
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 do 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]); | ||
|
||
const [queryHandler] = useSessionQuery<OpenEdxProfile>(['open-edx-profile'], queryFn); | ||
return { data: queryHandler.data, error }; | ||
}; | ||
|
||
export default useOpenEdxProfile; |
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,129 @@ | ||
import { IntlShape, defineMessages } from 'react-intl'; | ||
import countries from 'i18n-iso-countries'; | ||
import { Gender, LevelOfEducation, OpenEdxApiProfile } from 'types/openEdx'; | ||
import { Maybe } from 'types/utils'; | ||
|
||
const levelOfEducationMessages = defineMessages<LevelOfEducation>({ | ||
[LevelOfEducation.MASTER_OR_PROFESSIONNAL_DEGREE]: { | ||
id: 'openEdxProfile.levelOfEducation.masterOrProfessionnalDegree', | ||
description: | ||
'Translation for level of education "master or professional degree" in openEdx profile', | ||
defaultMessage: 'Master', | ||
}, | ||
[LevelOfEducation.PHD_OR_DOCTORATE]: { | ||
id: 'openEdxProfile.levelOfEducation.phdOrDoctorate', | ||
description: 'Translation for level of education "phd or doctorate" in openEdx profile', | ||
defaultMessage: 'PHD', | ||
}, | ||
[LevelOfEducation.BACHELOR_DEGREE]: { | ||
id: 'openEdxProfile.levelOfEducation.bachelorDegree', | ||
description: 'Translation for level of education "bachelor degree" in openEdx profile', | ||
defaultMessage: 'Bachelor degree', | ||
}, | ||
[LevelOfEducation.ASSOCIATE_DEGREE]: { | ||
id: 'openEdxProfile.levelOfEducation.associateDegree', | ||
description: 'Translation for level of education "associate degree" in openEdx profile', | ||
defaultMessage: 'Associate degree', | ||
}, | ||
[LevelOfEducation.SECONDARY_OR_HIGH_SCHOOL]: { | ||
id: 'openEdxProfile.levelOfEducation.secondaryOrHighSchool', | ||
description: 'Translation for level of education "secondary or high school" in openEdx profile', | ||
defaultMessage: 'Secondary or high school', | ||
}, | ||
[LevelOfEducation.JUNIOR_SECONDARY_OR_MIDDLE_SCHOOL]: { | ||
id: 'openEdxProfile.levelOfEducation.juniorSecondaryOrMiddleSchool', | ||
description: | ||
'Translation for level of education "junior secondary or middle school" in openEdx profile', | ||
defaultMessage: 'Junior secondary or middle school', | ||
}, | ||
[LevelOfEducation.ELEMENTARY_PRIMARY_SCHOOL]: { | ||
id: 'openEdxProfile.levelOfEducation.elementaryPrimarySchool', | ||
description: | ||
'Translation for level of education "elementary primary school" in openEdx profile', | ||
defaultMessage: 'Elementary primary school', | ||
}, | ||
[LevelOfEducation.NONE]: { | ||
id: 'openEdxProfile.levelOfEducation.none', | ||
description: 'Translation for level of education "none" in openEdx profile', | ||
defaultMessage: 'None', | ||
}, | ||
[LevelOfEducation.OTHER]: { | ||
id: 'openEdxProfile.levelOfEducation.other', | ||
description: 'Translation for level of education "other" in openEdx profile', | ||
defaultMessage: 'Other', | ||
}, | ||
}); | ||
|
||
const genderMessages = defineMessages<Gender>({ | ||
[Gender.MALE]: { | ||
id: 'openEdxProfile.gender.male', | ||
description: 'Translation for gender "male" in openEdx profile', | ||
defaultMessage: 'Male', | ||
}, | ||
[Gender.FEMALE]: { | ||
id: 'openEdxProfile.gender.female', | ||
description: 'Translation for gender "female" in openEdx profile', | ||
defaultMessage: 'Female', | ||
}, | ||
[Gender.OTHER]: { | ||
id: 'openEdxProfile.gender.other', | ||
description: 'Translation for gender "other" in openEdx profile', | ||
defaultMessage: 'Other', | ||
}, | ||
}); | ||
|
||
export interface OpenEdxProfile { | ||
username: Maybe<string>; | ||
name: Maybe<string>; | ||
country: Maybe<string>; | ||
yearOfBirth: Maybe<string>; | ||
levelOfEducation: Maybe<string>; | ||
email: Maybe<string>; | ||
dateJoined: Maybe<Date>; | ||
gender: Maybe<string>; | ||
// FIXME(rlecellier): openEdx do not return language | ||
// language: Maybe<string>; | ||
favoriteLanguage: Maybe<string>; | ||
} | ||
|
||
export const parseOpenEdxApiProfile = ( | ||
intl: IntlShape, | ||
data?: OpenEdxApiProfile, | ||
): OpenEdxProfile => { | ||
const [languageCode] = intl.locale.split('-'); | ||
const defaultValues: OpenEdxProfile = { | ||
username: undefined, | ||
name: undefined, | ||
email: undefined, | ||
// FIXME(rlecellier): openEdx do not return language | ||
// language: undefined, | ||
country: undefined, | ||
levelOfEducation: undefined, | ||
gender: undefined, | ||
yearOfBirth: undefined, | ||
favoriteLanguage: undefined, | ||
dateJoined: undefined, | ||
}; | ||
|
||
const languageNames = new Intl.DisplayNames([intl.locale], { type: 'language' }); | ||
const parsedData = data | ||
? { | ||
username: data.username, | ||
name: data.name, | ||
email: data.email, | ||
yearOfBirth: data.year_of_birth, | ||
dateJoined: new Date(data.date_joined), | ||
levelOfEducation: | ||
data.level_of_education !== null | ||
? intl.formatMessage(levelOfEducationMessages[data.level_of_education]) | ||
: undefined, | ||
gender: data.gender !== null ? intl.formatMessage(genderMessages[data.gender]) : undefined, | ||
country: data.country ? countries.getName(data.country, languageCode) : undefined, | ||
favoriteLanguage: data.language_proficiencies.length | ||
? languageNames.of(data.language_proficiencies[0].code) | ||
: undefined, | ||
} | ||
: defaultValues; | ||
|
||
return parsedData; | ||
}; |
Oops, something went wrong.