@@ -2,7 +2,8 @@ import React from 'react';
2
2
3
3
import { Button , ButtonGroup , Stack } from '@chakra-ui/react' ;
4
4
import { zodResolver } from '@hookform/resolvers/zod' ;
5
- import { SubmitHandler , useForm } from 'react-hook-form' ;
5
+ import { useMutation } from '@tanstack/react-query' ;
6
+ import { useForm } from 'react-hook-form' ;
6
7
import { useTranslation } from 'react-i18next' ;
7
8
8
9
import { ErrorPage } from '@/components/ErrorPage' ;
@@ -22,7 +23,7 @@ import {
22
23
AVAILABLE_LANGUAGES ,
23
24
DEFAULT_LANGUAGE_KEY ,
24
25
} from '@/lib/i18n/constants' ;
25
- import { useUploadFileMutation } from '@/lib/s3/client' ;
26
+ import { uploadFile } from '@/lib/s3/client' ;
26
27
import { FILES_COLLECTIONS_CONFIG } from '@/lib/s3/config' ;
27
28
import { trpc } from '@/lib/trpc/client' ;
28
29
@@ -33,9 +34,24 @@ export const AccountProfileForm = () => {
33
34
staleTime : Infinity ,
34
35
} ) ;
35
36
36
- const uploadAvatar = useUploadFileMutation ( 'avatar' ) ;
37
-
38
- const updateAccount = trpc . account . update . useMutation ( {
37
+ const updateAccount = useMutation ( {
38
+ mutationFn : async ( { image, ...values } : FormFieldsAccountProfile ) => {
39
+ return await trpcUtils . client . account . update . mutate ( {
40
+ ...values ,
41
+ image : image ?. file
42
+ ? await uploadFile ( {
43
+ trpcClient : trpcUtils . client ,
44
+ collection : 'avatar' ,
45
+ file : image . file ,
46
+ onError : ( ) => {
47
+ form . setError ( 'image' , {
48
+ message : t ( 'account:profile.feedbacks.uploadError.title' ) ,
49
+ } ) ;
50
+ } ,
51
+ } )
52
+ : account . data ?. image ,
53
+ } ) ;
54
+ } ,
39
55
onSuccess : async ( ) => {
40
56
await trpcUtils . account . invalidate ( ) ;
41
57
toastCustom ( {
@@ -61,31 +77,18 @@ export const AccountProfileForm = () => {
61
77
} ,
62
78
} ) ;
63
79
64
- const onSubmit : SubmitHandler < FormFieldsAccountProfile > = async ( {
65
- image,
66
- ...values
67
- } ) => {
68
- try {
69
- updateAccount . mutate ( {
70
- ...values ,
71
- image : image ?. file
72
- ? await uploadAvatar . mutateAsync ( image . file )
73
- : account . data ?. image ,
74
- } ) ;
75
- } catch {
76
- form . setError ( 'image' , {
77
- message : t ( 'account:profile.feedbacks.uploadError.title' ) ,
78
- } ) ;
79
- }
80
- } ;
81
-
82
80
return (
83
81
< >
84
82
{ account . isLoading && < LoaderFull /> }
85
83
{ account . isError && < ErrorPage /> }
86
84
{ account . isSuccess && (
87
85
< Stack spacing = { 4 } >
88
- < Form { ...form } onSubmit = { onSubmit } >
86
+ < Form
87
+ { ...form }
88
+ onSubmit = { ( values ) => {
89
+ updateAccount . mutate ( values ) ;
90
+ } }
91
+ >
89
92
< Stack spacing = { 4 } >
90
93
< FormField >
91
94
< FormFieldLabel >
@@ -129,7 +132,7 @@ export const AccountProfileForm = () => {
129
132
< Button
130
133
type = "submit"
131
134
variant = "@primary"
132
- isLoading = { updateAccount . isLoading || uploadAvatar . isLoading }
135
+ isLoading = { updateAccount . isLoading }
133
136
>
134
137
{ t ( 'account:profile.actions.update' ) }
135
138
</ Button >
0 commit comments