| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- import { useCallback, useState } from 'react'
- import { useTranslation } from 'react-i18next'
- import { Button, Modal } from 'react-bootstrap'
- 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 />,
- }
- type SSOLinkingWidgetProps = {
- providerId: string
- title: string
- description: string
- helpPath?: string
- linked?: boolean
- linkPath: string
- onUnlink: () => Promise<void>
- }
- export function SSOLinkingWidget({
- providerId,
- title,
- description,
- helpPath,
- linked,
- linkPath,
- onUnlink,
- }: SSOLinkingWidgetProps) {
- const { t } = useTranslation()
- 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 (
- <div className="settings-widget-container">
- <div>{providerLogos[providerId]}</div>
- <div className="description-container">
- <div className="title-row">
- <h4>{title}</h4>
- </div>
- <p>
- {description?.replace(/<[^>]+>/g, '')}{' '}
- {helpPath ? (
- <a href={helpPath} target="_blank" rel="noreferrer">
- {t('learn_more')}
- </a>
- ) : null}
- </p>
- {errorMessage && <div>{errorMessage} </div>}
- </div>
- <div>
- <ActionButton
- unlinkRequestInflight={unlinkRequestInflight}
- accountIsLinked={linked}
- linkPath={linkPath}
- onUnlinkClick={handleUnlinkClick}
- />
- </div>
- <UnlinkConfirmModal
- title={title}
- 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 (
- <Button bsStyle="danger" disabled>
- {t('unlinking')}
- </Button>
- )
- } else if (accountIsLinked) {
- return (
- <Button bsStyle="danger" onClick={onUnlinkClick}>
- {t('unlink')}
- </Button>
- )
- } else {
- return (
- <a href={linkPath} className="btn btn-primary text-capitalize">
- {t('link')}
- </a>
- )
- }
- }
- type UnlinkConfirmModalProps = {
- title: string
- show: boolean
- handleConfirmation: () => void
- handleHide: () => void
- }
- function UnlinkConfirmModal({
- title,
- show,
- handleConfirmation,
- handleHide,
- }: UnlinkConfirmModalProps) {
- const { t } = useTranslation()
- return (
- <AccessibleModal show={show} onHide={handleHide}>
- <Modal.Header closeButton>
- <Modal.Title>
- {t('unlink_provider_account_title', { provider: title })}
- </Modal.Title>
- </Modal.Header>
- <Modal.Body className="modal-body-share">
- <p>{t('unlink_provider_account_warning', { provider: title })}</p>
- </Modal.Body>
- <Modal.Footer>
- <Button bsStyle="default" onClick={handleHide}>
- {t('cancel')}
- </Button>
- <Button bsStyle="danger" onClick={handleConfirmation}>
- {t('unlink')}
- </Button>
- </Modal.Footer>
- </AccessibleModal>
- )
- }
|