|
| 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 | + |
| 14 | +type UpdateGroupSearchViewsVariables = { |
| 15 | + groupSearchViews: GroupSearchView[]; |
| 16 | + orgSlug: string; |
| 17 | +}; |
| 18 | + |
| 19 | +// The PUT groupsearchviews endpoint updates the views AND returns the updated views |
| 20 | +type UpdateGroupSearchViewResponse = GroupSearchView[]; |
| 21 | + |
| 22 | +export const useUpdateGroupSearchViews = ( |
| 23 | + options: Omit< |
| 24 | + UseMutationOptions< |
| 25 | + UpdateGroupSearchViewResponse, |
| 26 | + RequestError, |
| 27 | + UpdateGroupSearchViewsVariables |
| 28 | + >, |
| 29 | + 'mutationFn' |
| 30 | + > = {} |
| 31 | +) => { |
| 32 | + const api = useApi(); |
| 33 | + const queryClient = useQueryClient(); |
| 34 | + |
| 35 | + return useMutation< |
| 36 | + UpdateGroupSearchViewResponse, |
| 37 | + RequestError, |
| 38 | + UpdateGroupSearchViewsVariables |
| 39 | + >({ |
| 40 | + ...options, |
| 41 | + mutationFn: ({orgSlug, groupSearchViews: data}: UpdateGroupSearchViewsVariables) => |
| 42 | + api.requestPromise(`/organizations/${orgSlug}/group-search-views/`, { |
| 43 | + method: 'PUT', |
| 44 | + data, |
| 45 | + }), |
| 46 | + onSuccess: (groupSearchViews, parameters, context) => { |
| 47 | + setApiQueryData<GroupSearchView[]>( |
| 48 | + queryClient, |
| 49 | + makeFetchGroupSearchViewsKey({orgSlug: parameters.orgSlug}), |
| 50 | + groupSearchViews // Update the cache with the new groupSearchViews |
| 51 | + ); |
| 52 | + options.onSuccess?.(groupSearchViews, parameters, context); |
| 53 | + }, |
| 54 | + onError: (error, variables, context) => { |
| 55 | + addErrorMessage(t('Failed to update views')); |
| 56 | + options.onError?.(error, variables, context); |
| 57 | + }, |
| 58 | + }); |
| 59 | +}; |
0 commit comments