@@ -11,29 +11,40 @@ export function useToastMutation<
11
11
TError = DefaultError ,
12
12
TVariables = void ,
13
13
TContext = unknown ,
14
- > ( options : UseMutationOptions < TData , TError , TVariables , TContext > ) {
14
+ > ( {
15
+ successMsg,
16
+ errorMsg,
17
+ loadingMsg,
18
+ ...options
19
+ } : UseMutationOptions < TData , TError , TVariables , TContext > & {
20
+ successMsg ?: ( ( variables : TVariables ) => string ) | string ;
21
+ loadingMsg ?: string ;
22
+ errorMsg ?: string ;
23
+ } ) {
15
24
const {
16
25
mutateAsync : originalMutateAsync ,
17
- // NOTE: We are not allowing direct use of the `mutate` (sync) function.
26
+ // NOTE: We are not allowing direct use of the `mutate` (sync) function
18
27
// eslint-disable-next-line @typescript-eslint/no-unused-vars
19
28
mutate : _ ,
20
29
...rest
21
30
} = useMutation ( options ) ;
22
31
23
32
const mutateAsync = useCallback (
24
- < TError extends { detail : string | undefined } > (
33
+ async < TError extends { detail : string | undefined } > (
25
34
variables : Parameters < typeof originalMutateAsync > [ 0 ] ,
26
- options : Parameters < typeof originalMutateAsync > [ 1 ] ,
27
- { successMsg } : { successMsg : string } ,
35
+ options : Parameters < typeof originalMutateAsync > [ 1 ] = { } ,
28
36
) => {
29
37
const promise = originalMutateAsync ( variables , options ) ;
30
38
31
39
toast . promise ( promise , {
32
- success : successMsg ,
33
- error : ( e : TError ) => ( e . detail ? e . detail : "An error occurred" ) ,
40
+ success :
41
+ typeof successMsg === "function" ? successMsg ( variables ) : successMsg ,
42
+ loading : loadingMsg ?? "Loading..." ,
43
+ error : ( e : TError ) =>
44
+ errorMsg ?? ( e . detail ? e . detail : "An error occurred" ) ,
34
45
} ) ;
35
46
} ,
36
- [ originalMutateAsync ] ,
47
+ [ errorMsg , loadingMsg , originalMutateAsync , successMsg ] ,
37
48
) ;
38
49
39
50
return { mutateAsync, ...rest } ;
0 commit comments