2022-04-08 07:03:41 -04:00
|
|
|
import { useCallback, useState } from 'react'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import { Button, Modal } from 'react-bootstrap'
|
2022-04-22 09:49:26 -04:00
|
|
|
import AccessibleModal from '../../../../shared/components/accessible-modal'
|
|
|
|
import IEEELogo from '../../../../shared/svgs/ieee-logo'
|
|
|
|
import GoogleLogo from '../../../../shared/svgs/google-logo'
|
|
|
|
import OrcidLogo from '../../../../shared/svgs/orcid-logo'
|
|
|
|
|
|
|
|
const providerLogos = {
|
|
|
|
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)
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const handleUnlinkConfirmationClick = useCallback(() => {
|
|
|
|
setShowModal(false)
|
|
|
|
setUnlinkRequestInflight(true)
|
|
|
|
onUnlink()
|
|
|
|
.catch((error: Error) => {
|
|
|
|
setErrorMessage(error.message)
|
|
|
|
})
|
|
|
|
.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-04-08 07:03:41 -04:00
|
|
|
{errorMessage && <div>{errorMessage} </div>}
|
|
|
|
</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>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
type ActionButtonProps = {
|
|
|
|
unlinkRequestInflight: boolean
|
|
|
|
accountIsLinked?: boolean
|
|
|
|
linkPath: string
|
|
|
|
onUnlinkClick: () => void
|
|
|
|
}
|
|
|
|
|
|
|
|
function ActionButton({
|
|
|
|
unlinkRequestInflight,
|
|
|
|
accountIsLinked,
|
|
|
|
linkPath,
|
|
|
|
onUnlinkClick,
|
|
|
|
}: ActionButtonProps) {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
if (unlinkRequestInflight) {
|
|
|
|
return (
|
2022-04-22 09:49:26 -04:00
|
|
|
<Button bsStyle="danger" disabled>
|
2022-04-08 07:03:41 -04:00
|
|
|
{t('unlinking')}
|
2022-04-22 09:49:26 -04:00
|
|
|
</Button>
|
2022-04-08 07:03:41 -04:00
|
|
|
)
|
|
|
|
} else if (accountIsLinked) {
|
|
|
|
return (
|
2022-04-22 09:49:26 -04:00
|
|
|
<Button bsStyle="danger" onClick={onUnlinkClick}>
|
2022-04-08 07:03:41 -04:00
|
|
|
{t('unlink')}
|
2022-04-22 09:49:26 -04:00
|
|
|
</Button>
|
2022-04-08 07:03:41 -04:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<a href={linkPath} className="btn btn-primary text-capitalize">
|
|
|
|
{t('link')}
|
|
|
|
</a>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 (
|
|
|
|
<AccessibleModal show={show} onHide={handleHide}>
|
|
|
|
<Modal.Header closeButton>
|
2022-04-22 09:49:26 -04:00
|
|
|
<Modal.Title>
|
|
|
|
{t('unlink_provider_account_title', { provider: title })}
|
|
|
|
</Modal.Title>
|
2022-04-08 07:03:41 -04:00
|
|
|
</Modal.Header>
|
|
|
|
|
|
|
|
<Modal.Body className="modal-body-share">
|
2022-04-22 09:49:26 -04:00
|
|
|
<p>{t('unlink_provider_account_warning', { provider: title })}</p>
|
2022-04-08 07:03:41 -04:00
|
|
|
</Modal.Body>
|
|
|
|
|
|
|
|
<Modal.Footer>
|
|
|
|
<Button bsStyle="default" onClick={handleHide}>
|
|
|
|
{t('cancel')}
|
|
|
|
</Button>
|
|
|
|
<Button bsStyle="danger" onClick={handleConfirmation}>
|
|
|
|
{t('unlink')}
|
|
|
|
</Button>
|
|
|
|
</Modal.Footer>
|
|
|
|
</AccessibleModal>
|
|
|
|
)
|
|
|
|
}
|