Compare commits

...

3 Commits

Author SHA1 Message Date
Brayan Ceron
e7cddb60f7 chore: update snapshots 2026-01-28 19:48:32 +05:30
Brayan Ceron
9b6e928260 feat: wrap AdditionalProfileFieldsSlot in a div for improved layout 2026-01-28 19:48:32 +05:30
Brayan Cerón
0e676ffff3 feat: add slot to extend the profile fields (#1211)
* feat: add extended profile fields functionality with context and form components

* refactor: replace string literals with FORM_MODE constants in profile fields components

* feat: implement BaseField component and refactor field elements to use it

* chore: remove unused webpack development configuration file

* feat: refactor extended profile fields implementation and remove unused components

* feat: update dependencies for frontend-plugin-framework and remove unused dompurify

* refactor: simplify pluginProps structure in ExtendedProfileFieldsSlot component

* feat: add README and example images for Extended Profile Fields slot

* refactor: improve performance & keep consistency

* feat: add Additional Profile Fields slot with example implementation and documentation

* feat: update custom fields image for Additional Profile Fields slot

* fix: reorder import of AdditionalProfileFieldsSlot for consistency

* test: fix snapshot

* fix: adjust margin in example to avoid oddities on mobile

* fix: remove unnecessary empty divs from ProfilePage snapshots
2026-01-28 19:48:32 +05:30
8 changed files with 322 additions and 0 deletions

26
package-lock.json generated
View File

@@ -19,6 +19,7 @@
"@fortawesome/free-regular-svg-icons": "6.7.2",
"@fortawesome/free-solid-svg-icons": "6.7.2",
"@fortawesome/react-fontawesome": "0.2.2",
"@openedx/frontend-plugin-framework": "^1.7.0",
"@openedx/paragon": "^22.17.0",
"@pact-foundation/pact": "^11.0.2",
"@redux-devtools/extension": "3.3.0",
@@ -57,6 +58,31 @@
"redux-mock-store": "1.5.5"
}
},
"frontend-component-extended-fields": {
"name": "@edunext/frontend-component-extended-fields",
"version": "1.0.0",
"extraneous": true,
"license": "AGPL-3.0",
"dependencies": {
"@openedx/frontend-plugin-framework": "^1.5.0"
},
"devDependencies": {
"@edx/browserslist-config": "^1.1.1",
"@edx/frontend-component-footer": "^14.2.0",
"@openedx/frontend-build": "^14.3.1",
"core-js": "3.42.0",
"glob": "7.2.3",
"husky": "7.0.4",
"jest": "29.7.0",
"prop-types": "^15.8.1",
"react-dom": "^18.3.1"
},
"peerDependencies": {
"@edx/frontend-component-footer": "*",
"react": "^18.0.0",
"react-dom": "^18.0.0"
}
},
"node_modules/@adobe/css-tools": {
"version": "4.4.2",
"resolved": "https://registry.npmjs.org/@adobe/css-tools/-/css-tools-4.4.2.tgz",

View File

@@ -39,6 +39,7 @@
"@fortawesome/free-regular-svg-icons": "6.7.2",
"@fortawesome/free-solid-svg-icons": "6.7.2",
"@fortawesome/react-fontawesome": "0.2.2",
"@openedx/frontend-plugin-framework": "^1.7.0",
"@openedx/paragon": "^22.17.0",
"@pact-foundation/pact": "^11.0.2",
"@redux-devtools/extension": "3.3.0",

View File

@@ -0,0 +1,97 @@
# Additional Profile Fields
### Slot ID: `org.openedx.frontend.profile.additional_profile_fields.v1`
## Description
This slot is used to replace/modify/hide the additional profile fields in the profile page.
## Example
The following `env.config.jsx` will extend the default fields with a additional custom fields through a simple example component.
![Screenshot of Custom Fields](./images/custom_fields.png)
### Using the Additional Fields Component
Create a file named `env.config.jsx` at the MFE root with this:
```jsx
import { DIRECT_PLUGIN, PLUGIN_OPERATIONS } from '@openedx/frontend-plugin-framework';
import Example from './src/plugin-slots/AdditionalProfileFieldsSlot/example';
const config = {
pluginSlots: {
'org.openedx.frontend.profile.additional_profile_fields.v1': {
plugins: [
{
op: PLUGIN_OPERATIONS.Insert,
widget: {
id: 'additional_profile_fields',
type: DIRECT_PLUGIN,
RenderWidget: Example,
},
},
],
},
},
};
export default config;
```
## Plugin Props
When implementing a plugin for this slot, the following props are available:
### `updateUserProfile`
- **Type**: Function
- **Description**: A function for updating the user's profile with new field values. This handles the API call to persist changes to the backend.
- **Usage**: Pass an object containing the field updates to be saved to the user's profile. The function automatically handles the persistence and UI updates.
#### Example
```javascript
updateUserProfile({ extendedProfile: [{ fieldName: 'favorite_color', fieldValue: value }] });
```
### `profileFieldValues`
- **Type**: Array of Objects
- **Description**: Contains the current values of all additional profile fields as an array of objects. Each object has a `fieldName` property (string) and a `fieldValue` property (which can be string, boolean, number, or other data types depending on the field type).
- **Usage**: Access specific field values by finding the object with the matching `fieldName` and reading its `fieldValue` property. Use array methods like `find()` to locate specific fields.
#### Example
```javascript
// Finding a specific field value
const nifField = profileFieldValues.find(field => field.fieldName === 'nif');
const nifValue = nifField ? nifField.fieldValue : null;
// Example data structure:
[
{
"fieldName": "favorite_color",
"fieldValue": "red"
},
{
"fieldName": "employment_situation",
"fieldValue": "Unemployed"
},
]
```
### `profileFieldErrors`
- **Type**: Object
- **Description**: Contains validation errors for profile fields. Each key corresponds to a field name, and the value is the error message.
- **Usage**: Check for field-specific errors to display validation feedback to users.
### `formComponents`
- **Type**: Object
- **Description**: Provides access to reusable form components that are consistent with the rest of the profile page styling and behavior. These components follow the platform's design system and include proper validation and accessibility features.
- **Usage**: Use these components in your custom fields implementation to maintain UI consistency. Available components include `SwitchContent` for managing different UI states, `EmptyContent` for empty states, and `EditableItemHeader` for consistent headers.
### `refreshUserProfile`
- **Type**: Function
- **Description**: A function that triggers a refresh of the user's profile data. This can be used after updating profile fields to ensure the UI reflects the latest data from the server.
- **Usage**: Call this function with the username parameter when you need to manually reload the user profile information. Note that `updateUserProfile` typically handles data refresh automatically.
#### Example
```javascript
refreshUserProfile(username);
```

View File

@@ -0,0 +1,129 @@
import { useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import { Button } from '@openedx/paragon';
import { getAuthenticatedUser } from '@edx/frontend-platform/auth';
/**
* Straightforward example of how you could use the pluginProps provided by
* the AdditionalProfileFieldsSlot to create a custom profile field.
*
* Here you can set a 'favorite_color' field with radio buttons and
* save it to the user's profile, especifically to their `meta` in
* the user's model. For more information, see the documentation:
*
* https://github.com/openedx/edx-platform/blob/master/openedx/core/djangoapps/user_api/README.rst#persisting-optional-user-metadata
*/
const Example = ({
updateUserProfile,
profileFieldValues,
profileFieldErrors,
formComponents: { SwitchContent, EditableItemHeader, EmptyContent } = {},
}) => {
const authenticatedUser = getAuthenticatedUser();
const [formMode, setFormMode] = useState('editable');
// Get current favorite color from profileFieldValues
const currentColorField = profileFieldValues?.find(field => field.fieldName === 'favorite_color');
const currentColor = currentColorField ? currentColorField.fieldValue : '';
const [value, setValue] = useState(currentColor);
const handleChange = e => setValue(e.target.value);
// Get any validation errors for the favorite_color field
const colorFieldError = profileFieldErrors?.favorite_color;
useEffect(() => {
if (!value) { setFormMode('empty'); }
if (colorFieldError) {
setFormMode('editing');
}
}, [colorFieldError, value]);
const handleSubmit = () => {
try {
updateUserProfile(authenticatedUser.username, { extendedProfile: [{ fieldName: 'favorite_color', fieldValue: value }] });
setFormMode('editable');
} catch (error) {
setFormMode('editing');
}
};
return (
<div className="border border-accent-500 p-3 mt-5">
<h3 className="h3">Example Additional Profile Fields Slot</h3>
<SwitchContent
className="pt-40px"
expression={formMode}
cases={{
editing: (
<>
<label className="edit-section-header" htmlFor="favorite_color">
Favorite Color
</label>
<input
className="form-control"
id="favorite_color"
name="favorite_color"
value={value}
onChange={handleChange}
/>
<Button type="button" className="mt-2" onClick={handleSubmit}>
Save
</Button>
</>
),
editable: (
<>
<div className="row m-0 pb-1.5 align-items-center">
<p data-hj-suppress className="h5 font-weight-bold m-0">
Favorite Color
</p>
</div>
<EditableItemHeader
content={value}
showEditButton
onClickEdit={() => setFormMode('editing')}
showVisibility={false}
visibility="private"
/>
</>
),
empty: (
<>
<div className="row m-0 pb-1.5 align-items-center">
<p data-hj-suppress className="h5 font-weight-bold m-0">
Favorite Color
</p>
</div>
<EmptyContent onClick={() => setFormMode('editing')}>
<p className="mb-0">Click to add your favorite color</p>
</EmptyContent>
</>
),
}}
/>
</div>
);
};
Example.propTypes = {
updateUserProfile: PropTypes.func.isRequired,
profileFieldValues: PropTypes.arrayOf(
PropTypes.shape({
fieldName: PropTypes.string.isRequired,
fieldValue: PropTypes.oneOfType([
PropTypes.string,
PropTypes.bool,
PropTypes.number,
]).isRequired,
}),
),
profileFieldErrors: PropTypes.objectOf(PropTypes.string),
formComponents: PropTypes.shape({
SwitchContent: PropTypes.elementType.isRequired,
}),
};
export default Example;

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

View File

@@ -0,0 +1,37 @@
import { PluginSlot } from '@openedx/frontend-plugin-framework';
import { useDispatch, useSelector } from 'react-redux';
import { useCallback } from 'react';
import { patchProfile } from '../../profile/data/services';
import { fetchProfile } from '../../profile/data/actions';
import SwitchContent from '../../profile/forms/elements/SwitchContent';
import EmptyContent from '../../profile/forms/elements/EmptyContent';
import EditableItemHeader from '../../profile/forms/elements/EditableItemHeader';
const AdditionalProfileFieldsSlot = () => {
const dispatch = useDispatch();
const extendedProfileValues = useSelector((state) => state.profilePage.account.extendedProfile);
const errors = useSelector((state) => state.profilePage.errors);
const pluginProps = {
refreshUserProfile: useCallback((username) => dispatch(fetchProfile(username)), [dispatch]),
updateUserProfile: patchProfile,
profileFieldValues: extendedProfileValues,
profileFieldErrors: errors,
formComponents: {
SwitchContent,
EmptyContent,
EditableItemHeader,
},
};
return (
<PluginSlot
id="org.openedx.frontend.profile.additional_profile_fields.v1"
pluginProps={pluginProps}
/>
);
};
export default AdditionalProfileFieldsSlot;

View File

@@ -43,6 +43,8 @@ import messages from './ProfilePage.messages';
import withParams from '../utils/hoc';
import AdditionalProfileFieldsSlot from '../plugin-slots/AdditionalProfileFieldsSlot';
ensureConfig(['CREDENTIALS_BASE_URL', 'LMS_BASE_URL'], 'ProfilePage');
class ProfilePage extends React.Component {
@@ -289,6 +291,9 @@ class ProfilePage extends React.Component {
{...commonFormProps}
/>
)}
<div className="mb-4">
<AdditionalProfileFieldsSlot />
</div>
</div>
<div className="pt-md-3 col-md-8 col-lg-7 offset-lg-1">
{!this.isYOBDisabled() && this.renderAgeMessage()}

View File

@@ -547,6 +547,9 @@ exports[`<ProfilePage /> Renders correctly in various states successfully redire
</ul>
</div>
</div>
<div
class="mb-4"
/>
</div>
<div
class="pt-md-3 col-md-8 col-lg-7 offset-lg-1"
@@ -2630,6 +2633,9 @@ exports[`<ProfilePage /> Renders correctly in various states test country edit w
</ul>
</div>
</div>
<div
class="mb-4"
/>
</div>
<div
class="pt-md-3 col-md-8 col-lg-7 offset-lg-1"
@@ -3629,6 +3635,9 @@ exports[`<ProfilePage /> Renders correctly in various states test education edit
</ul>
</div>
</div>
<div
class="mb-4"
/>
</div>
<div
class="pt-md-3 col-md-8 col-lg-7 offset-lg-1"
@@ -5503,6 +5512,9 @@ exports[`<ProfilePage /> Renders correctly in various states test preferreded la
</ul>
</div>
</div>
<div
class="mb-4"
/>
</div>
<div
class="pt-md-3 col-md-8 col-lg-7 offset-lg-1"
@@ -6096,6 +6108,9 @@ exports[`<ProfilePage /> Renders correctly in various states viewing other profi
/>
</div>
</div>
<div
class="mb-4"
/>
</div>
<div
class="pt-md-3 col-md-8 col-lg-7 offset-lg-1"
@@ -6786,6 +6801,9 @@ exports[`<ProfilePage /> Renders correctly in various states viewing own profile
</ul>
</div>
</div>
<div
class="mb-4"
/>
</div>
<div
class="pt-md-3 col-md-8 col-lg-7 offset-lg-1"
@@ -7663,6 +7681,9 @@ exports[`<ProfilePage /> Renders correctly in various states while saving an edi
</ul>
</div>
</div>
<div
class="mb-4"
/>
</div>
<div
class="pt-md-3 col-md-8 col-lg-7 offset-lg-1"
@@ -8604,6 +8625,9 @@ exports[`<ProfilePage /> Renders correctly in various states while saving an edi
</ul>
</div>
</div>
<div
class="mb-4"
/>
</div>
<div
class="pt-md-3 col-md-8 col-lg-7 offset-lg-1"
@@ -9473,6 +9497,9 @@ exports[`<ProfilePage /> Renders correctly in various states without credentials
</ul>
</div>
</div>
<div
class="mb-4"
/>
</div>
<div
class="pt-md-3 col-md-8 col-lg-7 offset-lg-1"