integrationExternalMappingForm.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import React from 'react';
  2. import capitalize from 'lodash/capitalize';
  3. import pick from 'lodash/pick';
  4. import {t, tct} from 'app/locale';
  5. import {ExternalActorMapping, Integration, Organization} from 'app/types';
  6. import {FieldFromConfig} from 'app/views/settings/components/forms';
  7. import Form from 'app/views/settings/components/forms/form';
  8. import {Field} from 'app/views/settings/components/forms/type';
  9. type Props = Pick<Form['props'], 'onSubmitSuccess' | 'onCancel'> &
  10. Partial<Pick<Form['props'], 'onSubmit'>> & {
  11. organization: Organization;
  12. integration: Integration;
  13. mapping?: ExternalActorMapping;
  14. sentryNames: {id: string; name: string}[];
  15. type: 'user' | 'team';
  16. baseEndpoint?: string;
  17. };
  18. export default class IntegrationExternalMappingForm extends React.Component<Props> {
  19. get initialData() {
  20. const {integration, mapping} = this.props;
  21. return {
  22. externalName: '',
  23. userId: '',
  24. teamId: '',
  25. sentryName: '',
  26. provider: integration.provider.key,
  27. integrationId: integration.id,
  28. ...pick(mapping, ['externalName', 'userId', 'sentryName', 'teamId']),
  29. };
  30. }
  31. get formFields(): Field[] {
  32. const {sentryNames, type} = this.props;
  33. const options = sentryNames.map(({name, id}) => ({value: id, label: name}));
  34. const fields: any[] = [
  35. {
  36. name: 'externalName',
  37. type: 'string',
  38. required: true,
  39. label: tct('External [type]', {type: capitalize(type)}),
  40. placeholder: t(`${type === 'team' ? '@org/teamname' : '@username'}`),
  41. },
  42. ];
  43. if (type === 'user') {
  44. fields.push({
  45. name: 'userId',
  46. type: 'select',
  47. required: true,
  48. label: tct('Sentry [type]', {type: capitalize(type)}),
  49. placeholder: t(`Choose your Sentry User`),
  50. options,
  51. deprecatedSelectControl: false,
  52. });
  53. }
  54. if (type === 'team') {
  55. fields.push({
  56. name: 'teamId',
  57. type: 'select',
  58. required: true,
  59. label: tct('Sentry [type]', {type: capitalize(type)}),
  60. placeholder: t(`Choose your Sentry Team`),
  61. options,
  62. deprecatedSelectControl: false,
  63. });
  64. }
  65. return fields;
  66. }
  67. render() {
  68. const {onSubmitSuccess, onCancel, mapping, baseEndpoint, onSubmit} = this.props;
  69. // endpoint changes if we are making a new row or updating an existing one
  70. const endpoint = !baseEndpoint
  71. ? undefined
  72. : mapping
  73. ? `${baseEndpoint}${mapping.id}/`
  74. : baseEndpoint;
  75. const apiMethod = !baseEndpoint ? undefined : mapping ? 'PUT' : 'POST';
  76. return (
  77. <Form
  78. onSubmitSuccess={onSubmitSuccess}
  79. initialData={this.initialData}
  80. apiEndpoint={endpoint}
  81. apiMethod={apiMethod}
  82. onCancel={onCancel}
  83. onSubmit={onSubmit}
  84. >
  85. {this.formFields.map(field => (
  86. <FieldFromConfig
  87. key={field.name}
  88. field={field}
  89. inline={false}
  90. stacked
  91. flexibleControlStateSize
  92. />
  93. ))}
  94. </Form>
  95. );
  96. }
  97. }