edit.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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, Project} from 'sentry/types';
  6. import {metric} from 'sentry/utils/analytics';
  7. import routeTitleGen from 'sentry/utils/routeTitle';
  8. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  9. import RuleForm from 'sentry/views/alerts/rules/metric/ruleForm';
  10. import type {MetricRule} from 'sentry/views/alerts/rules/metric/types';
  11. import DeprecatedAsyncView from 'sentry/views/deprecatedAsyncView';
  12. type RouteParams = {
  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. } & DeprecatedAsyncView['state'];
  26. class MetricRulesEdit extends DeprecatedAsyncView<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<DeprecatedAsyncView['getEndpoints']> {
  45. const {organization} = this.props;
  46. const {ruleId} = this.props.params;
  47. return [['rule', `/organizations/${organization.slug}/alert-rules/${ruleId}/`]];
  48. }
  49. onRequestSuccess({stateKey, data}) {
  50. if (stateKey === 'rule' && data.name) {
  51. this.props.onChangeTitle(data.name);
  52. }
  53. }
  54. onLoadAllEndpointsSuccess() {
  55. const {rule} = this.state;
  56. if (rule?.errors) {
  57. (rule?.errors || []).map(({detail}) => addErrorMessage(detail, {append: true}));
  58. }
  59. }
  60. handleSubmitSuccess = () => {
  61. const {organization, router} = this.props;
  62. const {ruleId} = this.props.params;
  63. metric.endSpan({name: 'saveAlertRule'});
  64. router.push(
  65. normalizeUrl({
  66. pathname: `/organizations/${organization.slug}/alerts/rules/details/${ruleId}/`,
  67. })
  68. );
  69. };
  70. renderError(error?: Error, disableLog = false): React.ReactNode {
  71. const {errors} = this.state;
  72. const notFound = Object.values(errors).find(resp => resp && resp.status === 404);
  73. if (notFound) {
  74. return (
  75. <Alert type="error" showIcon>
  76. {t('This alert rule could not be found.')}
  77. </Alert>
  78. );
  79. }
  80. return super.renderError(error, disableLog);
  81. }
  82. renderBody() {
  83. const {ruleId} = this.props.params;
  84. const {rule} = this.state;
  85. return (
  86. <RuleForm
  87. {...this.props}
  88. ruleId={ruleId}
  89. rule={rule}
  90. onSubmitSuccess={this.handleSubmitSuccess}
  91. disableProjectSelector
  92. />
  93. );
  94. }
  95. }
  96. export default MetricRulesEdit;