Rank 1: Thread
import { zodResolver } from "@hookform/resolvers/zod"
import { useForm } from "react-hook-form"
import * as z from "zod"
import { useNavigate } from "react-router-dom";
import { Button } from "@/components/ui/button"
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage,
} from "@/components/ui/form"
import { Input } from "@/components/ui/input"
import { Textarea } from "../ui/textarea"
import FileUploader from "../shared/FileUploader"
import { PostValidation } from "@/lib/validation"
import { Models } from "appwrite"
import { useUserContext } from "@/context/AuthContext"
import { useToast } from "../ui/use-toast"
import { useCreatePost } from "@/lib/react-query/queriesAndMutations";
type PostFormProps = {
post?: Models.Document;
}
const PostForm = ({ post }: PostFormProps) => {
const { mutateAsync: createPost, isPending: isLoadingCreate } = useCreatePost();
const { user } = useUserContext();
const { toast } = useToast();
const navigate = useNavigate();
// 1. Define your form schema.
const form = useForm<z.infer>({
resolver: zodResolver(PostValidation),
defaultValues: {
caption: post ? post?.caption : "",
file: [],
location: post ? post?.location : "",
tags: post ? post.tags.join(',') : ""
},
})
// 2. Define a submit handler.
async function onSubmit(values: z.infer) {
const newPost = await createPost({
...values,
userId: user.id,
})
if(!newPost) {
toast({
title: 'Please try again'
})
}
navigate('/');