2022-04-08 07:03:41 -04:00
|
|
|
import { useCallback, useState } from 'react'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
2022-05-16 04:02:17 -04:00
|
|
|
import { FetchError } from '../../../../infrastructure/fetch-json'
|
2022-04-22 09:49:26 -04:00
|
|
|
import IEEELogo from '../../../../shared/svgs/ieee-logo'
|
|
|
|
import GoogleLogo from '../../../../shared/svgs/google-logo'
|
|
|
|
import OrcidLogo from '../../../../shared/svgs/orcid-logo'
|
2022-05-16 04:02:17 -04:00
|
|
|
import LinkingStatus from './status'
|
2024-05-15 10:31:00 -04:00
|
|
|
import OLButton from '@/features/ui/components/ol/ol-button'
|
2024-05-14 11:53:25 -04:00
|
|
|
import OLModal, {
|
|
|
|
OLModalBody,
|
|
|
|
OLModalFooter,
|
|
|
|
OLModalHeader,
|
|
|
|
OLModalTitle,
|
2024-05-15 10:31:00 -04:00
|
|
|
} from '@/features/ui/components/ol/ol-modal'
|
2022-04-22 09:49:26 -04:00
|
|
|
|
2022-05-26 05:39:07 -04:00
|
|
|
const providerLogos: { readonly [p: string]: JSX.Element } = {
|
2022-04-22 09:49:26 -04:00
|
|
|
collabratec: <IEEELogo />,
|
|
|
|
google: <GoogleLogo />,
|
|
|
|
orcid: <OrcidLogo />,
|
|
|
|
}
|
2022-04-08 07:03:41 -04:00
|
|
|
|
|
|
|
type SSOLinkingWidgetProps = {
|
2022-04-22 09:49:26 -04:00
|
|
|
providerId: string
|
2022-04-08 07:03:41 -04:00
|
|
|
title: string
|
|
|
|
description: string
|
2022-04-22 09:49:26 -04:00
|
|
|
helpPath?: string
|
2022-04-08 07:03:41 -04:00
|
|
|
linked?: boolean
|
|
|
|
linkPath: string
|
|
|
|
onUnlink: () => Promise<void>
|
|
|
|
}
|
|
|
|
|
|
|
|
export function SSOLinkingWidget({
|
2022-04-22 09:49:26 -04:00
|
|
|
providerId,
|
2022-04-08 07:03:41 -04:00
|
|
|
title,
|
|
|
|
description,
|
2022-04-22 09:49:26 -04:00
|
|
|
helpPath,
|
2022-04-08 07:03:41 -04:00
|
|
|
linked,
|
|
|
|
linkPath,
|
|
|
|
onUnlink,
|
|
|
|
}: SSOLinkingWidgetProps) {
|
2022-04-22 09:49:26 -04:00
|
|
|
const { t } = useTranslation()
|
2022-04-08 07:03:41 -04:00
|
|
|
const [showModal, setShowModal] = useState(false)
|
|
|
|
const [unlinkRequestInflight, setUnlinkRequestInflight] = useState(false)
|
|
|
|
const [errorMessage, setErrorMessage] = useState('')
|
|
|
|
|
|
|
|
const handleUnlinkClick = useCallback(() => {
|
|
|
|
setShowModal(true)
|
2022-05-16 04:02:17 -04:00
|
|
|
setErrorMessage('')
|
2022-04-08 07:03:41 -04:00
|
|
|
}, [])
|
|
|
|
|
|
|
|
const handleUnlinkConfirmationClick = useCallback(() => {
|
|
|
|
setShowModal(false)
|
|
|
|
setUnlinkRequestInflight(true)
|
|
|
|
onUnlink()
|
2022-05-16 04:02:17 -04:00
|
|
|
.catch((error: FetchError) => {
|
|
|
|
setErrorMessage(error.getUserFacingMessage())
|
2022-04-08 07:03:41 -04:00
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
setUnlinkRequestInflight(false)
|
|
|
|
})
|
|
|
|
}, [onUnlink])
|
|
|
|
|
|
|
|
const handleModalHide = useCallback(() => {
|
|
|
|
setShowModal(false)
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
return (
|
2022-04-22 09:49:26 -04:00
|
|
|
<div className="settings-widget-container">
|
|
|
|
<div>{providerLogos[providerId]}</div>
|
|
|
|
<div className="description-container">
|
|
|
|
<div className="title-row">
|
|
|
|
<h4>{title}</h4>
|
|
|
|
</div>
|
2022-04-26 11:20:09 -04:00
|
|
|
<p className="small">
|
2022-04-22 09:49:26 -04:00
|
|
|
{description?.replace(/<[^>]+>/g, '')}{' '}
|
|
|
|
{helpPath ? (
|
|
|
|
<a href={helpPath} target="_blank" rel="noreferrer">
|
|
|
|
{t('learn_more')}
|
|
|
|
</a>
|
|
|
|
) : null}
|
|
|
|
</p>
|
2022-05-16 04:02:17 -04:00
|
|
|
{errorMessage ? (
|
|
|
|
<LinkingStatus status="error" description={errorMessage} />
|
|
|
|
) : null}
|
2022-04-08 07:03:41 -04:00
|
|
|
</div>
|
2022-04-22 09:49:26 -04:00
|
|
|
<div>
|
2022-04-08 07:03:41 -04:00
|
|
|
<ActionButton
|
|
|
|
unlinkRequestInflight={unlinkRequestInflight}
|
|
|
|
accountIsLinked={linked}
|
2022-04-26 11:20:09 -04:00
|
|
|
linkPath={`${linkPath}?intent=link`}
|
2022-04-08 07:03:41 -04:00
|
|
|
onUnlinkClick={handleUnlinkClick}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<UnlinkConfirmModal
|
2022-04-22 09:49:26 -04:00
|
|
|
title={title}
|
2022-04-08 07:03:41 -04:00
|
|
|
show={showModal}
|
|
|
|
handleConfirmation={handleUnlinkConfirmationClick}
|
|
|
|
handleHide={handleModalHide}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
2022-05-26 05:39:07 -04:00
|
|
|
|
2022-04-08 07:03:41 -04:00
|
|
|
type ActionButtonProps = {
|
|
|
|
unlinkRequestInflight: boolean
|
|
|
|
accountIsLinked?: boolean
|
|
|
|
linkPath: string
|
|
|
|
onUnlinkClick: () => void
|
|
|
|
}
|
|
|
|
|
|
|
|
function ActionButton({
|
|
|
|
unlinkRequestInflight,
|
|
|
|
accountIsLinked,
|
|
|
|
linkPath,
|
|
|
|
onUnlinkClick,
|
|
|
|
}: ActionButtonProps) {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
if (unlinkRequestInflight) {
|
|
|
|
return (
|
2024-06-06 11:37:47 -04:00
|
|
|
<OLButton variant="danger-ghost" disabled>
|
2022-04-08 07:03:41 -04:00
|
|
|
{t('unlinking')}
|
2024-05-15 10:31:00 -04:00
|
|
|
</OLButton>
|
2022-04-08 07:03:41 -04:00
|
|
|
)
|
|
|
|
} else if (accountIsLinked) {
|
|
|
|
return (
|
2024-06-06 11:37:47 -04:00
|
|
|
<OLButton variant="danger-ghost" onClick={onUnlinkClick}>
|
2022-04-08 07:03:41 -04:00
|
|
|
{t('unlink')}
|
2024-05-15 10:31:00 -04:00
|
|
|
</OLButton>
|
2022-04-08 07:03:41 -04:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return (
|
2024-06-06 11:37:47 -04:00
|
|
|
<OLButton variant="secondary" href={linkPath} className="text-capitalize">
|
2022-04-08 07:03:41 -04:00
|
|
|
{t('link')}
|
2024-05-15 10:31:00 -04:00
|
|
|
</OLButton>
|
2022-04-08 07:03:41 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type UnlinkConfirmModalProps = {
|
|
|
|
title: string
|
2022-04-22 09:49:26 -04:00
|
|
|
show: boolean
|
2022-04-08 07:03:41 -04:00
|
|
|
handleConfirmation: () => void
|
|
|
|
handleHide: () => void
|
|
|
|
}
|
|
|
|
|
|
|
|
function UnlinkConfirmModal({
|
|
|
|
title,
|
2022-04-22 09:49:26 -04:00
|
|
|
show,
|
2022-04-08 07:03:41 -04:00
|
|
|
handleConfirmation,
|
|
|
|
handleHide,
|
|
|
|
}: UnlinkConfirmModalProps) {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
|
|
|
|
return (
|
2024-05-14 11:53:25 -04:00
|
|
|
<OLModal show={show} onHide={handleHide}>
|
|
|
|
<OLModalHeader closeButton>
|
|
|
|
<OLModalTitle>
|
2022-04-22 09:49:26 -04:00
|
|
|
{t('unlink_provider_account_title', { provider: title })}
|
2024-05-14 11:53:25 -04:00
|
|
|
</OLModalTitle>
|
|
|
|
</OLModalHeader>
|
2022-04-08 07:03:41 -04:00
|
|
|
|
2024-05-14 11:53:25 -04:00
|
|
|
<OLModalBody>
|
2022-04-22 09:49:26 -04:00
|
|
|
<p>{t('unlink_provider_account_warning', { provider: title })}</p>
|
2024-05-14 11:53:25 -04:00
|
|
|
</OLModalBody>
|
2022-04-08 07:03:41 -04:00
|
|
|
|
2024-05-14 11:53:25 -04:00
|
|
|
<OLModalFooter>
|
2024-06-06 11:37:47 -04:00
|
|
|
<OLButton variant="secondary" onClick={handleHide}>
|
2022-04-08 07:03:41 -04:00
|
|
|
{t('cancel')}
|
2024-05-15 10:31:00 -04:00
|
|
|
</OLButton>
|
2024-06-06 11:37:47 -04:00
|
|
|
<OLButton variant="danger-ghost" onClick={handleConfirmation}>
|
2022-04-08 07:03:41 -04:00
|
|
|
{t('unlink')}
|
2024-05-15 10:31:00 -04:00
|
|
|
</OLButton>
|
2024-05-14 11:53:25 -04:00
|
|
|
</OLModalFooter>
|
|
|
|
</OLModal>
|
2022-04-08 07:03:41 -04:00
|
|
|
)
|
|
|
|
}
|