Skip to content

Commit 62f1784

Browse files
committed
fix(aci): update monitor form logic
1 parent e6963f9 commit 62f1784

File tree

4 files changed

+56
-7
lines changed

4 files changed

+56
-7
lines changed

static/app/views/detectors/components/forms/metric.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import SegmentedRadioField from 'sentry/components/forms/fields/segmentedRadioFi
88
import SelectField from 'sentry/components/forms/fields/selectField';
99
import SentryMemberTeamSelectorField from 'sentry/components/forms/fields/sentryMemberTeamSelectorField';
1010
import Form from 'sentry/components/forms/form';
11+
import type FormModel from 'sentry/components/forms/model';
1112
import {SearchQueryBuilder} from 'sentry/components/searchQueryBuilder';
1213
import type {FilterKeySection} from 'sentry/components/searchQueryBuilder/types';
1314
import PriorityControl from 'sentry/components/workflowEngine/form/control/priorityControl';
@@ -32,9 +33,9 @@ import {
3233

3334
type MetricDetectorKind = 'threshold' | 'change' | 'dynamic';
3435

35-
export function MetricDetectorForm() {
36+
export function MetricDetectorForm({model}: {model: FormModel}) {
3637
return (
37-
<Form hideFooter>
38+
<Form hideFooter model={model}>
3839
<ChartContainer>
3940
<MockChart />
4041
</ChartContainer>

static/app/views/detectors/edit.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import {Fragment, useState} from 'react';
33

44
import {Button} from 'sentry/components/core/button';
5+
import FormModel from 'sentry/components/forms/model';
56
import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
67
import {ActionsProvider} from 'sentry/components/workflowEngine/layout/actions';
78
import {BreadcrumbsProvider} from 'sentry/components/workflowEngine/layout/breadcrumbs';
@@ -16,6 +17,7 @@ export default function DetectorEdit() {
1617
const organization = useOrganization();
1718
useWorkflowEngineFeatureGate({redirect: true});
1819
const [title, setTitle] = useState(t('Edit Monitor'));
20+
const model = new FormModel();
1921

2022
return (
2123
<SentryDocumentTitle title={title} noSuffix>
@@ -24,7 +26,7 @@ export default function DetectorEdit() {
2426
>
2527
<ActionsProvider actions={<Actions />}>
2628
<EditLayout onTitleChange={setTitle}>
27-
<MetricDetectorForm />
29+
<MetricDetectorForm model={model} />
2830
</EditLayout>
2931
</ActionsProvider>
3032
</BreadcrumbsProvider>

static/app/views/detectors/hooks/index.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
import {t} from 'sentry/locale';
2+
import AlertStore from 'sentry/stores/alertStore';
13
import type {Detector} from 'sentry/types/workflowEngine/detectors';
2-
import {useApiQueries, useApiQuery} from 'sentry/utils/queryClient';
4+
import {
5+
useApiQueries,
6+
useApiQuery,
7+
useMutation,
8+
useQueryClient,
9+
} from 'sentry/utils/queryClient';
10+
import useApi from 'sentry/utils/useApi';
311
import useOrganization from 'sentry/utils/useOrganization';
412

513
export interface UseDetectorsQueryOptions {
@@ -18,6 +26,27 @@ export const makeDetectorQueryKey = (orgSlug: string, detectorId = ''): [url: st
1826
`/organizations/${orgSlug}/detectors/${detectorId ? `${detectorId}/` : ''}`,
1927
];
2028

29+
export function useCreateDetector() {
30+
const org = useOrganization();
31+
const api = useApi({persistInFlight: true});
32+
const queryClient = useQueryClient();
33+
const queryKey = makeDetectorQueryKey(org.slug);
34+
35+
return useMutation<Detector, void, Detector>({
36+
mutationFn: data =>
37+
api.requestPromise(queryKey[0], {
38+
method: 'POST',
39+
data,
40+
}),
41+
onSuccess: _ => {
42+
queryClient.invalidateQueries({queryKey});
43+
},
44+
onError: _ => {
45+
AlertStore.addAlert({type: 'error', message: t('Unable to create monitor')});
46+
},
47+
});
48+
}
49+
2150
export function useDetectorQuery(detectorId: string) {
2251
const org = useOrganization();
2352

static/app/views/detectors/new-settings.tsx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,41 @@
1+
import {useCallback, useMemo} from 'react';
2+
13
import {Flex} from 'sentry/components/container/flex';
4+
import {Button} from 'sentry/components/core/button';
25
import {LinkButton} from 'sentry/components/core/button/linkButton';
6+
import FormModel from 'sentry/components/forms/model';
37
import {
48
StickyFooter,
59
StickyFooterLabel,
610
} from 'sentry/components/workflowEngine/ui/footer';
711
import {useWorkflowEngineFeatureGate} from 'sentry/components/workflowEngine/useWorkflowEngineFeatureGate';
812
import {t} from 'sentry/locale';
913
import {space} from 'sentry/styles/space';
14+
import type {Detector} from 'sentry/types/workflowEngine/detectors';
15+
import {useNavigate} from 'sentry/utils/useNavigate';
1016
import useOrganization from 'sentry/utils/useOrganization';
1117
import {MetricDetectorForm} from 'sentry/views/detectors/components/forms/metric';
18+
import {useCreateDetector} from 'sentry/views/detectors/hooks';
1219
import NewDetectorLayout from 'sentry/views/detectors/layouts/new';
1320
import {makeMonitorBasePathname} from 'sentry/views/detectors/pathnames';
1421

1522
export default function DetectorNewSettings() {
1623
const organization = useOrganization();
1724
useWorkflowEngineFeatureGate({redirect: true});
25+
const navigate = useNavigate();
26+
const model = useMemo(() => new FormModel(), []);
27+
28+
const {mutateAsync: createDetector} = useCreateDetector();
29+
30+
const handleSubmit = useCallback(async () => {
31+
const data = model.getData() as unknown as Detector;
32+
const result = await createDetector(data);
33+
navigate(`${makeMonitorBasePathname(organization.slug)}${result.id}`);
34+
}, [createDetector, model, navigate, organization]);
1835

1936
return (
2037
<NewDetectorLayout>
21-
<MetricDetectorForm />
38+
<MetricDetectorForm model={model} />
2239
<StickyFooter>
2340
<StickyFooterLabel>{t('Step 2 of 2')}</StickyFooterLabel>
2441
<Flex gap={space(1)}>
@@ -28,9 +45,9 @@ export default function DetectorNewSettings() {
2845
>
2946
{t('Back')}
3047
</LinkButton>
31-
<LinkButton priority="primary" to="/issues/monitors/1">
48+
<Button priority="primary" onClick={handleSubmit}>
3249
{t('Create Monitor')}
33-
</LinkButton>
50+
</Button>
3451
</Flex>
3552
</StickyFooter>
3653
</NewDetectorLayout>

0 commit comments

Comments
 (0)