|
1 | 1 | import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query' |
| 2 | +import toast from 'react-hot-toast' |
2 | 3 |
|
3 | 4 | import { components } from 'data/api' |
4 | 5 | import { put } from 'data/fetchers' |
5 | | -import { contentKeys } from './keys' |
| 6 | +import { ResponseError } from 'types' |
6 | 7 | import { Content } from './content-query' |
| 8 | +import { contentKeys } from './keys' |
7 | 9 |
|
8 | 10 | export type UpsertContentPayload = Omit<components['schemas']['UpsertContentParams'], 'content'> & { |
9 | 11 | content: Content['content'] |
@@ -41,24 +43,30 @@ export async function upsertContent( |
41 | 43 | export type UpsertContentData = Awaited<ReturnType<typeof upsertContent>> |
42 | 44 |
|
43 | 45 | export const useContentUpsertMutation = ({ |
| 46 | + onError, |
44 | 47 | onSuccess, |
45 | 48 | ...options |
46 | 49 | }: Omit< |
47 | | - UseMutationOptions<UpsertContentData, unknown, UpsertContentVariables>, |
| 50 | + UseMutationOptions<UpsertContentData, ResponseError, UpsertContentVariables>, |
48 | 51 | 'mutationFn' |
49 | 52 | > = {}) => { |
50 | 53 | const queryClient = useQueryClient() |
51 | 54 |
|
52 | | - return useMutation<UpsertContentData, unknown, UpsertContentVariables>( |
| 55 | + return useMutation<UpsertContentData, ResponseError, UpsertContentVariables>( |
53 | 56 | (args) => upsertContent(args), |
54 | 57 | { |
55 | 58 | async onSuccess(data, variables, context) { |
56 | 59 | const { projectRef } = variables |
57 | | - |
58 | | - await Promise.all([queryClient.invalidateQueries(contentKeys.list(projectRef))]) |
59 | | - |
| 60 | + await queryClient.invalidateQueries(contentKeys.list(projectRef)) |
60 | 61 | await onSuccess?.(data, variables, context) |
61 | 62 | }, |
| 63 | + async onError(data, variables, context) { |
| 64 | + if (onError === undefined) { |
| 65 | + toast.error(`Failed to insert content: ${data.message}`) |
| 66 | + } else { |
| 67 | + onError(data, variables, context) |
| 68 | + } |
| 69 | + }, |
62 | 70 | ...options, |
63 | 71 | } |
64 | 72 | ) |
|
0 commit comments