transfer-ownership-modal.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { useState } from 'react'
  2. import { Modal, Button } from 'react-bootstrap'
  3. import { Trans } from 'react-i18next'
  4. import PropTypes from 'prop-types'
  5. import Icon from '../../../shared/components/icon'
  6. import { transferProjectOwnership } from '../utils/api'
  7. import AccessibleModal from '../../../shared/components/accessible-modal'
  8. import { reload } from '../utils/location'
  9. import { useProjectContext } from '../../../shared/context/project-context'
  10. export default function TransferOwnershipModal({ member, cancel }) {
  11. const [inflight, setInflight] = useState(false)
  12. const [error, setError] = useState(false)
  13. const project = useProjectContext()
  14. function confirm() {
  15. setError(false)
  16. setInflight(true)
  17. transferProjectOwnership(project, member)
  18. .then(() => {
  19. reload()
  20. })
  21. .catch(() => {
  22. setError(true)
  23. setInflight(false)
  24. })
  25. }
  26. return (
  27. <AccessibleModal show onHide={cancel}>
  28. <Modal.Header closeButton>
  29. <Modal.Title>
  30. <Trans i18nKey="change_project_owner" />
  31. </Modal.Title>
  32. </Modal.Header>
  33. <Modal.Body>
  34. <p>
  35. <Trans
  36. i18nKey="project_ownership_transfer_confirmation_1"
  37. values={{ user: member.email, project: project.name }}
  38. components={[<strong key="strong-1" />, <strong key="strong-2" />]}
  39. />
  40. </p>
  41. <p>
  42. <Trans i18nKey="project_ownership_transfer_confirmation_2" />
  43. </p>
  44. </Modal.Body>
  45. <Modal.Footer>
  46. <div className="modal-footer-left">
  47. {inflight && <Icon type="refresh" spin />}
  48. {error && (
  49. <span className="text-danger">
  50. <Trans i18nKey="generic_something_went_wrong" />
  51. </span>
  52. )}
  53. </div>
  54. <div className="modal-footer-right">
  55. <Button
  56. type="button"
  57. bsStyle="default"
  58. onClick={cancel}
  59. disabled={inflight}
  60. >
  61. <Trans i18nKey="cancel" />
  62. </Button>
  63. <Button
  64. type="button"
  65. bsStyle="success"
  66. onClick={confirm}
  67. disabled={inflight}
  68. >
  69. <Trans i18nKey="change_owner" />
  70. </Button>
  71. </div>
  72. </Modal.Footer>
  73. </AccessibleModal>
  74. )
  75. }
  76. TransferOwnershipModal.propTypes = {
  77. member: PropTypes.object.isRequired,
  78. cancel: PropTypes.func.isRequired,
  79. }