Skip to content

Commit a9991cc

Browse files
author
David Wang
authored
ref(crons): Convert monitors/edit.tsx to FC (#45881)
Loading state looks the same as before: <img width="1504" alt="image" src="https://user-images.githubusercontent.com/9372512/225440118-9a63779f-f7f3-4496-983e-6db6e4d6ca61.png"> Error state: <img width="1512" alt="image" src="https://user-images.githubusercontent.com/9372512/225440171-a9613d70-fd73-42d2-9065-6185673a7836.png">
1 parent 80cc50e commit a9991cc

File tree

1 file changed

+41
-41
lines changed

1 file changed

+41
-41
lines changed

static/app/views/monitors/edit.tsx

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,67 @@
1-
import {browserHistory, RouteComponentProps} from 'react-router';
1+
import {browserHistory} from 'react-router';
22

33
import Breadcrumbs from 'sentry/components/breadcrumbs';
44
import * as Layout from 'sentry/components/layouts/thirds';
5+
import LoadingError from 'sentry/components/loadingError';
6+
import LoadingIndicator from 'sentry/components/loadingIndicator';
7+
import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
58
import {t} from 'sentry/locale';
6-
import {Organization} from 'sentry/types';
9+
import {useQuery, useQueryClient} from 'sentry/utils/queryClient';
10+
import useOrganization from 'sentry/utils/useOrganization';
11+
import {useParams} from 'sentry/utils/useParams';
712
import {normalizeUrl} from 'sentry/utils/withDomainRequired';
8-
import withOrganization from 'sentry/utils/withOrganization';
9-
import AsyncView from 'sentry/views/asyncView';
1013

1114
import MonitorForm from './components/monitorForm';
1215
import {Monitor} from './types';
1316

14-
type Props = AsyncView['props'] &
15-
RouteComponentProps<{monitorSlug: string}, {}> & {
16-
organization: Organization;
17-
};
17+
export default function EditMonitor() {
18+
const {monitorSlug} = useParams();
19+
const organization = useOrganization();
20+
const queryClient = useQueryClient();
1821

19-
type State = AsyncView['state'] & {
20-
monitor: Monitor | null;
21-
};
22+
const queryKeyUrl = `/organizations/${organization.slug}/monitors/${monitorSlug}/`;
2223

23-
class EditMonitor extends AsyncView<Props, State> {
24-
get orgSlug() {
25-
return this.props.organization.slug;
26-
}
27-
28-
getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
29-
const {params} = this.props;
30-
return [
31-
['monitor', `/organizations/${this.orgSlug}/monitors/${params.monitorSlug}/`],
32-
];
33-
}
24+
const {
25+
isLoading,
26+
isError,
27+
data: monitor,
28+
refetch,
29+
} = useQuery<Monitor>([queryKeyUrl], {
30+
staleTime: 0,
31+
});
3432

35-
onSubmitSuccess = (data: Monitor) =>
33+
function onSubmitSuccess(data: Monitor) {
34+
queryClient.setQueryData([queryKeyUrl], data);
3635
browserHistory.push(
37-
normalizeUrl(`/organizations/${this.orgSlug}/crons/${data.slug}/`)
36+
normalizeUrl(`/organizations/${organization.slug}/crons/${data.slug}/`)
3837
);
38+
}
3939

40-
getTitle() {
41-
if (this.state.monitor) {
42-
return `${this.state.monitor.name} - Crons - ${this.orgSlug}`;
40+
function getTitle() {
41+
if (monitor) {
42+
return `${monitor.name} - Crons - ${organization.slug}`;
4343
}
44-
return `Crons - ${this.orgSlug}`;
44+
return `Crons - ${organization.slug}`;
4545
}
4646

47-
renderBody() {
48-
const {monitor} = this.state;
47+
if (isLoading) {
48+
return <LoadingIndicator />;
49+
}
4950

50-
if (monitor === null) {
51-
return null;
52-
}
51+
if (isError) {
52+
return <LoadingError onRetry={refetch} message="Failed to load monitor." />;
53+
}
5354

54-
return (
55+
return (
56+
<SentryDocumentTitle title={getTitle()}>
5557
<Layout.Page>
5658
<Layout.Header>
5759
<Layout.HeaderContent>
5860
<Breadcrumbs
5961
crumbs={[
6062
{
6163
label: t('Crons'),
62-
to: `/organizations/${this.orgSlug}/crons/`,
64+
to: `/organizations/${organization.slug}/crons/`,
6365
},
6466
{
6567
label: t('Editing %s', monitor.name),
@@ -74,14 +76,12 @@ class EditMonitor extends AsyncView<Props, State> {
7476
<MonitorForm
7577
monitor={monitor}
7678
apiMethod="PUT"
77-
apiEndpoint={`/organizations/${this.orgSlug}/monitors/${monitor.slug}/`}
78-
onSubmitSuccess={this.onSubmitSuccess}
79+
apiEndpoint={`/organizations/${organization.slug}/monitors/${monitor.slug}/`}
80+
onSubmitSuccess={onSubmitSuccess}
7981
/>
8082
</Layout.Main>
8183
</Layout.Body>
8284
</Layout.Page>
83-
);
84-
}
85+
</SentryDocumentTitle>
86+
);
8587
}
86-
87-
export default withOrganization(EditMonitor);

0 commit comments

Comments
 (0)