sso-alert.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. shouldUnescape
  59. tOptions={{ interpolation: { escapeValue: true } }}
  60. />
  61. </p>
  62. {institutionLinked.hasEntitlement && (
  63. <p className="text-center">
  64. <Trans
  65. i18nKey="this_grants_access_to_features_2"
  66. components={[<strong />]} // eslint-disable-line react/jsx-key
  67. values={{ featureType: t('professional') }}
  68. shouldUnescape
  69. tOptions={{ interpolation: { escapeValue: true } }}
  70. />
  71. </p>
  72. )}
  73. </Alert>
  74. )}
  75. {!warningClosed && institutionEmailNonCanonical && (
  76. <Alert
  77. bsStyle="warning"
  78. className="mb-0"
  79. onDismiss={handleWarningClosed}
  80. >
  81. <p className="text-center">
  82. <Icon
  83. type="exclamation-triangle"
  84. accessibilityLabel={t('generic_something_went_wrong')}
  85. />{' '}
  86. <Trans
  87. i18nKey="in_order_to_match_institutional_metadata_2"
  88. components={[<strong />]} // eslint-disable-line react/jsx-key
  89. values={{ email: institutionEmailNonCanonical }}
  90. shouldUnescape
  91. tOptions={{ interpolation: { escapeValue: true } }}
  92. />
  93. </p>
  94. </Alert>
  95. )}
  96. </>
  97. )
  98. }