@@ -28,12 +28,21 @@ import {
2828 PopoverContent ,
2929 PopoverTrigger ,
3030} from '@/components/ui/popover'
31- import { useRouter } from 'next/navigation'
3231import { getListLibraries } from '@/lib/api/library'
3332import { Library } from '@/lib/types/library'
3433import { useState , useEffect , useCallback } from 'react'
35- import { Input } from '../ui/input'
36- import { createBook } from '@/lib/api/book'
34+ import { Input } from '@/components/ui/input'
35+ import { Book } from '@/lib/types/book'
36+
37+ export type BookFormValues = Pick <
38+ Book ,
39+ 'title' | 'author' | 'year' | 'code' | 'library_id'
40+ >
41+
42+ type BookFormProps = {
43+ initialData : BookFormValues
44+ onSubmit ( data : BookFormValues ) : void
45+ }
3746
3847const FormSchema = z . object ( {
3948 title : z
@@ -63,18 +72,13 @@ const FormSchema = z.object({
6372 . uuid ( ) ,
6473} )
6574
66- export const CreateBookForm : React . FC = ( ) => {
67- const router = useRouter ( )
68-
75+ export const BookForm : React . FC < BookFormProps > = ( {
76+ initialData,
77+ onSubmit,
78+ } ) => {
6979 const form = useForm < z . infer < typeof FormSchema > > ( {
7080 resolver : zodResolver ( FormSchema ) ,
71- defaultValues : {
72- title : '' ,
73- author : '' ,
74- code : '' ,
75- year : 0 ,
76- library_id : '' ,
77- } ,
81+ defaultValues : initialData ,
7882 } )
7983
8084 const [ libQ , setLibQ ] = useState ( '' )
@@ -96,24 +100,6 @@ export const CreateBookForm: React.FC = () => {
96100 } )
97101 } , [ libQ ] )
98102
99- function onSubmit ( data : z . infer < typeof FormSchema > ) {
100- createBook ( data )
101- . then ( console . log )
102- . then ( ( ) => {
103- toast ( {
104- title : 'Book Registered' ,
105- } )
106- router . push ( '/books' )
107- } )
108- . catch ( ( e ) => {
109- toast ( {
110- title : 'Error' ,
111- description : e ?. error ,
112- variant : 'destructive' ,
113- } )
114- } )
115- }
116-
117103 const onReset = useCallback ( ( ) => {
118104 form . reset ( )
119105 } , [ form ] )
0 commit comments