sso-alert.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { useState } from 'react'
  2. import { useTranslation, Trans } from 'react-i18next'
  3. import { Alert } from 'react-bootstrap'
  4. import Icon from '../../../../shared/components/icon'
  5. import getMeta from '../../../../utils/meta'
  6. type InstitutionLink = {
  7. universityName: string
  8. hasEntitlement?: boolean
  9. }
  10. type SAMLError = {
  11. translatedMessage?: string
  12. message?: string
  13. tryAgain?: boolean
  14. }
  15. export function SSOAlert() {
  16. const { t } = useTranslation()
  17. const institutionLinked: InstitutionLink | undefined = getMeta(
  18. 'ol-institutionLinked'
  19. )
  20. const institutionEmailNonCanonical: string | undefined = getMeta(
  21. 'ol-institutionEmailNonCanonical'
  22. )
  23. const samlError: SAMLError | undefined = getMeta('ol-samlError')
  24. const [infoClosed, setInfoClosed] = useState(false)
  25. const [warningClosed, setWarningClosed] = useState(false)
  26. const [errorClosed, setErrorClosed] = useState(false)
  27. const handleInfoClosed = () => setInfoClosed(true)
  28. const handleWarningClosed = () => setWarningClosed(true)
  29. const handleErrorClosed = () => setErrorClosed(true)
  30. if (samlError) {
  31. return !errorClosed ? (
  32. <Alert bsStyle="danger" className="mb-0" onDismiss={handleErrorClosed}>
  33. <p className="text-center">
  34. <Icon
  35. type="exclamation-triangle"
  36. accessibilityLabel={t('generic_something_went_wrong')}
  37. />{' '}
  38. {samlError.translatedMessage
  39. ? samlError.translatedMessage
  40. : samlError.message}
  41. </p>
  42. {samlError.tryAgain && <p className="text-center">{t('try_again')}</p>}
  43. </Alert>
  44. ) : null
  45. }
  46. if (!institutionLinked) {
  47. return null
  48. }
  49. return (
  50. <>
  51. {!infoClosed && (
  52. <Alert bsStyle="info" className="mb-0" onDismiss={handleInfoClosed}>
  53. <p className="text-center">
  54. <Trans
  55. i18nKey="institution_acct_successfully_linked_2"
  56. components={[<strong />]} // eslint-disable-line react/jsx-key
  57. values={{ institutionName: institutionLinked.universityName }}
  58. />
  59. </p>
  60. {institutionLinked.hasEntitlement && (
  61. <p className="text-center">
  62. <Trans
  63. i18nKey="this_grants_access_to_features_2"
  64. components={[<strong />]} // eslint-disable-line react/jsx-key
  65. values={{ featureType: t('professional') }}
  66. />
  67. </p>
  68. )}
  69. </Alert>
  70. )}
  71. {!warningClosed && institutionEmailNonCanonical && (
  72. <Alert
  73. bsStyle="warning"
  74. className="mb-0"
  75. onDismiss={handleWarningClosed}
  76. >
  77. <p className="text-center">
  78. <Icon
  79. type="exclamation-triangle"
  80. accessibilityLabel={t('generic_something_went_wrong')}
  81. />{' '}
  82. <Trans
  83. i18nKey="in_order_to_match_institutional_metadata_2"
  84. components={[<strong />]} // eslint-disable-line react/jsx-key
  85. values={{ email: institutionEmailNonCanonical }}
  86. />
  87. </p>
  88. </Alert>
  89. )}
  90. </>
  91. )
  92. }