transfer-ownership-modal.jsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { useState } from 'react'
  2. import { Trans, useTranslation } from 'react-i18next'
  3. import PropTypes from 'prop-types'
  4. import { transferProjectOwnership } from '../utils/api'
  5. import { useProjectContext } from '@/shared/context/project-context'
  6. import { useLocation } from '@/shared/hooks/use-location'
  7. import OLModal, {
  8. OLModalBody,
  9. OLModalFooter,
  10. OLModalHeader,
  11. OLModalTitle,
  12. } from '@/features/ui/components/ol/ol-modal'
  13. import OLNotification from '@/features/ui/components/ol/ol-notification'
  14. import OLButton from '@/features/ui/components/ol/ol-button'
  15. import { Spinner } from 'react-bootstrap'
  16. export default function TransferOwnershipModal({ member, cancel }) {
  17. const { t } = useTranslation()
  18. const [inflight, setInflight] = useState(false)
  19. const [error, setError] = useState(false)
  20. const location = useLocation()
  21. const { _id: projectId, name: projectName } = useProjectContext()
  22. function confirm() {
  23. setError(false)
  24. setInflight(true)
  25. transferProjectOwnership(projectId, member)
  26. .then(() => {
  27. location.reload()
  28. })
  29. .catch(() => {
  30. setError(true)
  31. setInflight(false)
  32. })
  33. }
  34. return (
  35. <OLModal show onHide={cancel}>
  36. <OLModalHeader closeButton>
  37. <OLModalTitle>{t('change_project_owner')}</OLModalTitle>
  38. </OLModalHeader>
  39. <OLModalBody>
  40. <p>
  41. <Trans
  42. i18nKey="project_ownership_transfer_confirmation_1"
  43. values={{ user: member.email, project: projectName }}
  44. components={[<strong key="strong-1" />, <strong key="strong-2" />]}
  45. shouldUnescape
  46. tOptions={{ interpolation: { escapeValue: true } }}
  47. />
  48. </p>
  49. <p>{t('project_ownership_transfer_confirmation_2')}</p>
  50. {error && (
  51. <OLNotification
  52. type="error"
  53. content={t('generic_something_went_wrong')}
  54. className="mb-0 mt-3"
  55. />
  56. )}
  57. </OLModalBody>
  58. <OLModalFooter>
  59. <div className="me-auto">
  60. {inflight && (
  61. <Spinner
  62. animation="border"
  63. aria-hidden="true"
  64. size="sm"
  65. role="status"
  66. />
  67. )}
  68. </div>
  69. <OLButton variant="secondary" onClick={cancel} disabled={inflight}>
  70. {t('cancel')}
  71. </OLButton>
  72. <OLButton variant="primary" onClick={confirm} disabled={inflight}>
  73. {t('change_owner')}
  74. </OLButton>
  75. </OLModalFooter>
  76. </OLModal>
  77. )
  78. }
  79. TransferOwnershipModal.propTypes = {
  80. member: PropTypes.object.isRequired,
  81. cancel: PropTypes.func.isRequired,
  82. }