diff --git a/src/frontend/js/api/lms/dummy.ts b/src/frontend/js/api/lms/dummy.ts index 41617c3064..49e82284c4 100644 --- a/src/frontend/js/api/lms/dummy.ts +++ b/src/frontend/js/api/lms/dummy.ts @@ -6,6 +6,8 @@ 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 } from 'types/openEdx'; +import { OpenEdxApiProfileFactory } from 'utils/test/factories/openEdx'; type JWTPayload = { email: string; @@ -89,6 +91,23 @@ 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( + OpenEdxApiProfileFactory({ + username: 'j_do', + name: 'John Do', + email: 'j.do@whois.net', + country: 'fr', + level_of_education: LevelOfEducation.MASTER_OR_PROFESSIONNAL_DEGREE, + gender: Gender.MALE, + year_of_birth: '1971', + language_proficiencies: [{ code: LanguageIsoCode.ENGLISH }], + }).one(), + ); + }, + }, }, enrollment: { get: async (url: string, user: Nullable) => diff --git a/src/frontend/js/api/lms/openedx-fonzie.ts b/src/frontend/js/api/lms/openedx-fonzie.ts index ea960ac364..2f3fc22cd6 100644 --- a/src/frontend/js/api/lms/openedx-fonzie.ts +++ b/src/frontend/js/api/lms/openedx-fonzie.ts @@ -1,6 +1,9 @@ import { AuthenticationBackend, LMSBackend } from 'types/commonDataProps'; import { APILms } from 'types/api'; import { RICHIE_USER_TOKEN } from 'settings'; +import { HttpError, HttpStatusCode } from 'utils/errors/HttpError'; +import { handle } from 'utils/errors/handle'; +import { OpenEdxApiProfile } from 'types/openEdx'; import OpenEdxHawthornApiInterface from './openedx-hawthorn'; /** @@ -24,6 +27,7 @@ const API = (APIConf: AuthenticationBackend | LMSBackend): APILms => { routes: { user: { me: `${APIConf.endpoint}/api/v1.0/user/me`, + account: `${APIConf.endpoint}/api/user/v1/accounts/:username`, }, }, }; @@ -36,6 +40,19 @@ const API = (APIConf: AuthenticationBackend | LMSBackend): APILms => { accessToken: () => { return sessionStorage.getItem(RICHIE_USER_TOKEN); }, + account: { + get: (username: string) => { + return fetch(APIOptions.routes.user.account.replace(':username', username), { + credentials: 'include', + }).then((response) => { + if (response.ok) return response.json() as unknown as OpenEdxApiProfile; + if (response.status >= HttpStatusCode.INTERNAL_SERVER_ERROR) { + handle(new Error(`[GET - Account] > ${response.status} - ${response.statusText}`)); + } + throw new HttpError(response.status, response.statusText); + }); + }, + }, }, }; }; diff --git a/src/frontend/js/components/Form/Form/_styles.scss b/src/frontend/js/components/Form/Form/_styles.scss index 6b2da81978..5ca523955d 100644 --- a/src/frontend/js/components/Form/Form/_styles.scss +++ b/src/frontend/js/components/Form/Form/_styles.scss @@ -25,4 +25,8 @@ flex-direction: column; } } + + &-footer { + margin-top: $vertical-spacing; + } } diff --git a/src/frontend/js/components/Form/Form/index.tsx b/src/frontend/js/components/Form/Form/index.tsx index 9e902f9646..c5f6d7335a 100644 --- a/src/frontend/js/components/Form/Form/index.tsx +++ b/src/frontend/js/components/Form/Form/index.tsx @@ -20,4 +20,8 @@ Form.Row = ({ children, className }: PropsWithChildren<{ className?: string }>) return
{children}
; }; +Form.Footer = ({ children, className }: PropsWithChildren<{ className?: string }>) => { + return
{children}
; +}; + export default Form; diff --git a/src/frontend/js/hooks/useOpenEdxProfile/index.ts b/src/frontend/js/hooks/useOpenEdxProfile/index.ts new file mode 100644 index 0000000000..2872f233f6 --- /dev/null +++ b/src/frontend/js/hooks/useOpenEdxProfile/index.ts @@ -0,0 +1,36 @@ +import { useCallback } from 'react'; +import { useIntl } from 'react-intl'; +import { AuthenticationApi } from 'api/authentication'; +import { useSessionQuery } from 'utils/react-query/useSessionQuery'; +import { OpenEdxProfile, parseOpenEdxApiProfile } from './utils'; + +interface UseOpenEdxProfileProps { + username: string; +} + +const useOpenEdxProfile = ({ username }: UseOpenEdxProfileProps) => { + if (!AuthenticationApi) { + throw new Error('AuthenticationApi is not defined'); + } + // TODO(rlecellier): need explaination about differents backend usage. + // does it work not having account implement everywhere ? oO + if (!AuthenticationApi!.account) { + throw new Error('Current AuthenticationApi do not support account request'); + } + + const intl = useIntl(); + + // TODO(rlecellier): handle translated error messages + // .catch(() => { + // setError(intl.formatMessage(actualMessages.errorGet)); + // }), + const queryFn: () => Promise = useCallback(async () => { + const openEdxApiProfile = await AuthenticationApi!.account!.get(username); + return parseOpenEdxApiProfile(intl, openEdxApiProfile); + }, [username]); + + const [queryHandler] = useSessionQuery(['open-edx-profile'], queryFn); + return queryHandler; +}; + +export default useOpenEdxProfile; diff --git a/src/frontend/js/hooks/useOpenEdxProfile/utils/index.ts b/src/frontend/js/hooks/useOpenEdxProfile/utils/index.ts new file mode 100644 index 0000000000..0082bd0174 --- /dev/null +++ b/src/frontend/js/hooks/useOpenEdxProfile/utils/index.ts @@ -0,0 +1,131 @@ +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.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.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; + name: Maybe; + country: Maybe; + yearOfBirth: Maybe; + levelOfEducation: Maybe; + email: Maybe; + dateJoined: Maybe; + gender: Maybe; + // FIXME(rlecellier): openEdx do not return language + // language: Maybe; + favoriteLanguage: Maybe; +} + +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 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, + // TODO(rlecellier): get a list of human readable languages + // FIXME(rlecellier): openEdx don't return the language + // language: String(data.language), + favoriteLanguage: data.language_proficiencies.length + ? String(data.language_proficiencies[0].code) + : undefined, + } + : defaultValues; + + return parsedData; +}; diff --git a/src/frontend/js/pages/DashboardOpenEdxProfile/index.tsx b/src/frontend/js/pages/DashboardOpenEdxProfile/index.tsx new file mode 100644 index 0000000000..e48bc41bd4 --- /dev/null +++ b/src/frontend/js/pages/DashboardOpenEdxProfile/index.tsx @@ -0,0 +1,225 @@ +import { Input } from '@openfun/cunningham-react'; +import { FormattedMessage, defineMessages, useIntl } from 'react-intl'; +import { useSession } from 'contexts/SessionContext'; +import useOpenEdxProfile from 'hooks/useOpenEdxProfile'; +import { DashboardBox } from 'widgets/Dashboard/components/DashboardBox'; +import { DashboardCard } from 'widgets/Dashboard/components/DashboardCard'; +import { RouterButton } from 'widgets/Dashboard/components/RouterButton'; +import Form from 'components/Form'; + +const messages = defineMessages({ + sectionHeader: { + id: 'components.DashboardOpenEdxProfile.header', + description: 'Title of the dashboard open edx profile block', + defaultMessage: 'Profile', + }, + baseInformationHeader: { + id: 'components.DashboardOpenEdxProfile.baseInformationHeader', + description: 'Title of the open edx profile form "basic information" block', + defaultMessage: 'Basic account information', + }, + additionalInformationHeader: { + id: 'components.DashboardOpenEdxProfile.additionalInformationHeader', + description: 'Title of the open edx profile form "additional information" block', + defaultMessage: 'Additional account information', + }, + editButtonLabel: { + id: 'components.DashboardOpenEdxProfile.EditButtonLabel', + description: 'Label of the edit button link of the open edx profile form', + defaultMessage: 'Edit your profile', + }, + + usernameInputLabel: { + id: 'components.DashboardOpenEdxProfile.usernameInputLabel', + description: 'Label of the openEdx profile "username" input', + defaultMessage: 'Username', + }, + fullNameInputLabel: { + id: 'components.DashboardOpenEdxProfile.fullNameInputLabel', + description: 'Label of the openEdx profile "fullName" input', + defaultMessage: 'Full name', + }, + emailInputLabel: { + id: 'components.DashboardOpenEdxProfile.emailInputLabel', + description: 'Label of the openEdx profile "email" input', + defaultMessage: 'Email', + }, + // FIXME(rlecellier): openEdx do not return language + // languageInputLabel: { + // id: 'components.DashboardOpenEdxProfile.languageInputLabel', + // description: 'Label of the openEdx profile "langue" input', + // defaultMessage: 'Langue', + // }, + countryInputLabel: { + id: 'components.DashboardOpenEdxProfile.countryInputLabel', + description: 'Label of the openEdx profile "country" input', + defaultMessage: 'Country', + }, + + levelOfEducationInputLabel: { + id: 'components.DashboardOpenEdxProfile.levelOfEducationInputLabel', + description: 'Label of the openEdx profile "level of education" input', + defaultMessage: 'Level of education', + }, + genderInputLabel: { + id: 'components.DashboardOpenEdxProfile.genderInputLabel', + description: 'Label of the openEdx profile "level of education" input', + defaultMessage: 'Sex', + }, + yearOfBirthInputLabel: { + id: 'components.DashboardOpenEdxProfile.yearOfBirthInputLabel', + description: 'Label of the openEdx profile "year of birth" input', + defaultMessage: 'Year of birth', + }, + favoritLanguageInputLabel: { + id: 'components.DashboardOpenEdxProfile.favoritLanguageInputLabel', + description: 'Label of the openEdx profile "favorite language" input', + defaultMessage: 'Favorite language', + }, + usernameInputDescription: { + id: 'components.DashboardOpenEdxProfile.usernameInputDescription', + description: 'Description of the openEdx profile "username" input', + defaultMessage: 'Your name on FUN-MOOC. You cannot change your username.', + }, + fullNameInputDescription: { + id: 'components.DashboardOpenEdxProfile.fullNameInputDescription', + description: 'Description of the openEdx profile "fullName" input', + defaultMessage: + 'The name that appears on your certificates. Other learners never see your full name', + }, + emailInputDescription: { + id: 'components.DashboardOpenEdxProfile.emailInputDescription', + description: 'Description of the openEdx profile "email" input', + defaultMessage: + 'Email used when sign-up, FUN-MOOC and leasons communications will be sent at this address', + }, + // FIXME(rlecellier): openEdx do not return language + // languageInputDescription: { + // id: 'components.DashboardOpenEdxProfile.languageInputDescription', + // description: 'Description of the openEdx profile "langue" input', + // defaultMessage: 'The language used on the website. The website languages are limitated.', + // }, +}); + +const DashboardOpenEdxProfile = () => { + const intl = useIntl(); + const { user } = useSession(); + const { data: openEdxProfileData } = useOpenEdxProfile({ username: user!.username }); + + return ( + }> + + }> + + + + + + + + + + + + {/* FIXME(rlecellier): openEdx do not return language */} + {/* + + */} + + + + + + }> + + + + + + + + + + + + + + + + + {/* FIXME(rlecellier): found how to get openEdx host */} + + + + + + ); +}; +export default DashboardOpenEdxProfile; diff --git a/src/frontend/js/pages/DashboardPreferences/index.tsx b/src/frontend/js/pages/DashboardPreferences/index.tsx index 62ceb20141..5cdd16c947 100644 --- a/src/frontend/js/pages/DashboardPreferences/index.tsx +++ b/src/frontend/js/pages/DashboardPreferences/index.tsx @@ -2,6 +2,7 @@ import { DashboardCreditCardsManagement } from 'pages/DashboardCreditCardsManage import { DashboardAddressesManagement } from 'pages/DashboardAddressesManagement'; import { LearnerDashboardPaths } from 'widgets/Dashboard/utils/learnerRouteMessages'; import { useDashboardNavigate } from 'widgets/Dashboard/hooks/useDashboardRouter'; +import DashboardOpenEdxProfile from 'pages/DashboardOpenEdxProfile'; /** * This component relies on react-router. @@ -10,6 +11,7 @@ export const DashboardPreferences = () => { const navigate = useDashboardNavigate(); return (
+ navigate(LearnerDashboardPaths.PREFERENCES_ADDRESS_CREATION)} onClickEdit={(address) => diff --git a/src/frontend/js/types/api.ts b/src/frontend/js/types/api.ts index 7d8d867e99..f35b4ae805 100644 --- a/src/frontend/js/types/api.ts +++ b/src/frontend/js/types/api.ts @@ -1,6 +1,7 @@ import { Maybe, Nullable } from 'types/utils'; import { User } from 'types/User'; import { UnknownEnrollment } from 'types'; +import { OpenEdxApiProfile } from './openEdx'; export interface APIListRequestParams { [key: string]: Maybe; @@ -20,6 +21,10 @@ export interface APIAuthentication { logout: () => Promise; me: () => Promise>; register: () => void; + // FIXME(rlecellier): should it be optional ? + account?: { + get: (username: string) => Promise; + }; } export interface APIEnrollment { diff --git a/src/frontend/js/types/openEdx.ts b/src/frontend/js/types/openEdx.ts new file mode 100644 index 0000000000..4f234fe458 --- /dev/null +++ b/src/frontend/js/types/openEdx.ts @@ -0,0 +1,45 @@ +import { Nullable } from './utils'; + +export enum LevelOfEducation { + PHD_OR_DOCTORATE = 'p', + MASTER_OR_PROFESSIONNAL_DEGREE = 'm', + BACHELOR_DEGREE = 'b', + ASSOCIATE_DEGREE = 'a', + SECONDARY_OR_HIGH_SCHOOL = 'hs', + JUNIOR_SECONDARY_OR_MIDDLE_SCHOOL = 'jhs', + ELEMENTARY_PRIMARY_SCHOOL = 'el', + NONE = 'none', + OTHER = 'o', +} + +// * null +// * "f" +// * "m" +// * "o" +export enum Gender { + FEMALE = 'f', + MALE = 'm', + OTHER = 'o', +} + +// TODO(rlecellier): find a language code list: +// * from openEdx +// * from an existing list +export enum LanguageIsoCode { + ENGLISH = 'en', + FRENCH = 'fr', +} + +export interface OpenEdxApiProfile { + username: string; + name: string; + country: string; + year_of_birth: string; + level_of_education: Nullable; + email: string; + date_joined: string; + gender: Nullable; + // FIXME(rlecellier): openEdx do not return language + // language: LanguageIsoCode; + language_proficiencies: { code: LanguageIsoCode }[]; +} diff --git a/src/frontend/js/utils/test/factories/openEdx.tsx b/src/frontend/js/utils/test/factories/openEdx.tsx new file mode 100644 index 0000000000..be686cd744 --- /dev/null +++ b/src/frontend/js/utils/test/factories/openEdx.tsx @@ -0,0 +1,19 @@ +import { faker } from '@faker-js/faker'; +import { Gender, LanguageIsoCode, LevelOfEducation, OpenEdxApiProfile } from 'types/openEdx'; +import { factory } from './factories'; + +export const OpenEdxApiProfileFactory = factory((): OpenEdxApiProfile => { + return { + username: faker.internet.userName(), + name: faker.person.fullName(), + country: faker.location.countryCode(), + year_of_birth: faker.date.past().toISOString(), + level_of_education: LevelOfEducation.ELEMENTARY_PRIMARY_SCHOOL, + email: faker.internet.email(), + date_joined: faker.date.past().toISOString(), + gender: Gender.MALE, + // FIXME(rlecellier): openEdx do not return language + // language: LanguageIsoCode; + language_proficiencies: [{ code: LanguageIsoCode.FRENCH }], + }; +});