remove-managed-user-modal.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { User } from '../../../../../../types/group-management/user'
  2. import { useState } from 'react'
  3. import useAsync from '@/shared/hooks/use-async'
  4. import { useTranslation, Trans } from 'react-i18next'
  5. import { useLocation } from '@/shared/hooks/use-location'
  6. import { FetchError, postJSON } from '@/infrastructure/fetch-json'
  7. import { debugConsole } from '@/utils/debugging'
  8. import OLModal, {
  9. OLModalBody,
  10. OLModalFooter,
  11. OLModalHeader,
  12. OLModalTitle,
  13. } from '@/features/ui/components/ol/ol-modal'
  14. import OLFormGroup from '@/features/ui/components/ol/ol-form-group'
  15. import OLButton from '@/features/ui/components/ol/ol-button'
  16. import OLNotification from '@/features/ui/components/ol/ol-notification'
  17. import OLFormControl from '@/features/ui/components/ol/ol-form-control'
  18. import OLFormLabel from '@/features/ui/components/ol/ol-form-label'
  19. import { sendMB } from '@/infrastructure/event-tracking'
  20. type RemoveManagedUserModalProps = {
  21. user: User
  22. groupId: string
  23. onClose: () => void
  24. }
  25. export default function RemoveManagedUserModal({
  26. user,
  27. groupId,
  28. onClose,
  29. }: RemoveManagedUserModalProps) {
  30. const { t } = useTranslation()
  31. const location = useLocation()
  32. const { isLoading, isSuccess, error, setError, runAsync } = useAsync<
  33. any,
  34. any
  35. >()
  36. const [suppliedEmail, setSuppliedEmail] = useState<string>()
  37. const shouldEnableRemoveUserButton = suppliedEmail === user.email
  38. const userFullName = user.last_name
  39. ? `${user.first_name || ''} ${user.last_name || ''}`
  40. : user.first_name
  41. const handleReleaseUserSubmit = (event: React.FormEvent<HTMLFormElement>) => {
  42. event.preventDefault()
  43. sendMB('remove-managed-user-confirmed')
  44. runAsync(
  45. postJSON(`/manage/groups/${groupId}/release-managed-user/${user._id}`, {
  46. body: {
  47. verificationEmail: suppliedEmail,
  48. },
  49. })
  50. .then(() => {
  51. location.reload()
  52. })
  53. .catch(err => {
  54. setError(
  55. err instanceof FetchError ? err.getUserFacingMessage() : err.message
  56. )
  57. debugConsole.error(err)
  58. })
  59. )
  60. }
  61. return (
  62. <OLModal id={`release-user-modal-${user._id}`} show onHide={onClose}>
  63. <form onSubmit={handleReleaseUserSubmit} data-testid="release-user-form">
  64. <OLModalHeader>
  65. <OLModalTitle>{t('remove_user')}</OLModalTitle>
  66. </OLModalHeader>
  67. <OLModalBody>
  68. <p>
  69. {t('about_to_remove_user_preamble', {
  70. userName: userFullName,
  71. userEmail: user.email,
  72. })}
  73. </p>
  74. <ul>
  75. <li>{t('they_will_be_removed_from_the_group')}</li>
  76. <li>{t('they_will_no_longer_be_a_managed_user')}</li>
  77. <li>
  78. {t('they_will_retain_their_existing_account_on_the_free_plan')}
  79. </li>
  80. <li>
  81. {t(
  82. 'they_will_retain_ownership_of_projects_currently_owned_by_them_and_collaborators_will_become_read_only'
  83. )}
  84. </li>
  85. <li>
  86. {t(
  87. 'they_will_continue_to_have_access_to_any_projects_shared_with_them'
  88. )}
  89. </li>
  90. <li>
  91. {t(
  92. 'they_wont_be_able_to_log_in_with_sso_they_will_need_to_set_password'
  93. )}
  94. </li>
  95. </ul>
  96. <p>
  97. <Trans
  98. i18nKey="user_has_left_organization_and_need_to_transfer_their_projects"
  99. components={[<b />]} // eslint-disable-line react/jsx-key
  100. />
  101. </p>
  102. <OLFormGroup controlId="supplied-email-input">
  103. <OLFormLabel>
  104. {t('confirm_remove_user_type_email_address', {
  105. userName: userFullName,
  106. })}
  107. </OLFormLabel>
  108. <OLFormControl
  109. type="email"
  110. aria-label={t('email')}
  111. onChange={e => setSuppliedEmail(e.target.value)}
  112. />
  113. </OLFormGroup>
  114. {error && (
  115. <OLNotification type="error" content={error} className="mb-0" />
  116. )}
  117. </OLModalBody>
  118. <OLModalFooter>
  119. <OLButton variant="secondary" onClick={onClose}>
  120. {t('cancel')}
  121. </OLButton>
  122. <OLButton
  123. type="submit"
  124. variant="danger"
  125. disabled={isLoading || isSuccess || !shouldEnableRemoveUserButton}
  126. isLoading={isLoading}
  127. >
  128. {t('remove_user')}
  129. </OLButton>
  130. </OLModalFooter>
  131. </form>
  132. </OLModal>
  133. )
  134. }