Skip to content

Commit

Permalink
feat: first draft
Browse files Browse the repository at this point in the history
  • Loading branch information
flaminic committed Jan 9, 2025
1 parent f466103 commit abcac63
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 5 deletions.
4 changes: 2 additions & 2 deletions i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
20 changes: 18 additions & 2 deletions src/layout/FormFields.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Check failure on line 21 in src/layout/FormFields.component.js

View workflow job for this annotation

GitHub Actions / lint / lint

`./ModalField.component` import should occur before import of `./theme.js`

Check failure on line 21 in src/layout/FormFields.component.js

View workflow job for this annotation

GitHub Actions / lint / lint

Missing file extension "js" for "./ModalField.component"

const styles = {
header: {
Expand Down Expand Up @@ -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}`)
Expand Down Expand Up @@ -276,7 +289,7 @@ function createFieldBaseObject(fieldName, mapping, valueStore) {
)
}

function createField(fieldName, valueStore, d2) {
function createField(fieldName, valueStore, d2, onUpdate) {

Check failure on line 292 in src/layout/FormFields.component.js

View workflow job for this annotation

GitHub Actions / lint / lint

Function 'createField' has too many parameters (4). Maximum allowed is 3
const mapping = userSettingsKeyMapping[fieldName]
const fieldBase = createFieldBaseObject(fieldName, mapping, valueStore)

Expand All @@ -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}"`
Expand Down Expand Up @@ -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))

Expand Down
63 changes: 63 additions & 0 deletions src/layout/ModalField.component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React, {useMemo, useState} from 'react'

Check failure on line 1 in src/layout/ModalField.component.js

View workflow job for this annotation

GitHub Actions / lint / lint

`react` import should occur after import of `prop-types`
import {
Button,
ButtonStrip,
composeValidators,

Check failure on line 5 in src/layout/ModalField.component.js

View workflow job for this annotation

GitHub Actions / lint / lint

'composeValidators' is defined but never used
email,

Check failure on line 6 in src/layout/ModalField.component.js

View workflow job for this annotation

GitHub Actions / lint / lint

'email' is defined but never used
hasValue,

Check failure on line 7 in src/layout/ModalField.component.js

View workflow job for this annotation

GitHub Actions / lint / lint

'hasValue' is defined but never used
InputField,
Modal,
ModalActions,
ModalContent,
ModalTitle,
} from "@dhis2/ui";
import PropTypes from "prop-types";

export function ModalField({userEmail, setUserEmail, onUpdate}) {

Check failure on line 16 in src/layout/ModalField.component.js

View workflow job for this annotation

GitHub Actions / lint / lint

'setUserEmail' is missing in props validation

Check failure on line 16 in src/layout/ModalField.component.js

View workflow job for this annotation

GitHub Actions / lint / lint

'onUpdate' is missing in props validation

const [modalOpen, setModalOpen] = useState()
const [newEmail, setNewEmail] = useState(userEmail)
const isValidNewEmail = useMemo(() =>
newEmail === "mamma" ? false : true
, [newEmail])
return (
<>
<InputField value={userEmail} disabled/>
<Button onClick={() => setModalOpen(true)}>Edit email</Button>
<Modal hide={!modalOpen}>
<ModalTitle>Update email</ModalTitle>

<ModalContent>
<InputField
value={newEmail}
type="email"
error={!isValidNewEmail}
validationText={isValidNewEmail? undefined : "OH NO"}
onChange={(newValue) => setNewEmail(newValue.value)}/>
</ModalContent>

<ModalActions>
<ButtonStrip end>
<Button onClick={() => setModalOpen(false)} secondary>
Close modal
</Button>

<Button
onClick={() => {
setUserEmail(newEmail)
onUpdate('email', newEmail)
setModalOpen(false)}}
primary
disabled={!isValidNewEmail}>
Save
</Button>
</ButtonStrip>
</ModalActions>
</Modal>
</>
)
}

ModalField.propTypes = {
userEmail: PropTypes.string
}
2 changes: 1 addition & 1 deletion src/userSettingsMapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const settingsKeyMapping = {
},
email: {
label: i18n.t('E-mail'),
type: 'textfield',
type: 'modal',
validators: ['email'],
},
emailVerification: {
Expand Down

0 comments on commit abcac63

Please sign in to comment.