edit.tsx 3.0 KB

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