acceptProjectTransfer.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import {RouteComponentProps} from 'react-router';
  2. import {addErrorMessage, addSuccessMessage} from 'app/actionCreators/indicator';
  3. import NarrowLayout from 'app/components/narrowLayout';
  4. import {t, tct} from 'app/locale';
  5. import {Organization, Project} from 'app/types';
  6. import AsyncView from 'app/views/asyncView';
  7. import Form from 'app/views/settings/components/forms/form';
  8. import SelectField from 'app/views/settings/components/forms/selectField';
  9. import SettingsPageHeader from 'app/views/settings/components/settingsPageHeader';
  10. type Props = RouteComponentProps<{}, {}>;
  11. type TransferDetails = {
  12. organizations: Organization[];
  13. project: Project;
  14. };
  15. type State = {
  16. transferDetails: TransferDetails | null;
  17. } & AsyncView['state'];
  18. class AcceptProjectTransfer extends AsyncView<Props, State> {
  19. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  20. const query = this.props.location.query;
  21. return [['transferDetails', '/accept-transfer/', {query}]];
  22. }
  23. getTitle() {
  24. return t('Accept Project Transfer');
  25. }
  26. handleSubmit = formData => {
  27. this.api.request('/accept-transfer/', {
  28. method: 'POST',
  29. data: {
  30. data: this.props.location.query.data,
  31. organization: formData.organization,
  32. },
  33. success: () => {
  34. const orgSlug = formData.organization;
  35. this.props.router.push(`/${orgSlug}`);
  36. addSuccessMessage(t('Project successfully transferred'));
  37. },
  38. error: error => {
  39. const errorMsg =
  40. error && error.responseJSON && typeof error.responseJSON.detail === 'string'
  41. ? error.responseJSON.detail
  42. : '';
  43. addErrorMessage(
  44. t('Unable to transfer project') + errorMsg ? `: ${errorMsg}` : ''
  45. );
  46. },
  47. });
  48. };
  49. renderError(error) {
  50. let disableLog = false;
  51. // Check if there is an error message with `transferDetails` endpoint
  52. // If so, show as toast and ignore, otherwise log to sentry
  53. if (error && error.responseJSON && typeof error.responseJSON.detail === 'string') {
  54. addErrorMessage(error.responseJSON.detail);
  55. disableLog = true;
  56. }
  57. return super.renderError(error, disableLog);
  58. }
  59. renderBody() {
  60. const {transferDetails} = this.state;
  61. const options = transferDetails?.organizations.map(org => ({
  62. label: org.slug,
  63. value: org.slug,
  64. }));
  65. const organization = options?.[0]?.value;
  66. return (
  67. <NarrowLayout>
  68. <SettingsPageHeader title={t('Approve Transfer Project Request')} />
  69. <p>
  70. {tct(
  71. 'Projects must be transferred to a specific [organization]. ' +
  72. 'You can grant specific teams access to the project later under the [projectSettings]. ' +
  73. '(Note that granting access to at least one team is necessary for the project to ' +
  74. 'appear in all parts of the UI.)',
  75. {
  76. organization: <strong>{t('Organization')}</strong>,
  77. projectSettings: <strong>{t('Project Settings')}</strong>,
  78. }
  79. )}
  80. </p>
  81. {transferDetails && (
  82. <p>
  83. {tct(
  84. 'Please select which [organization] you want for the project [project].',
  85. {
  86. organization: <strong>{t('Organization')}</strong>,
  87. project: transferDetails.project.slug,
  88. }
  89. )}
  90. </p>
  91. )}
  92. <Form
  93. onSubmit={this.handleSubmit}
  94. submitLabel={t('Transfer Project')}
  95. submitPriority="danger"
  96. initialData={organization ? {organization} : undefined}
  97. >
  98. <SelectField
  99. options={options}
  100. label={t('Organization')}
  101. name="organization"
  102. style={{borderBottom: 'none'}}
  103. />
  104. </Form>
  105. </NarrowLayout>
  106. );
  107. }
  108. }
  109. export default AcceptProjectTransfer;