edit.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import {useEffect} from 'react';
  2. import styled from '@emotion/styled';
  3. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  4. import {Alert} from 'sentry/components/core/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 useApi from 'sentry/utils/useApi';
  14. import {useNavigate} from 'sentry/utils/useNavigate';
  15. import {makeAlertsPathname} from 'sentry/views/alerts/pathnames';
  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.Container>
  54. <Alert type="error" showIcon>
  55. {t('This alert rule could not be found.')}
  56. </Alert>
  57. </Alert.Container>
  58. );
  59. }
  60. return <LoadingError />;
  61. }
  62. const handleDelete = async () => {
  63. try {
  64. await api.requestPromise(apiUrl, {method: 'DELETE'});
  65. navigate(
  66. makeAlertsPathname({
  67. path: `/rules/`,
  68. organization,
  69. })
  70. );
  71. } catch (_err) {
  72. addErrorMessage(t('Error deleting rule'));
  73. }
  74. };
  75. return (
  76. <Main fullWidth>
  77. <UptimeAlertForm
  78. organization={organization}
  79. project={project}
  80. rule={rule}
  81. handleDelete={handleDelete}
  82. />
  83. </Main>
  84. );
  85. }
  86. const Main = styled(Layout.Main)`
  87. max-width: 1000px;
  88. `;