Skip to content

Commit

Permalink
feat: check for keyEmailHostname and keyEmailUsername in user settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Chisomchima committed Nov 19, 2024
1 parent 211ce07 commit 13df357
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
7 changes: 2 additions & 5 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: 2024-11-19T05:39:42.631Z\n"
"PO-Revision-Date: 2024-11-19T05:39:42.631Z\n"
"POT-Creation-Date: 2024-11-19T09:39:21.743Z\n"
"PO-Revision-Date: 2024-11-19T09:39:21.743Z\n"

msgid "Never"
msgstr "Never"
Expand Down Expand Up @@ -346,9 +346,6 @@ msgstr "No options"
msgid "System default"
msgstr "System default"

msgid "Verify Email"
msgstr "Verify Email"

msgid "User profile"
msgstr "User profile"

Expand Down
5 changes: 3 additions & 2 deletions src/layout/FormFields.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,6 @@ 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))
Expand All @@ -368,10 +367,12 @@ class FormFields extends Component {
// Append the Verify Email button after the 'email' field
const emailFieldIndex = fieldNames.indexOf('email')
if (emailFieldIndex !== -1) {
// check if user has an email configured
const emailValue = valueStore.state['email'] || ''
const verifyEmailField = {
name: 'emailVerification',
component: VerifyEmail,
props: {},
props: { userEmail: emailValue },
}

fields.splice(emailFieldIndex + 1, 0, verifyEmailField)
Expand Down
47 changes: 31 additions & 16 deletions src/layout/VerifyEmail.component.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
import { useAlert } from '@dhis2/app-runtime'
import { CircularLoader } from '@dhis2/ui'
import { useAlert, useDataQuery } from '@dhis2/app-runtime'
import { Button, CircularLoader } from '@dhis2/ui'
import { getInstance as getD2 } from 'd2'
import PropTypes from 'prop-types'
import React, { useState } from 'react'

export function VerifyEmail() {
const systemSettingsQuery = {
systemSettings: {
resource: 'systemSettings',
},
}

export function VerifyEmail({ userEmail }) {
const errorAlert = useAlert(({ message }) => message, { critical: true })
const successAlert = useAlert(({ message }) => message, { success: true })
const [isLoading, setIsLoading] = useState(false)
const { data, loading: systemInfoLoading } =
useDataQuery(systemSettingsQuery)

const keyEmailHostname = data?.systemSettings?.keyEmailHostname
const keyEmailUsername = data?.systemSettings?.keyEmailUsername

const emailConfigured = !!keyEmailHostname && !!keyEmailUsername

const isButtonDisabled =
systemInfoLoading || !emailConfigured || !userEmail?.trim() || isLoading

if (systemInfoLoading) {
return <CircularLoader />
}
const handleEmailVerification = async () => {
setIsLoading(true)
try {
const d2 = await getD2()
const api = d2.Api.getApi()
api.baseUrl = 'http://localhost:8080/'
api.baseUrl = 'http://localhost:8080/api/'

await api.post('account/sendEmailVerification')
successAlert.show({
Expand All @@ -32,22 +52,17 @@ export function VerifyEmail() {

return (
<div style={{ display: 'flex', alignItems: 'center', gap: '10px' }}>
<button
type="button"
style={{
background: 'white',
border: '1px solid',
borderColor: '#1976D2',
color: '#1976D2',
padding: '10px',
cursor: isLoading ? 'not-allowed' : 'pointer',
}}
<Button
secondary
onClick={handleEmailVerification}
disabled={isLoading}
disabled={isButtonDisabled}
>
Verify Email
</button>
</Button>
{isLoading && <CircularLoader small />}
</div>
)
}
VerifyEmail.propTypes = {
userEmail: PropTypes.string.isRequired,
}

0 comments on commit 13df357

Please sign in to comment.