remove-managed-user-modal.tsx 4.5 KB

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