useUpdateGroupSearchViews.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  2. import {t} from 'sentry/locale';
  3. import {
  4. setApiQueryData,
  5. useMutation,
  6. type UseMutationOptions,
  7. useQueryClient,
  8. } from 'sentry/utils/queryClient';
  9. import type RequestError from 'sentry/utils/requestError/requestError';
  10. import useApi from 'sentry/utils/useApi';
  11. import {makeFetchGroupSearchViewsKey} from 'sentry/views/issueList/queries/useFetchGroupSearchViews';
  12. import type {GroupSearchView} from 'sentry/views/issueList/types';
  13. type UpdateGroupSearchViewsVariables = {
  14. groupSearchViews: GroupSearchView[];
  15. orgSlug: string;
  16. };
  17. // The PUT groupsearchviews endpoint updates the views AND returns the updated views
  18. type UpdateGroupSearchViewResponse = GroupSearchView[];
  19. export const useUpdateGroupSearchViews = (
  20. options: Omit<
  21. UseMutationOptions<
  22. UpdateGroupSearchViewResponse,
  23. RequestError,
  24. UpdateGroupSearchViewsVariables
  25. >,
  26. 'mutationFn'
  27. > = {}
  28. ) => {
  29. const api = useApi();
  30. const queryClient = useQueryClient();
  31. return useMutation<
  32. UpdateGroupSearchViewResponse,
  33. RequestError,
  34. UpdateGroupSearchViewsVariables
  35. >({
  36. ...options,
  37. mutationFn: ({orgSlug, groupSearchViews: data}: UpdateGroupSearchViewsVariables) =>
  38. api.requestPromise(`/organizations/${orgSlug}/group-search-views/`, {
  39. method: 'PUT',
  40. data,
  41. }),
  42. onSuccess: (groupSearchViews, parameters, context) => {
  43. setApiQueryData<GroupSearchView[]>(
  44. queryClient,
  45. makeFetchGroupSearchViewsKey({orgSlug: parameters.orgSlug}),
  46. groupSearchViews // Update the cache with the new groupSearchViews
  47. );
  48. options.onSuccess?.(groupSearchViews, parameters, context);
  49. },
  50. onError: (error, variables, context) => {
  51. addErrorMessage(t('Failed to update views'));
  52. options.onError?.(error, variables, context);
  53. },
  54. });
  55. };