Skip to content

Commit

Permalink
Merge pull request #1445 from dhis2/DHIS2-18374/verify-email-button
Browse files Browse the repository at this point in the history
feat: add send email verification button
  • Loading branch information
Chisomchima authored Nov 22, 2024
2 parents 6e600c8 + 07346f9 commit 96c01c7
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 4 deletions.
19 changes: 17 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: 2023-02-27T14:55:39.217Z\n"
"PO-Revision-Date: 2023-02-27T14:55:39.217Z\n"
"POT-Creation-Date: 2024-11-21T08:20:57.566Z\n"
"PO-Revision-Date: 2024-11-21T08:20:57.566Z\n"

msgid "Never"
msgstr "Never"
Expand Down Expand Up @@ -289,6 +289,9 @@ msgstr "Could not disable two factor authentication"
msgid "Two factor authentication was disabled successfully"
msgstr "Two factor authentication was disabled successfully"

msgid "Please choose a profile avatar less than {{- maxSize}}MB in size."
msgstr "Please choose a profile avatar less than {{- maxSize}}MB in size."

msgid "Failed to upload profile picture"
msgstr "Failed to upload profile picture"

Expand Down Expand Up @@ -364,6 +367,15 @@ msgstr "Personal access tokens"
msgid "About DHIS2"
msgstr "About DHIS2"

msgid "Email verification link sent successfully!"
msgstr "Email verification link sent successfully!"

msgid "Failed to send email verification link."
msgstr "Failed to send email verification link."

msgid "Verify Email"
msgstr "Verify Email"

msgid "Manage personal access tokens"
msgstr "Manage personal access tokens"

Expand Down Expand Up @@ -588,6 +600,9 @@ msgstr "Other"
msgid "E-mail"
msgstr "E-mail"

msgid "E-mail Verification"
msgstr "E-mail Verification"

msgid "Mobile phone number"
msgstr "Mobile phone number"

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 @@ -16,6 +16,7 @@ import userSettingsStore from '../settings/userSettings.store.js'
import userSettingsKeyMapping from '../userSettingsMapping.js'
import AvatarEditor from './AvatarEditor.component.js'
import AppTheme from './theme.js'
import { VerifyEmail } from './VerifyEmail.component.js'

const styles = {
header: {
Expand Down Expand Up @@ -234,9 +235,21 @@ function createAvatarEditor(fieldBase, d2, valueStore) {
})
}

function createVerifyButton(fieldBase, valueStore) {
return Object.assign({}, fieldBase, {
component: VerifyEmail,
props: { userEmail: valueStore.state['email'] || '' },
})
}

function createFieldBaseObject(fieldName, mapping, valueStore) {
if (!mapping) {
log.warn(`Mapping not found for field: ${fieldName}`)
return null // Skip this field
}

const state = valueStore.state
const hintText = mapping.hintText
const hintText = mapping.hintText || ''

const valueString = Object.prototype.hasOwnProperty.call(state, fieldName)
? String(state[fieldName]).trim()
Expand Down Expand Up @@ -265,6 +278,7 @@ function createFieldBaseObject(fieldName, mapping, valueStore) {
function createField(fieldName, valueStore, d2) {
const mapping = userSettingsKeyMapping[fieldName]
const fieldBase = createFieldBaseObject(fieldName, mapping, valueStore)

switch (mapping.type) {
case 'textfield':
return createTextField(fieldBase, mapping)
Expand All @@ -278,6 +292,8 @@ function createField(fieldName, valueStore, d2) {
return createAccountEditor(fieldBase, d2, valueStore)
case 'avatar':
return createAvatarEditor(fieldBase, d2, valueStore)
case 'submit':
return createVerifyButton(fieldBase, valueStore)
default:
log.warn(
`Unknown control type "${mapping.type}" encountered for field "${fieldName}"`
Expand Down Expand Up @@ -348,7 +364,7 @@ class FormFields extends Component {
renderFields(fieldNames) {
const d2 = this.context.d2
const valueStore = this.props.valueStore

// Create the regular fields
const fields = fieldNames
.map((fieldName) => createField(fieldName, valueStore, d2))
.filter((field) => !!field.name)
Expand Down
57 changes: 57 additions & 0 deletions src/layout/VerifyEmail.component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { useAlert, useDataMutation, useConfig } from '@dhis2/app-runtime'
import { Button } from '@dhis2/ui'
import PropTypes from 'prop-types'
import React from 'react'
import i18n from '../locales/index.js'

const sendEmailVerificationMutation = {
resource: 'account/sendEmailVerification',
type: 'create',
}

export function VerifyEmail({ userEmail }) {
const errorAlert = useAlert(({ message }) => message, { critical: true })
const successAlert = useAlert(({ message }) => message, { success: true })
const { systemInfo } = useConfig()

const [mutateEmailVerification, { loading: mutationLoading }] =
useDataMutation(sendEmailVerificationMutation, {
onComplete: () => {
successAlert.show({
message: i18n.t(
'Email verification link sent successfully!'
),
})
},
onError: (err) => {
errorAlert.show({
message:
err.message ||
i18n.t('Failed to send email verification link.'),
})
},
})

const emailConfigured = systemInfo?.emailConfigured

if (!emailConfigured) {
return null // If emailConfigured is false, don't display the button
}

return (
<div style={{ display: 'flex', alignItems: 'center', gap: '10px' }}>
<Button
secondary
onClick={mutateEmailVerification}
disabled={mutationLoading || !userEmail}
loading={mutationLoading}
>
{i18n.t('Verify Email')}
</Button>
</div>
)
}

VerifyEmail.propTypes = {
userEmail: PropTypes.string.isRequired,
}
1 change: 1 addition & 0 deletions src/profile/Profile.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function EditProfile() {
'firstName',
'surname',
'email',
'emailVerification',
'avatar',
'phoneNumber',
'introduction',
Expand Down
5 changes: 5 additions & 0 deletions src/userSettingsMapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ const settingsKeyMapping = {
type: 'textfield',
validators: ['email'],
},
emailVerification: {
name: 'emailVerification',
label: i18n.t('E-mail Verification'),
type: 'submit',
},
phoneNumber: {
label: i18n.t('Mobile phone number'),
type: 'textfield',
Expand Down

1 comment on commit 96c01c7

@dhis2-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.