edit.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import {RouteComponentProps} from 'react-router';
  2. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  3. import Alert from 'sentry/components/alert';
  4. import {t} from 'sentry/locale';
  5. import {Organization, Project} from 'sentry/types';
  6. import {metric} from 'sentry/utils/analytics';
  7. import routeTitleGen from 'sentry/utils/routeTitle';
  8. import RuleForm from 'sentry/views/alerts/rules/metric/ruleForm';
  9. import {MetricRule} from 'sentry/views/alerts/rules/metric/types';
  10. import AsyncView from 'sentry/views/asyncView';
  11. type RouteParams = {
  12. orgId: string;
  13. projectId: string;
  14. ruleId: string;
  15. };
  16. type Props = {
  17. onChangeTitle: (data: string) => void;
  18. organization: Organization;
  19. project: Project;
  20. userTeamIds: string[];
  21. } & RouteComponentProps<RouteParams, {}>;
  22. type State = {
  23. actions: Map<string, any>;
  24. rule: MetricRule; // This is temp
  25. } & AsyncView['state'];
  26. class MetricRulesEdit extends AsyncView<Props, State> {
  27. getDefaultState() {
  28. return {
  29. ...super.getDefaultState(),
  30. actions: new Map(),
  31. };
  32. }
  33. getTitle(): string {
  34. const {organization, project} = this.props;
  35. const {rule} = this.state;
  36. const ruleName = rule?.name;
  37. return routeTitleGen(
  38. ruleName ? t('Alert %s', ruleName) : '',
  39. organization.slug,
  40. false,
  41. project?.slug
  42. );
  43. }
  44. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  45. const {orgId, ruleId} = this.props.params;
  46. return [['rule', `/organizations/${orgId}/alert-rules/${ruleId}/`]];
  47. }
  48. onRequestSuccess({stateKey, data}) {
  49. if (stateKey === 'rule' && data.name) {
  50. this.props.onChangeTitle(data.name);
  51. }
  52. }
  53. onLoadAllEndpointsSuccess() {
  54. const {rule} = this.state;
  55. if (rule?.errors) {
  56. (rule?.errors || []).map(({detail}) => addErrorMessage(detail, {append: true}));
  57. }
  58. }
  59. handleSubmitSuccess = () => {
  60. const {router} = this.props;
  61. const {orgId, ruleId} = this.props.params;
  62. metric.endTransaction({name: 'saveAlertRule'});
  63. router.push({
  64. pathname: `/organizations/${orgId}/alerts/rules/details/${ruleId}/`,
  65. });
  66. };
  67. renderError(error?: Error, disableLog = false): React.ReactNode {
  68. const {errors} = this.state;
  69. const notFound = Object.values(errors).find(resp => resp && resp.status === 404);
  70. if (notFound) {
  71. return (
  72. <Alert type="error" showIcon>
  73. {t('This alert rule could not be found.')}
  74. </Alert>
  75. );
  76. }
  77. return super.renderError(error, disableLog);
  78. }
  79. renderBody() {
  80. const {ruleId} = this.props.params;
  81. const {rule} = this.state;
  82. return (
  83. <RuleForm
  84. {...this.props}
  85. ruleId={ruleId}
  86. rule={rule}
  87. onSubmitSuccess={this.handleSubmitSuccess}
  88. disableProjectSelector
  89. />
  90. );
  91. }
  92. }
  93. export default MetricRulesEdit;