/
dafaqtx
/
threads-clone
Обзор
Документация
Войти
/
dafaqtx
/
threads-clone
Код
Запросы
0
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
components/forms/PostThread.tsx
80 строк
2 KB
Alexander Kuchuk
added all pages
14 авг 2023, 20:56
14 авг 2023, 20:56
5c949db
Код
Авторство
О чём код?
"use client"; import { z } from "zod"; import { useForm } from "react-hook-form"; import { Button } from "@/components/ui/button"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Textarea } from "@/components/ui/textarea"; import { zodResolver } from "@hookform/resolvers/zod"; import { usePathname, useRouter } from "next/navigation"; import { ThreadValidation } from "@/lib/validations/thread"; import { createThread } from "@/lib/actions/thread.actions"; type ThreadSchema = z.infer<typeof ThreadValidation>; export function PostThread({ userId }: { userId: string }) { const path = usePathname(); const router = useRouter(); const form = useForm<ThreadSchema>({ resolver: zodResolver(ThreadValidation), defaultValues: { thread: "", accountId: userId, }, }); const onSubmit = async (values: ThreadSchema) => { await createThread({ text: values.thread, author: userId, communityId: null, path, }); router.push("/"); }; return ( <Form {...form}> <form onSubmit={form.handleSubmit(onSubmit)} className="mt-10 flex flex-col justify-start gap-10" > <FormField control={form.control} name="thread" render={({ field }) => ( <FormItem className="flex flex-col gap-3 w-full"> <FormLabel className="text-base-semibold text-light-2"> Content </FormLabel> <FormControl className="no-focus border border-dark-4 bg-dark-3 text-light-1"> <Textarea rows={15} className="account-form_input no-focus" {...field} /> </FormControl> <FormMessage /> </FormItem> )} /> <Button type="submit" className="bg-primary-500"> Post Thread </Button> </form> </Form> ); } export default PostThread;