2024-04-23 06:25:05 -04:00
|
|
|
import { useState, useEffect, useLayoutEffect } from 'react'
|
2022-05-31 08:06:28 -04:00
|
|
|
import { UserEmailData } from '../../../../../../types/user-email'
|
|
|
|
import getMeta from '../../../../utils/meta'
|
|
|
|
import ReconfirmationInfoSuccess from './reconfirmation-info/reconfirmation-info-success'
|
2024-04-23 06:25:05 -04:00
|
|
|
import ReconfirmationInfoPromptText from './reconfirmation-info/reconfirmation-info-prompt-text'
|
2024-05-15 10:31:00 -04:00
|
|
|
import OLRow from '@/features/ui/components/ol/ol-row'
|
|
|
|
import OLCol from '@/features/ui/components/ol/ol-col'
|
|
|
|
import OLNotification from '@/features/ui/components/ol/ol-notification'
|
2024-04-23 06:25:05 -04:00
|
|
|
import { isBootstrap5 } from '@/features/utils/bootstrap-5'
|
|
|
|
import Icon from '@/shared/components/icon'
|
|
|
|
import { useUserEmailsContext } from '@/features/settings/context/user-email-context'
|
|
|
|
import { FetchError, postJSON } from '@/infrastructure/fetch-json'
|
|
|
|
import { debugConsole } from '@/utils/debugging'
|
|
|
|
import { ssoAvailableForInstitution } from '@/features/settings/utils/sso'
|
|
|
|
import { Trans, useTranslation } from 'react-i18next'
|
|
|
|
import useAsync from '@/shared/hooks/use-async'
|
|
|
|
import { useLocation } from '@/shared/hooks/use-location'
|
2024-05-15 10:31:00 -04:00
|
|
|
import OLButton from '@/features/ui/components/ol/ol-button'
|
2024-04-23 06:25:05 -04:00
|
|
|
import classnames from 'classnames'
|
2022-05-31 08:06:28 -04:00
|
|
|
|
|
|
|
type ReconfirmationInfoProps = {
|
|
|
|
userEmailData: UserEmailData
|
|
|
|
}
|
|
|
|
|
|
|
|
function ReconfirmationInfo({ userEmailData }: ReconfirmationInfoProps) {
|
2024-06-18 06:01:37 -04:00
|
|
|
const reconfirmationRemoveEmail = getMeta('ol-reconfirmationRemoveEmail')
|
|
|
|
const reconfirmedViaSAML = getMeta('ol-reconfirmedViaSAML')
|
2022-05-31 08:06:28 -04:00
|
|
|
|
2024-04-23 06:25:05 -04:00
|
|
|
const { t } = useTranslation()
|
2024-06-18 06:01:37 -04:00
|
|
|
const { samlInitPath } = getMeta('ol-ExposedSettings')
|
2024-04-23 06:25:05 -04:00
|
|
|
const { error, isLoading, isError, isSuccess, runAsync } = useAsync()
|
|
|
|
const { state, setLoading: setUserEmailsContextLoading } =
|
|
|
|
useUserEmailsContext()
|
|
|
|
const [hasSent, setHasSent] = useState(false)
|
|
|
|
const [isPending, setIsPending] = useState(false)
|
|
|
|
const location = useLocation()
|
|
|
|
const ssoAvailable = Boolean(
|
|
|
|
ssoAvailableForInstitution(userEmailData.affiliation?.institution ?? null)
|
|
|
|
)
|
|
|
|
|
|
|
|
const handleRequestReconfirmation = () => {
|
|
|
|
if (userEmailData.affiliation?.institution && ssoAvailable) {
|
|
|
|
setIsPending(true)
|
|
|
|
location.assign(
|
|
|
|
`${samlInitPath}?university_id=${userEmailData.affiliation.institution.id}&reconfirm=/user/settings`
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
runAsync(
|
|
|
|
postJSON('/user/emails/send-reconfirmation', {
|
|
|
|
body: {
|
|
|
|
email: userEmailData.email,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
).catch(debugConsole.error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setUserEmailsContextLoading(isLoading)
|
|
|
|
}, [setUserEmailsContextLoading, isLoading])
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
if (isSuccess) {
|
|
|
|
setHasSent(true)
|
|
|
|
}
|
|
|
|
}, [isSuccess])
|
|
|
|
|
|
|
|
const rateLimited =
|
|
|
|
isError && error instanceof FetchError && error.response?.status === 429
|
|
|
|
|
2022-05-31 08:06:28 -04:00
|
|
|
if (!userEmailData.affiliation) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
userEmailData.samlProviderId &&
|
|
|
|
userEmailData.samlProviderId === reconfirmedViaSAML
|
|
|
|
) {
|
|
|
|
return (
|
2024-05-15 10:31:00 -04:00
|
|
|
<OLRow>
|
2024-05-28 04:48:42 -04:00
|
|
|
<OLCol lg={12}>
|
2024-05-15 10:31:00 -04:00
|
|
|
<OLNotification
|
2024-04-23 06:25:05 -04:00
|
|
|
type="info"
|
|
|
|
content={
|
|
|
|
<ReconfirmationInfoSuccess
|
|
|
|
institution={userEmailData.affiliation.institution}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
bs3Props={{ className: 'settings-reconfirm-info small' }}
|
|
|
|
/>
|
2024-05-15 10:31:00 -04:00
|
|
|
</OLCol>
|
|
|
|
</OLRow>
|
2022-05-31 08:06:28 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (userEmailData.affiliation.inReconfirmNotificationPeriod) {
|
|
|
|
return (
|
2024-05-15 10:31:00 -04:00
|
|
|
<OLRow>
|
2024-05-28 04:48:42 -04:00
|
|
|
<OLCol lg={12}>
|
2024-07-24 09:40:24 -04:00
|
|
|
{isBootstrap5() ? (
|
2024-05-15 10:31:00 -04:00
|
|
|
<OLNotification
|
2024-04-23 06:25:05 -04:00
|
|
|
type="info"
|
|
|
|
content={
|
|
|
|
<>
|
|
|
|
{hasSent ? (
|
|
|
|
<Trans
|
|
|
|
i18nKey="please_check_your_inbox_to_confirm"
|
|
|
|
values={{
|
|
|
|
institutionName:
|
|
|
|
userEmailData.affiliation.institution.name,
|
|
|
|
}}
|
|
|
|
shouldUnescape
|
|
|
|
tOptions={{ interpolation: { escapeValue: true } }}
|
|
|
|
components={
|
|
|
|
/* eslint-disable-next-line jsx-a11y/anchor-has-content, react/jsx-key */
|
|
|
|
[<strong />]
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<ReconfirmationInfoPromptText
|
|
|
|
institutionName={
|
|
|
|
userEmailData.affiliation.institution.name
|
|
|
|
}
|
|
|
|
primary={userEmailData.default}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<br />
|
|
|
|
{isError && (
|
|
|
|
<div className="text-danger">
|
|
|
|
{rateLimited
|
|
|
|
? t('too_many_requests')
|
|
|
|
: t('generic_something_went_wrong')}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
action={
|
|
|
|
hasSent ? (
|
|
|
|
<>
|
|
|
|
{isLoading ? (
|
|
|
|
<>
|
|
|
|
<Icon type="refresh" spin fw /> {t('sending')}...
|
|
|
|
</>
|
|
|
|
) : (
|
2024-05-15 10:31:00 -04:00
|
|
|
<OLButton
|
2024-04-23 10:12:25 -04:00
|
|
|
variant="link"
|
2024-04-23 06:25:05 -04:00
|
|
|
disabled={state.isLoading}
|
|
|
|
onClick={handleRequestReconfirmation}
|
2024-05-21 08:16:19 -04:00
|
|
|
className="btn-inline-link"
|
2024-04-23 06:25:05 -04:00
|
|
|
>
|
|
|
|
{t('resend_confirmation_email')}
|
2024-05-15 10:31:00 -04:00
|
|
|
</OLButton>
|
2024-04-23 06:25:05 -04:00
|
|
|
)}
|
|
|
|
</>
|
|
|
|
) : (
|
2024-05-15 10:31:00 -04:00
|
|
|
<OLButton
|
2024-04-23 06:25:05 -04:00
|
|
|
variant="secondary"
|
2024-04-23 10:12:25 -04:00
|
|
|
disabled={isPending}
|
|
|
|
isLoading={isLoading}
|
2024-04-23 06:25:05 -04:00
|
|
|
onClick={handleRequestReconfirmation}
|
|
|
|
>
|
|
|
|
{isLoading ? (
|
|
|
|
<>
|
|
|
|
<Icon type="refresh" spin fw /> {t('sending')}...
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
t('confirm_affiliation')
|
|
|
|
)}
|
2024-05-15 10:31:00 -04:00
|
|
|
</OLButton>
|
2024-04-23 06:25:05 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<div
|
|
|
|
className={classnames('settings-reconfirm-info', 'small', {
|
|
|
|
'alert alert-info':
|
|
|
|
reconfirmationRemoveEmail === userEmailData.email,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{hasSent ? (
|
|
|
|
<div>
|
|
|
|
<Trans
|
|
|
|
i18nKey="please_check_your_inbox_to_confirm"
|
|
|
|
values={{
|
|
|
|
institutionName:
|
|
|
|
userEmailData.affiliation.institution.name,
|
|
|
|
}}
|
|
|
|
shouldUnescape
|
|
|
|
tOptions={{ interpolation: { escapeValue: true } }}
|
|
|
|
components={
|
|
|
|
/* eslint-disable-next-line jsx-a11y/anchor-has-content, react/jsx-key */
|
|
|
|
[<strong />]
|
|
|
|
}
|
|
|
|
/>{' '}
|
|
|
|
{isLoading ? (
|
|
|
|
<>
|
|
|
|
<Icon type="refresh" spin fw /> {t('sending')}...
|
|
|
|
</>
|
|
|
|
) : (
|
2024-05-15 10:31:00 -04:00
|
|
|
<OLButton
|
2024-04-23 10:12:25 -04:00
|
|
|
variant="link"
|
2024-04-23 06:25:05 -04:00
|
|
|
disabled={state.isLoading}
|
|
|
|
onClick={handleRequestReconfirmation}
|
2024-05-21 08:16:19 -04:00
|
|
|
className="btn-inline-link"
|
2024-04-23 06:25:05 -04:00
|
|
|
>
|
|
|
|
{t('resend_confirmation_email')}
|
2024-05-15 10:31:00 -04:00
|
|
|
</OLButton>
|
2024-04-23 06:25:05 -04:00
|
|
|
)}
|
|
|
|
<br />
|
|
|
|
{isError && (
|
|
|
|
<div className="text-danger">
|
|
|
|
{rateLimited
|
|
|
|
? t('too_many_requests')
|
|
|
|
: t('generic_something_went_wrong')}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<div>
|
|
|
|
<ReconfirmationInfoPromptText
|
|
|
|
institutionName={
|
|
|
|
userEmailData.affiliation.institution.name
|
|
|
|
}
|
|
|
|
primary={userEmailData.default}
|
|
|
|
icon={
|
|
|
|
<Icon type="warning" className="me-1 icon-warning" />
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="setting-reconfirm-info-right">
|
2024-05-15 10:31:00 -04:00
|
|
|
<OLButton
|
2024-04-23 06:25:05 -04:00
|
|
|
variant="secondary"
|
|
|
|
disabled={state.isLoading || isPending}
|
|
|
|
onClick={handleRequestReconfirmation}
|
|
|
|
>
|
|
|
|
{isLoading ? (
|
|
|
|
<>
|
|
|
|
<Icon type="refresh" spin fw /> {t('sending')}...
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
t('confirm_affiliation')
|
|
|
|
)}
|
2024-05-15 10:31:00 -04:00
|
|
|
</OLButton>
|
2024-04-23 06:25:05 -04:00
|
|
|
<br />
|
|
|
|
{isError && (
|
|
|
|
<div className="text-danger">
|
|
|
|
{rateLimited
|
|
|
|
? t('too_many_requests')
|
|
|
|
: t('generic_something_went_wrong')}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)}
|
2024-05-15 10:31:00 -04:00
|
|
|
</OLCol>
|
|
|
|
</OLRow>
|
2022-05-31 08:06:28 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ReconfirmationInfo
|