@@ -30,18 +30,6 @@ import { SeatsSelect } from "@/components/ui/Inputs/SeatSelect";
3030import { CustomTextarea } from "@/components/ui/Inputs/CustomTextarea" ;
3131import { onError } from "@/utils/formUtils" ;
3232
33- const FIELD_LABELS : Record < string , string > = {
34- departureLocation : "Departure location" ,
35- arrivalLocation : "Arrival location" ,
36- dateTrip : "Travel Date" ,
37- time : "Time" ,
38- availableSeats : "Available seats" ,
39- pricePerSeat : "Price per seat" ,
40- smoking : "Smoking" ,
41- petsAllowed : "Pets allowed" ,
42- additionalInfo : "Additional info" ,
43- } ;
44-
4533export default function CreateRoute ( ) {
4634 const { register, control, handleSubmit } = useForm < CreateRouteFormValues > ( {
4735 resolver : zodResolver ( createRouteSchema ) ,
@@ -94,26 +82,28 @@ export default function CreateRoute() {
9482 mutation . mutate ( body ) ;
9583 } ;
9684
97- const onError = ( errors : FieldErrors ) => {
98- const firstErrorKey = Object . keys ( errors ) [ 0 ] ;
99- const firstError = errors [ firstErrorKey as keyof CreateRouteFormValues ] ;
85+ const handleOnError = ( errors : FieldErrors ) => {
86+ const FIELD_LABELS : Record < string , string > = {
87+ departureLocation : "Departure location" ,
88+ arrivalLocation : "Arrival location" ,
89+ dateTrip : "Travel Date" ,
90+ time : "Time" ,
91+ availableSeats : "Available seats" ,
92+ pricePerSeat : "Price per seat" ,
93+ smoking : "Smoking" ,
94+ petsAllowed : "Pets allowed" ,
95+ additionalInfo : "Additional info" ,
96+ } ;
10097
101- if ( firstError ?. message && firstErrorKey ) {
102- const fieldLabel = FIELD_LABELS [ firstErrorKey ] || firstErrorKey ;
103- toast . error ( `${ fieldLabel } : ${ firstError . message } ` ) ;
104- } else {
105- toast . error ( "Please check the form for errors" ) ;
106- }
98+ onError ( FIELD_LABELS , errors ) ;
10799 } ;
108100
109101 return (
110102 < section className = "flex justify-center w-full py-8 px-4" >
111103 < div className = "flex flex-col gap-8 max-w-xl w-full" >
112104 { /* Header */ }
113105 < div className = "flex flex-col gap-2" >
114- < Typography variant = "h2" >
115- Offer a ride
116- </ Typography >
106+ < Typography variant = "h2" > Offer a ride</ Typography >
117107 < Typography className = "text-muted-foreground text-lg" >
118108 Share your journey and split the costs. Fill in the details below to
119109 post your ride.
@@ -122,7 +112,7 @@ export default function CreateRoute() {
122112
123113 < form
124114 id = "create-route"
125- onSubmit = { handleSubmit ( onSubmit , onError ) }
115+ onSubmit = { handleSubmit ( onSubmit , handleOnError ) }
126116 className = "flex flex-col gap-6 w-full"
127117 noValidate
128118 >
0 commit comments