offboard-managed-user-modal.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { User } from '../../../../../../types/group-management/user'
  2. import { useState } from 'react'
  3. import useAsync from '@/shared/hooks/use-async'
  4. import { useTranslation } 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 OLFormSelect from '@/shared/components/ol/ol-form-select'
  21. import { sendMB } from '@/infrastructure/event-tracking'
  22. type OffboardManagedUserModalProps = {
  23. user: User
  24. allMembers: User[]
  25. groupId: string
  26. onClose: () => void
  27. }
  28. export default function OffboardManagedUserModal({
  29. user,
  30. allMembers,
  31. groupId,
  32. onClose,
  33. }: OffboardManagedUserModalProps) {
  34. const { t } = useTranslation()
  35. const location = useLocation()
  36. const [selectedRecipientId, setSelectedRecipientId] = useState<string>()
  37. const [suppliedEmail, setSuppliedEmail] = useState<string>()
  38. const [error, setError] = useState<string>()
  39. const { isLoading, isSuccess, runAsync } = useAsync()
  40. const otherMembers = allMembers.filter(u => u._id !== user._id && !u.invite)
  41. const userFullName = user.last_name
  42. ? `${user.first_name || ''} ${user.last_name || ''}`
  43. : user.first_name
  44. const shouldEnableDeleteUserButton =
  45. suppliedEmail === user.email && !!selectedRecipientId
  46. const handleDeleteUserSubmit = (event: React.FormEvent<HTMLFormElement>) => {
  47. event.preventDefault()
  48. sendMB('delete-managed-user-confirmed')
  49. runAsync(
  50. postJSON(`/manage/groups/${groupId}/offboardManagedUser/${user._id}`, {
  51. body: {
  52. verificationEmail: suppliedEmail,
  53. transferProjectsToUserId: selectedRecipientId,
  54. },
  55. })
  56. .then(() => {
  57. location.reload()
  58. })
  59. .catch(err => {
  60. debugConsole.error(err)
  61. setError(
  62. err instanceof FetchError ? err.getUserFacingMessage() : err.message
  63. )
  64. })
  65. )
  66. }
  67. return (
  68. <OLModal id={`delete-user-modal-${user._id}`} show onHide={onClose}>
  69. <form id="delete-user-form" onSubmit={handleDeleteUserSubmit}>
  70. <OLModalHeader>
  71. <OLModalTitle>{t('delete_user')}</OLModalTitle>
  72. </OLModalHeader>
  73. <OLModalBody>
  74. <p>
  75. {t('about_to_delete_user_preamble', {
  76. userName: userFullName,
  77. userEmail: user.email,
  78. })}
  79. </p>
  80. <ul>
  81. <li>{t('they_lose_access_to_account')}</li>
  82. <li>{t('their_projects_will_be_transferred_to_another_user')}</li>
  83. <li>{t('you_will_be_able_to_reassign_subscription')}</li>
  84. </ul>
  85. <p>
  86. <span>{t('this_action_cannot_be_reversed')}</span>
  87. &nbsp;
  88. <a href="/learn/how-to/User_Management_in_Overleaf" target="_blank">
  89. {t('learn_more_about_managed_users')}
  90. </a>
  91. </p>
  92. <strong>{t('transfer_this_users_projects')}</strong>
  93. <p>{t('transfer_this_users_projects_description')}</p>
  94. <OLFormGroup controlId="recipient-select-input">
  95. <OLFormLabel>{t('select_a_new_owner_for_projects')}</OLFormLabel>
  96. <OLFormSelect
  97. aria-label={t('select_user')}
  98. required
  99. value={selectedRecipientId || ''}
  100. onChange={e => setSelectedRecipientId(e.target.value)}
  101. >
  102. <option hidden disabled value="">
  103. {t('choose_from_group_members')}
  104. </option>
  105. {otherMembers.map(member => (
  106. <option value={member._id} key={member.email}>
  107. {member.email}
  108. </option>
  109. ))}
  110. </OLFormSelect>
  111. </OLFormGroup>
  112. <p>
  113. <span>{t('all_projects_will_be_transferred_immediately')}</span>
  114. </p>
  115. <OLFormGroup controlId="supplied-email-input">
  116. <OLFormLabel>
  117. {t('confirm_delete_user_type_email_address', {
  118. userName: userFullName,
  119. })}
  120. </OLFormLabel>
  121. <OLFormControl
  122. type="email"
  123. aria-label={t('email')}
  124. onChange={e => setSuppliedEmail(e.target.value)}
  125. />
  126. </OLFormGroup>
  127. {error && (
  128. <OLNotification type="error" content={error} className="mb-0" />
  129. )}
  130. </OLModalBody>
  131. <OLModalFooter>
  132. <OLButton variant="secondary" onClick={onClose}>
  133. {t('cancel')}
  134. </OLButton>
  135. <OLButton
  136. type="submit"
  137. variant="danger"
  138. disabled={isLoading || isSuccess || !shouldEnableDeleteUserButton}
  139. loadingLabel={t('deleting')}
  140. isLoading={isLoading}
  141. >
  142. {t('delete_user')}
  143. </OLButton>
  144. </OLModalFooter>
  145. </form>
  146. </OLModal>
  147. )
  148. }