diff --git a/env.config.jsx b/env.config.jsx new file mode 100644 index 000000000..cfc4ab1a5 --- /dev/null +++ b/env.config.jsx @@ -0,0 +1,23 @@ +import { PLUGIN_OPERATIONS, DIRECT_PLUGIN } from '@openedx/frontend-plugin-framework'; +import { ExtendedProfileFields } from '@edunext/frontend-component-extended-fields'; + +// Load environment variables from .env file +const config = { + ...process.env, + pluginSlots: { + 'org.openedx.frontend.account.additional_profile_fields.v1': { + plugins: [ + { + op: PLUGIN_OPERATIONS.Insert, + widget: { + id: 'extended_account_fields', + type: DIRECT_PLUGIN, + RenderWidget: ExtendedProfileFields, + }, + }, + ], + }, + }, +}; + +export default config; diff --git a/package-lock.json b/package-lock.json index e98c07550..67dcd6afe 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0-semantically-released", "license": "AGPL-3.0", "dependencies": { + "@edunext/frontend-component-extended-fields": "^1.0.8-alpha", "@edx/brand": "github:nelc/brand-openedx#open-release/teak.nelp", "@edx/frontend-component-footer": "^14.6.0", "@edx/frontend-component-header": "npm:@edunext/frontend-component-header@6.6.1-nelp.2", @@ -2323,6 +2324,24 @@ "node": ">=10.0.0" } }, + "node_modules/@edunext/frontend-component-extended-fields": { + "version": "1.0.8-alpha", + "resolved": "https://registry.npmjs.org/@edunext/frontend-component-extended-fields/-/frontend-component-extended-fields-1.0.8-alpha.tgz", + "integrity": "sha512-dOAPZMtBuA6TKXl+I86OUF4hAp/X8S25HUai3oaeRwf+MtQDxY3qg+9PMNcy0wTbnSM0Ff7aIeEOjCkLMQZePA==", + "dependencies": { + "@edx/frontend-platform": "^7.0.0 || ^8.0.0", + "@openedx/frontend-plugin-framework": "^1.0.0", + "dompurify": "^3.2.6", + "prop-types": "15.8.1" + }, + "peerDependencies": { + "@edx/frontend-platform": "^7.0.0 || ^8.0.0", + "@openedx/frontend-plugin-framework": "^1.0.0", + "@openedx/paragon": "^21.0.0 || ^22.0.0 || ^23.0.0", + "react": "^17.0.0 || ^18.0.0", + "react-dom": "^17.0.0 || ^18.0.0" + } + }, "node_modules/@edunext/frontend-essentials": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/@edunext/frontend-essentials/-/frontend-essentials-5.0.0.tgz", @@ -8457,6 +8476,12 @@ "integrity": "sha512-/Ad8+nIOV7Rl++6f1BdKxFSMgmoqEoYbHRpPcx3JEfv8VRsQe9Z4mCXeJBzxs7mbHY/XOZZuXlRNfhpVPbs6ZA==", "license": "MIT" }, + "node_modules/@types/trusted-types": { + "version": "2.0.7", + "resolved": "https://registry.npmjs.org/@types/trusted-types/-/trusted-types-2.0.7.tgz", + "integrity": "sha512-ScaPdn1dQczgbl0QFTeTOmVHFULt394XJgOQNoyVhZ6r2vLnMLJfBPd53SB52T/3G36VI1/g2MZaX0cwDuXsfw==", + "optional": true + }, "node_modules/@types/use-sync-external-store": { "version": "0.0.3", "resolved": "https://registry.npmjs.org/@types/use-sync-external-store/-/use-sync-external-store-0.0.3.tgz", @@ -12848,6 +12873,14 @@ "url": "https://github.com/fb55/domhandler?sponsor=1" } }, + "node_modules/dompurify": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/dompurify/-/dompurify-3.3.0.tgz", + "integrity": "sha512-r+f6MYR1gGN1eJv0TVQbhA7if/U7P87cdPl3HN5rikqaBSBxLiCb/b9O+2eG0cxz0ghyU+mU1QkbsOwERMYlWQ==", + "optionalDependencies": { + "@types/trusted-types": "^2.0.7" + } + }, "node_modules/domutils": { "version": "2.8.0", "resolved": "https://registry.npmjs.org/domutils/-/domutils-2.8.0.tgz", diff --git a/package.json b/package.json index ff1938ad8..a24935b31 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "extends @edx/browserslist-config" ], "dependencies": { + "@edunext/frontend-component-extended-fields": "^1.0.8-alpha", "@edx/brand": "github:nelc/brand-openedx#open-release/teak.nelp", "@edx/frontend-component-footer": "^14.6.0", "@edx/frontend-component-header": "npm:@edunext/frontend-component-header@6.6.1-nelp.2", diff --git a/src/account-settings/AccountSettingsPage.jsx b/src/account-settings/AccountSettingsPage.jsx index bf89102ef..1564ea3f0 100644 --- a/src/account-settings/AccountSettingsPage.jsx +++ b/src/account-settings/AccountSettingsPage.jsx @@ -53,6 +53,7 @@ import { fetchSiteLanguages } from './site-language'; import { fetchCourseList } from '../notification-preferences/data/thunks'; import NotificationSettings from '../notification-preferences/NotificationSettings'; import { withLocation, withNavigate } from './hoc'; +import AdditionalProfileFieldsSlot from '../plugin-slots/AdditionalProfileFieldsSlot'; class AccountSettingsPage extends React.Component { constructor(props, context) { @@ -732,6 +733,8 @@ class AccountSettingsPage extends React.Component { emptyLabel={this.props.intl.formatMessage(messages['account.settings.field.language.proficiencies.empty'])} {...editableFieldProps} /> + +

diff --git a/src/plugin-slots/AdditionalProfileFieldsSlot/README.md b/src/plugin-slots/AdditionalProfileFieldsSlot/README.md new file mode 100644 index 000000000..26c4dd9cd --- /dev/null +++ b/src/plugin-slots/AdditionalProfileFieldsSlot/README.md @@ -0,0 +1,87 @@ +# Additional Profile Fields + +### Slot ID: `org.openedx.frontend.account.additional_profile_fields.v1` + +## Description + +This slot is used to replace/modify/hide the additional profile fields in the account 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 Example Component +Create a file named `env.config.jsx` at the MFE root with this: + +```jsx +import { PLUGIN_OPERATIONS, DIRECT_PLUGIN } from '@openedx/frontend-plugin-framework'; +import Example from './src/plugin-slots/AdditionalProfileFieldsSlot/example'; + +const config = { + pluginSlots: { + 'org.openedx.frontend.account.additional_profile_fields.v1': { + plugins: [ + { + op: PLUGIN_OPERATIONS.Insert, + widget: { + id: 'additional_account_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 preserving the required structure. 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 +```json +[ + { + "fieldName": "favorite_color", + "fieldValue": "red" + }, + { + "fieldName": "data_authorization", + "fieldValue": true + }, +] +``` + +### `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 account 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. + +### `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 when you need to manually reload the user profile information. Note that `updateUserProfile` typically handles data refresh automatically. diff --git a/src/plugin-slots/AdditionalProfileFieldsSlot/example/index.jsx b/src/plugin-slots/AdditionalProfileFieldsSlot/example/index.jsx new file mode 100644 index 000000000..29551d1b7 --- /dev/null +++ b/src/plugin-slots/AdditionalProfileFieldsSlot/example/index.jsx @@ -0,0 +1,104 @@ +import { useState } from 'react'; +import PropTypes from 'prop-types'; +import { Form, Button } from '@openedx/paragon'; + +/** + * 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 } = {}, +}) => { + const [formMode, setFormMode] = useState('default'); + + // 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; + + const handleSubmit = () => { + try { + updateUserProfile({ extendedProfile: [{ fieldName: 'favorite_color', fieldValue: value }] }); + setFormMode('default'); + } catch (error) { + setFormMode('edit'); + } + }; + + return ( +
+

Example Additional Profile Fields Slot

+ + +

+ {value ? `Selected value: ${value}` : 'No color selected'} +

+ + + ), + edit: ( + <> + + Which Color? + + Red + Green + Blue + + {colorFieldError && ( + + {colorFieldError} + + )} + + + + + ), + }} + /> + +
+ ); +}; +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; diff --git a/src/plugin-slots/AdditionalProfileFieldsSlot/images/custom_fields.png b/src/plugin-slots/AdditionalProfileFieldsSlot/images/custom_fields.png new file mode 100644 index 000000000..547f502c5 Binary files /dev/null and b/src/plugin-slots/AdditionalProfileFieldsSlot/images/custom_fields.png differ diff --git a/src/plugin-slots/AdditionalProfileFieldsSlot/index.jsx b/src/plugin-slots/AdditionalProfileFieldsSlot/index.jsx new file mode 100644 index 000000000..839314b27 --- /dev/null +++ b/src/plugin-slots/AdditionalProfileFieldsSlot/index.jsx @@ -0,0 +1,32 @@ +import { PluginSlot } from '@openedx/frontend-plugin-framework'; +import { useDispatch, useSelector } from 'react-redux'; +import { camelCaseObject, snakeCaseObject } from '@edx/frontend-platform'; + +import { fetchSettings, saveSettings } from '../../account-settings/data/actions'; + +import SwitchContent from '../../account-settings/SwitchContent'; + +const AdditionalProfileFieldsSlot = () => { + const dispatch = useDispatch(); + const extendedProfileValues = useSelector((state) => state.accountSettings.values.extended_profile); + const errors = useSelector((state) => state.accountSettings.errors); + + const pluginProps = { + refreshUserProfile: (username) => dispatch(fetchSettings(username)), + updateUserProfile: (params) => dispatch(saveSettings(null, null, snakeCaseObject(params))), + profileFieldValues: camelCaseObject(extendedProfileValues), + profileFieldErrors: errors, + formComponents: { + SwitchContent, + }, + }; + + return ( + + ); +}; + +export default AdditionalProfileFieldsSlot; diff --git a/src/plugin-slots/README.md b/src/plugin-slots/README.md index afef00546..29358c9a8 100644 --- a/src/plugin-slots/README.md +++ b/src/plugin-slots/README.md @@ -2,3 +2,4 @@ * [`org.openedx.frontend.layout.footer.v1`](./FooterSlot/) * [`org.openedx.frontend.account.id_verification_page.v1`](./IdVerificationPageSlot/) +* [`org.openedx.frontend.account.additional_profile_fields.v1`](./AdditionalProfileFieldsSlot/)