@@ -23,7 +23,7 @@ import {
2323import { Tabs , TabsContent , TabsList , TabsTrigger } from '@/components/ui/tabs'
2424import { Badge } from '@/components/ui/badge'
2525import { Separator } from '@/components/ui/separator'
26- import { useRouter , useSearchParams } from 'next/navigation'
26+ import { useRouter } from 'next/navigation'
2727import { z } from 'zod'
2828import { User } from '@/lib/types/user'
2929import { getListUsers } from '@/lib/api/user'
@@ -67,9 +67,7 @@ import { Scanner } from '../common/Scanner'
6767import Image from 'next/image'
6868
6969const FormSchema = z . object ( {
70- user_id : z . string ( {
71- required_error : 'Please select a user.' ,
72- } ) ,
70+ user_id : z . string ( ) . optional ( ) ,
7371 book_id : z . string ( {
7472 required_error : 'Please select a book.' ,
7573 } ) ,
@@ -90,13 +88,9 @@ type FormBorrowProps = {
9088
9189export const FormBorrow : React . FC < FormBorrowProps > = ( props ) => {
9290 const router = useRouter ( )
93- const searchParams = useSearchParams ( )
94- const selectedMethod = searchParams . get ( 'tab' ) || 'manual'
95- const setSelectedMethod = ( value : 'manual' | 'qr' ) => {
96- const newParams = new URLSearchParams ( searchParams . toString ( ) )
97- newParams . set ( 'tab' , value )
98- router . replace ( `?${ newParams . toString ( ) } ` )
99- }
91+ const [ selectedMethod , setSelectedMethod ] = useState < 'manual' | 'qr' > (
92+ 'manual'
93+ )
10094
10195 const form = useForm < z . infer < typeof FormSchema > > ( {
10296 resolver : zodResolver ( FormSchema ) ,
@@ -337,7 +331,7 @@ export const FormBorrow: React.FC<FormBorrowProps> = (props) => {
337331 </ CardContent >
338332 </ Card >
339333
340- { selectedUser && (
334+ { ( selectedUser || form . formState . errors . subscription_id ) && (
341335 < Card >
342336 < CardHeader >
343337 < CardTitle
@@ -409,7 +403,7 @@ export const FormBorrow: React.FC<FormBorrowProps> = (props) => {
409403 </ Card >
410404 ) }
411405
412- { selectedSubscription && (
406+ { ( selectedSubscription || form . formState . errors . book_id ) && (
413407 < Card >
414408 < CardHeader >
415409 < CardTitle
@@ -506,7 +500,7 @@ export const FormBorrow: React.FC<FormBorrowProps> = (props) => {
506500 </ Card >
507501 ) }
508502
509- { selectedBook && (
503+ { ( selectedBook || form . formState . errors . staff_id ) && (
510504 < Card >
511505 < CardHeader >
512506 < CardTitle
@@ -597,14 +591,15 @@ export const FormBorrow: React.FC<FormBorrowProps> = (props) => {
597591 < FormField
598592 control = { form . control }
599593 name = "subscription_id"
600- render = { ( { field } ) => (
594+ render = { ( { field, fieldState : { error } } ) => (
601595 < Scanner
602596 title = "Scan Subscription QR"
603597 description = "Select to scan subscription QR code"
604598 onChange = { ( id ) => {
605599 setSubQ ( { id } )
606600 field . onChange ( id )
607601 } }
602+ error = { error ?. message }
608603 >
609604 { selectedSubscription && (
610605 < div className = "p-4 border border-primary/40 bg-primary/5 rounded-lg" >
@@ -635,14 +630,15 @@ export const FormBorrow: React.FC<FormBorrowProps> = (props) => {
635630 < FormField
636631 control = { form . control }
637632 name = "book_id"
638- render = { ( { field } ) => (
633+ render = { ( { field, fieldState : { error } } ) => (
639634 < Scanner
640635 title = "Scan Book QR"
641636 description = "Select to scan book QR code"
642637 onChange = { ( id ) => {
643638 setBookQ ( { id } )
644639 field . onChange ( id )
645640 } }
641+ error = { error ?. message }
646642 >
647643 { selectedBook && (
648644 < div className = "p-4 border border-primary/40 bg-primary/5 rounded-lg" >
0 commit comments