From abcac63518f1c99053b74f4b277aab211485866d Mon Sep 17 00:00:00 2001 From: Flaminia Cavallo Date: Thu, 9 Jan 2025 08:39:36 +0100 Subject: [PATCH] feat: first draft --- i18n/en.pot | 4 +- src/layout/FormFields.component.js | 20 +++++++++- src/layout/ModalField.component.js | 63 ++++++++++++++++++++++++++++++ src/userSettingsMapping.js | 2 +- 4 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 src/layout/ModalField.component.js diff --git a/i18n/en.pot b/i18n/en.pot index fd723b7b..23d6c939 100644 --- a/i18n/en.pot +++ b/i18n/en.pot @@ -5,8 +5,8 @@ msgstr "" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n != 1)\n" -"POT-Creation-Date: 2025-01-07T11:04:56.155Z\n" -"PO-Revision-Date: 2025-01-07T11:04:56.155Z\n" +"POT-Creation-Date: 2025-01-08T13:52:44.921Z\n" +"PO-Revision-Date: 2025-01-08T13:52:44.921Z\n" msgid "Never" msgstr "Never" diff --git a/src/layout/FormFields.component.js b/src/layout/FormFields.component.js index 45f91315..19f64e59 100644 --- a/src/layout/FormFields.component.js +++ b/src/layout/FormFields.component.js @@ -18,6 +18,7 @@ import AvatarEditor from './AvatarEditor.component.js' import AppTheme from './theme.js' import { VerifyEmail } from './VerifyEmail.component.js' import { VerifyEmailWarning } from './VerifyEmailWarning.js' +import {ModalField} from "./ModalField.component"; const styles = { header: { @@ -243,6 +244,18 @@ function createVerifyButton(fieldBase, valueStore) { }) } +function createModalField(fieldBase, valueStore, onUpdate) { + return Object.assign({}, fieldBase, { + component: ModalField, + props: { + onUpdate, + userEmail: valueStore.state['email'] || '', + setUserEmail: email => { + valueStore.setState({...valueStore.state, email})} + }, + }) +} + function createFieldBaseObject(fieldName, mapping, valueStore) { if (!mapping) { log.warn(`Mapping not found for field: ${fieldName}`) @@ -276,7 +289,7 @@ function createFieldBaseObject(fieldName, mapping, valueStore) { ) } -function createField(fieldName, valueStore, d2) { +function createField(fieldName, valueStore, d2, onUpdate) { const mapping = userSettingsKeyMapping[fieldName] const fieldBase = createFieldBaseObject(fieldName, mapping, valueStore) @@ -295,6 +308,8 @@ function createField(fieldName, valueStore, d2) { return createAvatarEditor(fieldBase, d2, valueStore) case 'submit': return createVerifyButton(fieldBase, valueStore) + case 'modal': + return createModalField(fieldBase, valueStore, onUpdate) default: log.warn( `Unknown control type "${mapping.type}" encountered for field "${fieldName}"` @@ -365,9 +380,10 @@ class FormFields extends Component { renderFields(fieldNames) { const d2 = this.context.d2 const valueStore = this.props.valueStore + const onUpdate = this.props.onUpdateField // Create the regular fields const fields = fieldNames - .map((fieldName) => createField(fieldName, valueStore, d2)) + .map((fieldName) => createField(fieldName, valueStore, d2, onUpdate)) .filter((field) => !!field.name) .map((field) => wrapFieldWithLabel(field)) diff --git a/src/layout/ModalField.component.js b/src/layout/ModalField.component.js new file mode 100644 index 00000000..84082e6e --- /dev/null +++ b/src/layout/ModalField.component.js @@ -0,0 +1,63 @@ +import React, {useMemo, useState} from 'react' +import { + Button, + ButtonStrip, + composeValidators, + email, + hasValue, + InputField, + Modal, + ModalActions, + ModalContent, + ModalTitle, +} from "@dhis2/ui"; +import PropTypes from "prop-types"; + +export function ModalField({userEmail, setUserEmail, onUpdate}) { + + const [modalOpen, setModalOpen] = useState() + const [newEmail, setNewEmail] = useState(userEmail) + const isValidNewEmail = useMemo(() => + newEmail === "mamma" ? false : true + , [newEmail]) + return ( + <> + + + + Update email + + + setNewEmail(newValue.value)}/> + + + + + + + + + + + + ) +} + +ModalField.propTypes = { + userEmail: PropTypes.string +} diff --git a/src/userSettingsMapping.js b/src/userSettingsMapping.js index cf1163a0..c4b2c946 100644 --- a/src/userSettingsMapping.js +++ b/src/userSettingsMapping.js @@ -29,7 +29,7 @@ const settingsKeyMapping = { }, email: { label: i18n.t('E-mail'), - type: 'textfield', + type: 'modal', validators: ['email'], }, emailVerification: {