-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: get emailConfigured state from use config
- Loading branch information
1 parent
43d288f
commit 4b68240
Showing
5 changed files
with
48 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,57 @@ | ||
import { useAlert, useDataQuery } from '@dhis2/app-runtime' | ||
import { Button, CircularLoader } from '@dhis2/ui' | ||
import { getInstance as getD2 } from 'd2' | ||
import { useAlert, useDataMutation, useConfig } from '@dhis2/app-runtime' | ||
import { Button } from '@dhis2/ui' | ||
import PropTypes from 'prop-types' | ||
import React, { useState } from 'react' | ||
import React from 'react' | ||
import i18n from '../locales/index.js' | ||
|
||
const systemSettingsQuery = { | ||
systemSettings: { | ||
resource: 'systemSettings', | ||
}, | ||
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 [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() | ||
|
||
await api.post('account/sendEmailVerification') | ||
successAlert.show({ | ||
message: i18n.t('Email verification link sent successfully!'), | ||
}) | ||
} catch (err) { | ||
console.error('Error:', err) | ||
errorAlert.show({ | ||
message: | ||
err.message || | ||
i18n.t('Failed to send email verification link.'), | ||
}) | ||
} finally { | ||
setIsLoading(false) | ||
} | ||
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={handleEmailVerification} | ||
disabled={isButtonDisabled} | ||
onClick={mutateEmailVerification} | ||
disabled={mutationLoading || !userEmail} | ||
loading={mutationLoading} | ||
> | ||
{i18n.t('Verify Email')} | ||
</Button> | ||
{isLoading && <CircularLoader small />} | ||
</div> | ||
) | ||
} | ||
|
||
VerifyEmail.propTypes = { | ||
userEmail: PropTypes.string.isRequired, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters