|
1 |
| -import { useRouter } from 'next/router'; |
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { yupResolver } from '@hookform/resolvers/yup'; |
| 4 | +import { useRouter, useSearchParams } from 'next/navigation'; |
| 5 | +import { useEffect, useState } from 'react'; |
| 6 | +import { SubmitHandler, useForm } from 'react-hook-form'; |
| 7 | +import * as yup from 'yup'; |
| 8 | + |
| 9 | +import { FormValues } from './types'; |
| 10 | + |
| 11 | +import { Input } from '@/shared/components/FormComponents'; |
| 12 | +import { Button } from '@/shared/components/ui/button'; |
| 13 | +import { useToast } from '@/shared/components/ui/use-toast'; |
| 14 | +import useAxiosAuth from '@/shared/hooks/useAxiosAuth'; |
| 15 | +import Chapter from '@/shared/models/chapter'; |
| 16 | + |
| 17 | +const validationSchema: yup.ObjectSchema<FormValues> = yup.object({ |
| 18 | + title: yup |
| 19 | + .string() |
| 20 | + .required('No title provided.') |
| 21 | + .min(10, 'Title too short.') |
| 22 | + .max(124, 'Title too long.'), |
| 23 | + chapterNumber: yup.number().required('No chapter number provided.'), |
| 24 | + createdOn: yup.string().required('No release date provided.'), |
| 25 | +}); |
2 | 26 |
|
3 | 27 | function Page() {
|
| 28 | + const [chapter, setChapter] = useState<Chapter | null>(null); |
| 29 | + |
4 | 30 | const router = useRouter();
|
5 |
| - const { mangaId, chapterId } = router.query; |
| 31 | + const queryParams = useSearchParams(); |
| 32 | + |
| 33 | + const axiosAuth = useAxiosAuth(); |
| 34 | + const { toast } = useToast(); |
| 35 | + const { |
| 36 | + register, |
| 37 | + handleSubmit, |
| 38 | + setValue, |
| 39 | + formState: { errors }, |
| 40 | + } = useForm({ |
| 41 | + resolver: yupResolver(validationSchema), |
| 42 | + }); |
| 43 | + |
| 44 | + const mangaId = queryParams.get('mangaId'); |
| 45 | + const chapterId = queryParams.get('chapterId'); |
| 46 | + |
| 47 | + const fetchChapter = async () => { |
| 48 | + const res = await axiosAuth.get<Chapter>(`Chapters`, { |
| 49 | + params: { chapterId }, |
| 50 | + }); |
| 51 | + |
| 52 | + setChapter(res.data); |
| 53 | + setValue('title', res.data.title); |
| 54 | + setValue('chapterNumber', res.data.chapterNumber); |
| 55 | + setValue( |
| 56 | + 'createdOn', |
| 57 | + new Date(res.data.createdOn).toISOString().slice(0, 10), |
| 58 | + ); |
| 59 | + }; |
| 60 | + |
| 61 | + useEffect(() => { |
| 62 | + fetchChapter(); |
| 63 | + }, []); |
| 64 | + |
| 65 | + const onSubmit: SubmitHandler<FormValues> = async (data) => { |
| 66 | + try { |
| 67 | + await axiosAuth.post<unknown, unknown, unknown>('Chapters', { |
| 68 | + ...chapter, |
| 69 | + ...data, |
| 70 | + chapterId, |
| 71 | + mangaId, |
| 72 | + }); |
| 73 | + |
| 74 | + toast({ |
| 75 | + title: 'Success', |
| 76 | + description: `Chapter "${data.title}" was successfully created!`, |
| 77 | + }); |
| 78 | + |
| 79 | + router.push(`/manga/${mangaId}`); |
| 80 | + } catch (error) { |
| 81 | + toast({ |
| 82 | + title: 'Error occurred!', |
| 83 | + variant: 'destructive', |
| 84 | + }); |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + if (!chapter) { |
| 89 | + return <div>Loading...</div>; |
| 90 | + } |
6 | 91 |
|
7 | 92 | return (
|
8 |
| - <div> |
9 |
| - <h1>Page</h1> |
10 |
| - <p>Param1: {mangaId}</p> |
11 |
| - <p>Param2: {chapterId}</p> |
| 93 | + <div className="flex flex-col items-center gap-6 pt-10"> |
| 94 | + <p className="text-2xl">Edit chapter</p> |
| 95 | + <form |
| 96 | + className="flex w-[50%] flex-col gap-6 lg:w-[50rem]" |
| 97 | + onSubmit={handleSubmit(onSubmit)} |
| 98 | + > |
| 99 | + <Input |
| 100 | + label="title" |
| 101 | + register={register} |
| 102 | + error={errors?.title?.message} |
| 103 | + /> |
| 104 | + <Input |
| 105 | + label="chapterNumber" |
| 106 | + type="number" |
| 107 | + defaultValue={1} |
| 108 | + min={0} |
| 109 | + register={register} |
| 110 | + error={errors?.chapterNumber?.message} |
| 111 | + /> |
| 112 | + <Input |
| 113 | + label="createdOn" |
| 114 | + type="date" |
| 115 | + register={register} |
| 116 | + error={errors?.createdOn?.message} |
| 117 | + // defaultValue={new Date().toISOString().slice(0, 10)} |
| 118 | + /> |
| 119 | + <Button type="submit">Submit</Button> |
| 120 | + </form> |
12 | 121 | </div>
|
13 | 122 | );
|
14 | 123 | }
|
|
0 commit comments