useModifySavedSearch.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import {SavedSearch, SavedSearchType, SavedSearchVisibility} from 'sentry/types';
  2. import {useMutation, UseMutationOptions, useQueryClient} from 'sentry/utils/queryClient';
  3. import RequestError from 'sentry/utils/requestError/requestError';
  4. import useApi from 'sentry/utils/useApi';
  5. import {makeFetchSavedSearchesForOrgQueryKey} from 'sentry/views/issueList/queries/useFetchSavedSearchesForOrg';
  6. type ModifySavedSearchVariables = {
  7. id: string;
  8. name: string;
  9. orgSlug: string;
  10. query: string;
  11. sort: string | null;
  12. type: SavedSearchType;
  13. visibility: SavedSearchVisibility;
  14. };
  15. type ModifySavedSearchResponse = SavedSearch;
  16. export const useModifySavedSearch = (
  17. options: Omit<
  18. UseMutationOptions<
  19. ModifySavedSearchResponse,
  20. RequestError,
  21. ModifySavedSearchVariables
  22. >,
  23. 'mutationFn'
  24. > = {}
  25. ) => {
  26. const api = useApi();
  27. const queryClient = useQueryClient();
  28. return useMutation<ModifySavedSearchResponse, RequestError, ModifySavedSearchVariables>(
  29. {
  30. ...options,
  31. mutationFn: ({id, orgSlug, ...data}: ModifySavedSearchVariables) =>
  32. api.requestPromise(`/organizations/${orgSlug}/searches/${id}/`, {
  33. method: 'PUT',
  34. data,
  35. }),
  36. onSuccess: (savedSearch, parameters, context) => {
  37. queryClient.setQueryData<SavedSearch[]>(
  38. makeFetchSavedSearchesForOrgQueryKey({orgSlug: parameters.orgSlug}),
  39. oldData => {
  40. if (!Array.isArray(oldData)) {
  41. return oldData;
  42. }
  43. return oldData.map(search =>
  44. search?.id === parameters.id ? savedSearch : search
  45. );
  46. }
  47. );
  48. options.onSuccess?.(savedSearch, parameters, context);
  49. },
  50. }
  51. );
  52. };