edit.tsx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import {browserHistory} from 'react-router';
  2. import Breadcrumbs from 'sentry/components/breadcrumbs';
  3. import * as Layout from 'sentry/components/layouts/thirds';
  4. import LoadingError from 'sentry/components/loadingError';
  5. import LoadingIndicator from 'sentry/components/loadingIndicator';
  6. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  7. import {t} from 'sentry/locale';
  8. import {setApiQueryData, useApiQuery, useQueryClient} from 'sentry/utils/queryClient';
  9. import useOrganization from 'sentry/utils/useOrganization';
  10. import usePageFilters from 'sentry/utils/usePageFilters';
  11. import {useParams} from 'sentry/utils/useParams';
  12. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  13. import MonitorForm from './components/monitorForm';
  14. import {Monitor} from './types';
  15. export default function EditMonitor() {
  16. const {monitorSlug} = useParams();
  17. const {selection} = usePageFilters();
  18. const organization = useOrganization();
  19. const queryClient = useQueryClient();
  20. const queryKeyUrl = `/organizations/${organization.slug}/monitors/${monitorSlug}/`;
  21. const {
  22. isLoading,
  23. isError,
  24. data: monitor,
  25. refetch,
  26. } = useApiQuery<Monitor>([queryKeyUrl], {
  27. staleTime: 0,
  28. });
  29. function onSubmitSuccess(data: Monitor) {
  30. setApiQueryData(queryClient, [queryKeyUrl], data);
  31. browserHistory.push(
  32. normalizeUrl({
  33. pathname: `/organizations/${organization.slug}/crons/${data.slug}/`,
  34. query: {environment: selection.environments},
  35. })
  36. );
  37. }
  38. function getTitle() {
  39. if (monitor) {
  40. return `${monitor.name} - Crons - ${organization.slug}`;
  41. }
  42. return `Crons - ${organization.slug}`;
  43. }
  44. if (isLoading) {
  45. return <LoadingIndicator />;
  46. }
  47. if (isError) {
  48. return <LoadingError onRetry={refetch} message="Failed to load monitor." />;
  49. }
  50. return (
  51. <SentryDocumentTitle title={getTitle()}>
  52. <Layout.Page>
  53. <Layout.Header>
  54. <Layout.HeaderContent>
  55. <Breadcrumbs
  56. crumbs={[
  57. {
  58. label: t('Crons'),
  59. to: `/organizations/${organization.slug}/crons/`,
  60. },
  61. {
  62. label: t('Editing %s', monitor.name),
  63. },
  64. ]}
  65. />
  66. <Layout.Title>{t('Edit Monitor')}</Layout.Title>
  67. </Layout.HeaderContent>
  68. </Layout.Header>
  69. <Layout.Body>
  70. <Layout.Main fullWidth>
  71. <MonitorForm
  72. monitor={monitor}
  73. apiMethod="PUT"
  74. apiEndpoint={`/organizations/${organization.slug}/monitors/${monitor.slug}/`}
  75. onSubmitSuccess={onSubmitSuccess}
  76. />
  77. </Layout.Main>
  78. </Layout.Body>
  79. </Layout.Page>
  80. </SentryDocumentTitle>
  81. );
  82. }