performance.tsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import {
  2. addErrorMessage,
  3. addLoadingMessage,
  4. clearIndicators,
  5. } from 'app/actionCreators/indicator';
  6. import {Client} from 'app/api';
  7. import {t} from 'app/locale';
  8. export function toggleKeyTransaction(
  9. api: Client,
  10. isKeyTransaction: boolean,
  11. orgId: string,
  12. projects: Readonly<number[]>,
  13. transactionName: string,
  14. teamIds?: string[] // TODO(txiao): make this required
  15. ): Promise<undefined> {
  16. addLoadingMessage(t('Saving changes\u2026'));
  17. const promise: Promise<undefined> = api.requestPromise(
  18. `/organizations/${orgId}/key-transactions/`,
  19. {
  20. method: isKeyTransaction ? 'DELETE' : 'POST',
  21. query: {
  22. project: projects.map(id => String(id)),
  23. },
  24. data: {
  25. transaction: transactionName,
  26. team: teamIds,
  27. },
  28. }
  29. );
  30. promise.then(clearIndicators);
  31. promise.catch(response => {
  32. const non_field_errors = response?.responseJSON?.non_field_errors;
  33. if (
  34. Array.isArray(non_field_errors) &&
  35. non_field_errors.length &&
  36. non_field_errors[0]
  37. ) {
  38. addErrorMessage(response.responseJSON.non_field_errors[0]);
  39. } else {
  40. addErrorMessage(t('Unable to update key transaction'));
  41. }
  42. });
  43. return promise;
  44. }