edit.tsx 2.3 KB

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