edit.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import {useEffect} from 'react';
  2. import styled from '@emotion/styled';
  3. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  4. import Alert from 'sentry/components/alert';
  5. import * as Layout from 'sentry/components/layouts/thirds';
  6. import LoadingError from 'sentry/components/loadingError';
  7. import LoadingIndicator from 'sentry/components/loadingIndicator';
  8. import {t} from 'sentry/locale';
  9. import type {RouteComponentProps} from 'sentry/types/legacyReactRouter';
  10. import type {Organization} from 'sentry/types/organization';
  11. import type {Project} from 'sentry/types/project';
  12. import {useApiQuery} from 'sentry/utils/queryClient';
  13. import normalizeUrl from 'sentry/utils/url/normalizeUrl';
  14. import useApi from 'sentry/utils/useApi';
  15. import {useNavigate} from 'sentry/utils/useNavigate';
  16. import {UptimeAlertForm} from 'sentry/views/alerts/rules/uptime/uptimeAlertForm';
  17. import type {UptimeAlert} from 'sentry/views/alerts/types';
  18. type RouteParams = {
  19. projectId: string;
  20. ruleId: string;
  21. };
  22. type Props = {
  23. onChangeTitle: (data: string) => void;
  24. organization: Organization;
  25. project: Project;
  26. userTeamIds: string[];
  27. } & RouteComponentProps<RouteParams, {}>;
  28. export function UptimeRulesEdit({params, onChangeTitle, organization, project}: Props) {
  29. const api = useApi();
  30. const navigate = useNavigate();
  31. const apiUrl = `/projects/${organization.slug}/${params.projectId}/uptime/${params.ruleId}/`;
  32. const {
  33. isPending,
  34. isSuccess,
  35. isError,
  36. data: rule,
  37. error,
  38. } = useApiQuery<UptimeAlert>([apiUrl], {
  39. staleTime: 0,
  40. retry: false,
  41. });
  42. useEffect(() => {
  43. if (isSuccess && rule) {
  44. onChangeTitle(rule.name ?? '');
  45. }
  46. }, [onChangeTitle, isSuccess, rule]);
  47. if (isPending) {
  48. return <LoadingIndicator />;
  49. }
  50. if (isError) {
  51. if (error?.status === 404) {
  52. return (
  53. <Alert type="error" showIcon>
  54. {t('This alert rule could not be found.')}
  55. </Alert>
  56. );
  57. }
  58. return <LoadingError />;
  59. }
  60. const handleDelete = async () => {
  61. try {
  62. await api.requestPromise(apiUrl, {method: 'DELETE'});
  63. navigate(normalizeUrl(`/organizations/${organization.slug}/alerts/rules/`));
  64. } catch (_err) {
  65. addErrorMessage(t('Error deleting rule'));
  66. }
  67. };
  68. return (
  69. <Main fullWidth>
  70. <UptimeAlertForm
  71. organization={organization}
  72. project={project}
  73. rule={rule}
  74. handleDelete={handleDelete}
  75. />
  76. </Main>
  77. );
  78. }
  79. const Main = styled(Layout.Main)`
  80. max-width: 1000px;
  81. `;