import {RouteComponentProps} from 'react-router'; import {addErrorMessage} from 'sentry/actionCreators/indicator'; import Alert from 'sentry/components/alert'; import {t} from 'sentry/locale'; import {Organization, Project} from 'sentry/types'; import {metric} from 'sentry/utils/analytics'; import routeTitleGen from 'sentry/utils/routeTitle'; import RuleForm from 'sentry/views/alerts/rules/metric/ruleForm'; import {MetricRule} from 'sentry/views/alerts/rules/metric/types'; import AsyncView from 'sentry/views/asyncView'; type RouteParams = { orgId: string; projectId: string; ruleId: string; }; type Props = { onChangeTitle: (data: string) => void; organization: Organization; project: Project; userTeamIds: string[]; } & RouteComponentProps; type State = { actions: Map; rule: MetricRule; // This is temp } & AsyncView['state']; class MetricRulesEdit extends AsyncView { getDefaultState() { return { ...super.getDefaultState(), actions: new Map(), }; } getTitle(): string { const {organization, project} = this.props; const {rule} = this.state; const ruleName = rule?.name; return routeTitleGen( ruleName ? t('Alert %s', ruleName) : '', organization.slug, false, project?.slug ); } getEndpoints(): ReturnType { const {orgId, ruleId} = this.props.params; return [['rule', `/organizations/${orgId}/alert-rules/${ruleId}/`]]; } onRequestSuccess({stateKey, data}) { if (stateKey === 'rule' && data.name) { this.props.onChangeTitle(data.name); } } onLoadAllEndpointsSuccess() { const {rule} = this.state; if (rule?.errors) { (rule?.errors || []).map(({detail}) => addErrorMessage(detail, {append: true})); } } handleSubmitSuccess = () => { const {router} = this.props; const {orgId, ruleId} = this.props.params; metric.endTransaction({name: 'saveAlertRule'}); router.push({ pathname: `/organizations/${orgId}/alerts/rules/details/${ruleId}/`, }); }; renderError(error?: Error, disableLog = false): React.ReactNode { const {errors} = this.state; const notFound = Object.values(errors).find(resp => resp && resp.status === 404); if (notFound) { return ( {t('This alert rule could not be found.')} ); } return super.renderError(error, disableLog); } renderBody() { const {ruleId} = this.props.params; const {rule} = this.state; return ( ); } } export default MetricRulesEdit;