useUpdateGroupSearchViews.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 {
  13. GroupSearchView,
  14. UpdateGroupSearchViewPayload,
  15. } from 'sentry/views/issueList/types';
  16. type UpdateGroupSearchViewsVariables = {
  17. groupSearchViews: UpdateGroupSearchViewPayload[]; // Id is not required in request payload
  18. orgSlug: string;
  19. };
  20. export const useUpdateGroupSearchViews = (
  21. options: Omit<
  22. UseMutationOptions<GroupSearchView[], RequestError, UpdateGroupSearchViewsVariables>,
  23. 'mutationFn'
  24. > = {}
  25. ) => {
  26. const api = useApi();
  27. const queryClient = useQueryClient();
  28. return useMutation<GroupSearchView[], RequestError, UpdateGroupSearchViewsVariables>({
  29. ...options,
  30. mutationFn: ({orgSlug, groupSearchViews}: UpdateGroupSearchViewsVariables) =>
  31. api.requestPromise(`/organizations/${orgSlug}/group-search-views/`, {
  32. method: 'PUT',
  33. data: {views: groupSearchViews},
  34. }),
  35. onSuccess: (groupSearchViews, parameters, context) => {
  36. setApiQueryData<GroupSearchView[]>(
  37. queryClient,
  38. makeFetchGroupSearchViewsKey({orgSlug: parameters.orgSlug}),
  39. groupSearchViews // Update the cache with the new groupSearchViews
  40. );
  41. options.onSuccess?.(groupSearchViews, parameters, context);
  42. },
  43. onError: (error, variables, context) => {
  44. addErrorMessage(t('Failed to update views'));
  45. options.onError?.(error, variables, context);
  46. },
  47. });
  48. };