1
+ // ./components/BookingForm.tsx
1
2
import React , { useState , useEffect } from 'react' ;
2
3
import { databases } from '../lib/appwrite.config' ;
4
+ import { useRouter } from 'next/router' ;
3
5
4
6
interface BookingFormProps {
5
7
providerId : string ;
@@ -14,6 +16,25 @@ const BookingForm: React.FC<BookingFormProps> = ({ providerId, serviceId, select
14
16
const [ zipcode , setZipcode ] = useState ( '' ) ;
15
17
const [ coupon , setCoupon ] = useState ( '' ) ;
16
18
const [ discount , setDiscount ] = useState ( 0 ) ;
19
+ const router = useRouter ( ) ;
20
+ const consumerId = JSON . parse ( localStorage . getItem ( 'appwriteSession' ) || '{}' ) . userId ;
21
+
22
+ useEffect ( ( ) => {
23
+ const fetchServiceDetails = async ( ) => {
24
+ try {
25
+ const service = await databases . getDocument (
26
+ process . env . DATABASE_ID ! ,
27
+ process . env . SERVICE_COLLECTION_ID ! ,
28
+ serviceId
29
+ ) ;
30
+ setDiscount ( service . price ) ;
31
+ } catch ( error ) {
32
+ console . error ( 'Error fetching service details:' , error ) ;
33
+ }
34
+ } ;
35
+
36
+ fetchServiceDetails ( ) ;
37
+ } , [ serviceId ] ) ;
17
38
18
39
const handleCouponApply = async ( ) => {
19
40
if ( coupon === '100OFF' ) {
@@ -23,11 +44,12 @@ const BookingForm: React.FC<BookingFormProps> = ({ providerId, serviceId, select
23
44
}
24
45
} ;
25
46
26
- const handleSubmit = async ( e : React . FormEvent ) => {
47
+ const handleFormSubmit = async ( e : React . FormEvent ) => {
27
48
e . preventDefault ( ) ;
28
-
29
- const session = JSON . parse ( localStorage . getItem ( 'appwriteSession' ) || '{}' ) ;
30
- const consumerId = session . userId ;
49
+ if ( ! selectedDate ) {
50
+ alert ( "Please select a date for booking." ) ;
51
+ return ;
52
+ }
31
53
32
54
const formData = {
33
55
date : selectedDate . toISOString ( ) ,
@@ -39,7 +61,7 @@ const BookingForm: React.FC<BookingFormProps> = ({ providerId, serviceId, select
39
61
city,
40
62
state,
41
63
zipcode,
42
- discount,
64
+ discount
43
65
} ;
44
66
45
67
try {
@@ -53,8 +75,7 @@ const BookingForm: React.FC<BookingFormProps> = ({ providerId, serviceId, select
53
75
54
76
if ( response . ok ) {
55
77
const responseData = await response . json ( ) ;
56
- console . log ( 'Booking created successfully.' ) ;
57
- window . location . href = `/payment-confirmation?bookingId=${ responseData . bookingId } ` ;
78
+ router . push ( `/payment-confirmation?bookingId=${ responseData . bookingId } ` ) ;
58
79
} else {
59
80
console . error ( 'Failed to create booking.' ) ;
60
81
}
@@ -64,14 +85,14 @@ const BookingForm: React.FC<BookingFormProps> = ({ providerId, serviceId, select
64
85
} ;
65
86
66
87
return (
67
- < form onSubmit = { handleSubmit } className = "mt-4" >
88
+ < form onSubmit = { handleFormSubmit } className = "mt-4" >
68
89
< div >
69
- < label className = "block text-sm font-medium text-gray-700" > Selected Date</ label >
90
+ < label className = "block text-sm font-medium text-gray-700" > Date</ label >
70
91
< input
71
92
type = "text"
72
93
value = { selectedDate . toDateString ( ) }
73
- className = "mt-1 block w-1/2 sm:w-1/3 lg:w-1/4 border border-gray-300 rounded-md shadow-sm p-2"
74
94
readOnly
95
+ className = "mt-1 block w-1/2 sm:w-1/3 lg:w-1/4 border border-gray-300 rounded-md shadow-sm p-2"
75
96
/>
76
97
</ div >
77
98
< div className = "mt-2" >
0 commit comments