40 lines
976 B
TypeScript
40 lines
976 B
TypeScript
import { useMutation } from "@tanstack/react-query"
|
|
import { toast } from "sonner"
|
|
import type z from "zod"
|
|
import { createProfile } from "@/lib/server/user"
|
|
import { profileFormSchema } from "@/lib/validation/user"
|
|
import { useValidation } from "../useValidation"
|
|
|
|
type TProfileForm = z.infer<typeof profileFormSchema>
|
|
|
|
export const useProfile = () => {
|
|
const { validate, errors } = useValidation({
|
|
defaultSchema: profileFormSchema
|
|
})
|
|
const signup = useMutation({
|
|
mutationKey: ["create-profile"],
|
|
mutationFn: async (data: TProfileForm) => createProfile({ data }),
|
|
onSuccess: () => {
|
|
toast.success("Your profile is created..", {
|
|
id: "create-profile"
|
|
})
|
|
}
|
|
})
|
|
|
|
const validateSignup = (formData: TProfileForm) => {
|
|
const isValid = validate({ formData })
|
|
if (!isValid) {
|
|
toast.error("Don't create", {
|
|
id: "create-profile"
|
|
})
|
|
}
|
|
signup.mutate(formData)
|
|
}
|
|
|
|
return {
|
|
profile: validateSignup,
|
|
errors,
|
|
isPending: signup.isPending
|
|
}
|
|
}
|