({
onSuccess,
onError,
}: UseProfileFormProps = {})
| 70 | * @deprecated Use useUserInfoForm instead |
| 71 | */ |
| 72 | const useProfileForm = ({ |
| 73 | onSuccess, |
| 74 | onError, |
| 75 | }: UseProfileFormProps = {}): UseProfileForm => { |
| 76 | const { user, updateUser } = useContext(AuthContext); |
| 77 | const [hint, setHint] = useState<ProfileFormHint>({}); |
| 78 | const { isPending: isLoading, mutate: updateUserProfile } = useMutation< |
| 79 | LoggedUser, |
| 80 | ResponseError, |
| 81 | UpdateProfileParameters |
| 82 | >({ |
| 83 | mutationFn: async ({ upload, coverUpload, onUpdateSuccess, ...data }) => { |
| 84 | const res = await gqlClient.request<{ updateUserProfile: LoggedUser }>( |
| 85 | UPDATE_USER_PROFILE_MUTATION, |
| 86 | { |
| 87 | data, |
| 88 | upload, |
| 89 | coverUpload, |
| 90 | }, |
| 91 | ); |
| 92 | |
| 93 | return res?.updateUserProfile; |
| 94 | }, |
| 95 | |
| 96 | onSuccess: async ( |
| 97 | updatedUser, |
| 98 | { onUpdateSuccess, upload, coverUpload, ...userUpdates }, |
| 99 | ) => { |
| 100 | setHint({}); |
| 101 | |
| 102 | if (user) { |
| 103 | await updateUser({ ...user, ...userUpdates, ...updatedUser }); |
| 104 | } |
| 105 | |
| 106 | onUpdateSuccess?.(); |
| 107 | onSuccess?.(); |
| 108 | }, |
| 109 | onError: (err) => { |
| 110 | onError?.(err); |
| 111 | |
| 112 | if (!err?.response?.errors?.length) { |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | const data = parseProfileFormHint(err.response.errors[0]?.message); |
| 117 | |
| 118 | if (!data) { |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | setHint(data); |
| 123 | }, |
| 124 | }); |
| 125 | |
| 126 | return { |
| 127 | hint, |
| 128 | isLoading, |
| 129 | onUpdateHint: setHint, |
no test coverage detected