({
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: ({ upload, coverUpload, onUpdateSuccess, ...data }) => |
| 84 | gqlClient.request(UPDATE_USER_PROFILE_MUTATION, { |
| 85 | data, |
| 86 | upload, |
| 87 | coverUpload, |
| 88 | }), |
| 89 | |
| 90 | onSuccess: async ( |
| 91 | _, |
| 92 | { onUpdateSuccess, upload, coverUpload, ...userUpdates }, |
| 93 | ) => { |
| 94 | setHint({}); |
| 95 | |
| 96 | if (user) { |
| 97 | await updateUser({ ...user, ...userUpdates }); |
| 98 | } |
| 99 | |
| 100 | onUpdateSuccess?.(); |
| 101 | onSuccess?.(); |
| 102 | }, |
| 103 | onError: (err) => { |
| 104 | onError?.(err); |
| 105 | |
| 106 | if (!err?.response?.errors?.length) { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | const firstError = err.response.errors[0]; |
| 111 | if (!firstError?.message) { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | const data: ProfileFormHint = JSON.parse(firstError.message); |
| 116 | setHint(data); |
| 117 | }, |
| 118 | }); |
| 119 | |
| 120 | return { |
| 121 | hint, |
| 122 | isLoading, |
| 123 | onUpdateHint: setHint, |
| 124 | updateUserProfile, |
| 125 | }; |
| 126 | }; |
| 127 | |
| 128 | export default useProfileForm; |
no test coverage detected