|
@@ -1,8 +1,8 @@
|
|
|
-import {useState} from 'react';
|
|
|
+import {useEffect, useState} from 'react';
|
|
|
import styled from '@emotion/styled';
|
|
|
+import {observe} from 'mobx';
|
|
|
import {Observer} from 'mobx-react';
|
|
|
|
|
|
-import type {APIRequestMethod} from 'sentry/api';
|
|
|
import {Button} from 'sentry/components/button';
|
|
|
import Confirm from 'sentry/components/confirm';
|
|
|
import FieldWrapper from 'sentry/components/forms/fieldGroup/fieldWrapper';
|
|
@@ -12,14 +12,18 @@ import SentryMemberTeamSelectorField from 'sentry/components/forms/fields/sentry
|
|
|
import SentryProjectSelectorField from 'sentry/components/forms/fields/sentryProjectSelectorField';
|
|
|
import TextareaField from 'sentry/components/forms/fields/textareaField';
|
|
|
import TextField from 'sentry/components/forms/fields/textField';
|
|
|
-import Form, {type FormProps} from 'sentry/components/forms/form';
|
|
|
+import Form from 'sentry/components/forms/form';
|
|
|
import FormModel from 'sentry/components/forms/model';
|
|
|
import List from 'sentry/components/list';
|
|
|
import ListItem from 'sentry/components/list/listItem';
|
|
|
import Panel from 'sentry/components/panels/panel';
|
|
|
import {t} from 'sentry/locale';
|
|
|
import {space} from 'sentry/styles/space';
|
|
|
+import type {Organization} from 'sentry/types/organization';
|
|
|
import type {Project} from 'sentry/types/project';
|
|
|
+import normalizeUrl from 'sentry/utils/url/normalizeUrl';
|
|
|
+import {useNavigate} from 'sentry/utils/useNavigate';
|
|
|
+import useOrganization from 'sentry/utils/useOrganization';
|
|
|
import useProjects from 'sentry/utils/useProjects';
|
|
|
import type {UptimeRule} from 'sentry/views/alerts/rules/uptime/types';
|
|
|
|
|
@@ -27,9 +31,7 @@ import {HTTPSnippet} from './httpSnippet';
|
|
|
import {UptimeHeadersField} from './uptimeHeadersField';
|
|
|
|
|
|
interface Props {
|
|
|
- apiMethod: APIRequestMethod;
|
|
|
- apiUrl: string;
|
|
|
- onSubmitSuccess: FormProps['onSubmitSuccess'];
|
|
|
+ organization: Organization;
|
|
|
project: Project;
|
|
|
handleDelete?: () => void;
|
|
|
rule?: UptimeRule;
|
|
@@ -50,36 +52,49 @@ function getFormDataFromRule(rule: UptimeRule) {
|
|
|
};
|
|
|
}
|
|
|
|
|
|
-export function UptimeAlertForm({
|
|
|
- apiMethod,
|
|
|
- apiUrl,
|
|
|
- project,
|
|
|
- onSubmitSuccess,
|
|
|
- handleDelete,
|
|
|
- rule,
|
|
|
-}: Props) {
|
|
|
+export function UptimeAlertForm({project, handleDelete, rule}: Props) {
|
|
|
+ const navigate = useNavigate();
|
|
|
+ const organization = useOrganization();
|
|
|
const {projects} = useProjects();
|
|
|
|
|
|
const initialData = rule
|
|
|
? getFormDataFromRule(rule)
|
|
|
: {projectSlug: project.slug, method: 'GET', headers: []};
|
|
|
|
|
|
- const submitLabel = {
|
|
|
- POST: t('Create Rule'),
|
|
|
- PUT: t('Save Rule'),
|
|
|
- };
|
|
|
-
|
|
|
const [formModel] = useState(() => new FormModel());
|
|
|
|
|
|
+ // XXX(epurkhiser): The forms API endpoint is derived from the selcted
|
|
|
+ // project. We don't have an easy way to interpolate this into the <Form />
|
|
|
+ // components `apiEndpoint` prop, so instead we setup a mobx observer on
|
|
|
+ // value of the project slug and use that to update the endpoint of the form
|
|
|
+ // model
|
|
|
+ useEffect(
|
|
|
+ () =>
|
|
|
+ observe(formModel, () => {
|
|
|
+ const projectSlug = formModel.getValue('projectSlug');
|
|
|
+ const apiEndpoint = rule
|
|
|
+ ? `/projects/${organization.slug}/${projectSlug}/uptime/${rule.id}/`
|
|
|
+ : `/projects/${organization.slug}/${projectSlug}/uptime/`;
|
|
|
+
|
|
|
+ function onSubmitSuccess(response: any) {
|
|
|
+ navigate(
|
|
|
+ normalizeUrl(
|
|
|
+ `/organizations/${organization.slug}/alerts/rules/uptime/${projectSlug}/${response.id}/details/`
|
|
|
+ )
|
|
|
+ );
|
|
|
+ }
|
|
|
+ formModel.setFormOptions({apiEndpoint, onSubmitSuccess});
|
|
|
+ }),
|
|
|
+ [formModel, navigate, organization.slug, rule]
|
|
|
+ );
|
|
|
+
|
|
|
return (
|
|
|
<Form
|
|
|
model={formModel}
|
|
|
- apiMethod={apiMethod}
|
|
|
- apiEndpoint={apiUrl}
|
|
|
+ apiMethod={rule ? 'PUT' : 'POST'}
|
|
|
saveOnBlur={false}
|
|
|
initialData={initialData}
|
|
|
- onSubmitSuccess={onSubmitSuccess}
|
|
|
- submitLabel={submitLabel[apiMethod]}
|
|
|
+ submitLabel={rule ? t('Save Rule') : t('Create Rule')}
|
|
|
extraButton={
|
|
|
rule && handleDelete ? (
|
|
|
<Confirm
|