|
1 |
| -// components/BookingForm.tsx |
| 1 | +// ./components/BookingForm.tsx |
2 | 2 |
|
3 |
| -'use client'; |
4 |
| - |
5 |
| -import { zodResolver } from '@hookform/resolvers/zod'; |
6 |
| -import { useForm } from 'react-hook-form'; |
| 3 | +import React, { useState, useEffect } from 'react'; |
| 4 | +import { databases } from '../lib/appwrite.config'; |
| 5 | +import { BookingFormProps } from '../types/appwrite.type'; |
7 | 6 | import { useRouter } from 'next/router';
|
8 |
| -import { z } from 'zod'; |
9 |
| -import { getBookingSchema } from '../lib/validation'; |
10 |
| -import CustomFormField, { FormFieldType } from './CustomFormField'; |
11 |
| -import SubmitButton from './SubmitButton'; |
12 |
| -import { createBooking } from '../lib/booking.actions'; |
13 | 7 |
|
14 |
| -const BookingForm = () => { |
| 8 | +const BookingForm: React.FC<BookingFormProps> = ({ providerId, serviceId, selectedDate, onSubmit }) => { |
| 9 | + const [address, setAddress] = useState(''); |
| 10 | + const [city, setCity] = useState(''); |
| 11 | + const [state, setState] = useState(''); |
| 12 | + const [zipcode, setZipcode] = useState(''); |
| 13 | + const [coupon, setCoupon] = useState(''); |
| 14 | + const [discount, setDiscount] = useState(0); |
15 | 15 | const router = useRouter();
|
16 |
| - const form = useForm<z.infer<ReturnType<typeof getBookingSchema>>>({ |
17 |
| - resolver: zodResolver(getBookingSchema('create')), |
18 |
| - defaultValues: { |
19 |
| - service: '', |
20 |
| - date: new Date(), |
21 |
| - time: '', |
22 |
| - }, |
23 |
| - }); |
24 |
| - |
25 |
| - const onSubmit = async (values: z.infer<ReturnType<typeof getBookingSchema>>) => { |
| 16 | + const consumerId = JSON.parse(localStorage.getItem('appwriteSession') || '{}').userId; |
| 17 | + |
| 18 | + useEffect(() => { |
| 19 | + const fetchServiceDetails = async () => { |
| 20 | + try { |
| 21 | + const service = await databases.getDocument( |
| 22 | + process.env.DATABASE_ID!, |
| 23 | + process.env.SERVICE_COLLECTION_ID!, |
| 24 | + serviceId |
| 25 | + ); |
| 26 | + setDiscount(service.price); |
| 27 | + } catch (error) { |
| 28 | + console.error('Error fetching service details:', error); |
| 29 | + } |
| 30 | + }; |
| 31 | + |
| 32 | + fetchServiceDetails(); |
| 33 | + }, [serviceId]); |
| 34 | + |
| 35 | + const handleCouponApply = async () => { |
| 36 | + if (coupon === '100OFF') { |
| 37 | + setDiscount(100); |
| 38 | + } else { |
| 39 | + setDiscount(0); |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + const handleFormSubmit = async (e: React.FormEvent) => { |
| 44 | + e.preventDefault(); |
| 45 | + if (!selectedDate) { |
| 46 | + alert("Please select a date for booking."); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + const formData = { |
| 51 | + date: selectedDate.toISOString(), |
| 52 | + consumerId, |
| 53 | + providerId, |
| 54 | + serviceId, |
| 55 | + status: 'pending', |
| 56 | + address, |
| 57 | + city, |
| 58 | + state, |
| 59 | + zipcode, |
| 60 | + discount |
| 61 | + }; |
| 62 | + |
26 | 63 | try {
|
27 |
| - await createBooking(values); |
28 |
| - router.push('/customerProfile'); // Redirect after booking |
| 64 | + const response = await fetch('/api/bookings/create', { |
| 65 | + method: 'POST', |
| 66 | + headers: { |
| 67 | + 'Content-Type': 'application/json', |
| 68 | + }, |
| 69 | + body: JSON.stringify(formData), |
| 70 | + }); |
| 71 | + |
| 72 | + if (response.ok) { |
| 73 | + const responseData = await response.json(); |
| 74 | + router.push(`/payment-confirmation?bookingId=${responseData.bookingId}`); |
| 75 | + } else { |
| 76 | + console.error('Failed to create booking.'); |
| 77 | + } |
29 | 78 | } catch (error) {
|
30 | 79 | console.error('Error creating booking:', error);
|
31 | 80 | }
|
32 | 81 | };
|
33 | 82 |
|
34 | 83 | return (
|
35 |
| - <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6"> |
36 |
| - <CustomFormField |
37 |
| - fieldType={FormFieldType.INPUT} |
38 |
| - control={form.control} |
39 |
| - name="service" |
40 |
| - label="Service" |
41 |
| - placeholder="Service" |
42 |
| - /> |
43 |
| - <CustomFormField |
44 |
| - fieldType={FormFieldType.DATE_PICKER} |
45 |
| - control={form.control} |
46 |
| - name="date" |
47 |
| - label="Date" |
48 |
| - dateFormat="MM/dd/yyyy" |
49 |
| - /> |
50 |
| - <CustomFormField |
51 |
| - fieldType={FormFieldType.INPUT} |
52 |
| - control={form.control} |
53 |
| - name="time" |
54 |
| - label="Time" |
55 |
| - placeholder="Time" |
56 |
| - /> |
57 |
| - <SubmitButton isLoading={false}>Create Booking</SubmitButton> |
| 84 | + <form onSubmit={handleFormSubmit} className="mt-4"> |
| 85 | + <div> |
| 86 | + <label className="block text-sm font-medium text-gray-700">Date</label> |
| 87 | + <input |
| 88 | + type="text" |
| 89 | + value={selectedDate.toDateString()} |
| 90 | + readOnly |
| 91 | + 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" |
| 92 | + /> |
| 93 | + </div> |
| 94 | + <div className="mt-2"> |
| 95 | + <label className="block text-sm font-medium text-gray-700">Address</label> |
| 96 | + <input |
| 97 | + type="text" |
| 98 | + value={address} |
| 99 | + onChange={(e) => setAddress(e.target.value)} |
| 100 | + 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" |
| 101 | + required |
| 102 | + /> |
| 103 | + </div> |
| 104 | + <div className="mt-2"> |
| 105 | + <label className="block text-sm font-medium text-gray-700">City</label> |
| 106 | + <input |
| 107 | + type="text" |
| 108 | + value={city} |
| 109 | + onChange={(e) => setCity(e.target.value)} |
| 110 | + 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" |
| 111 | + required |
| 112 | + /> |
| 113 | + </div> |
| 114 | + <div className="mt-2"> |
| 115 | + <label className="block text-sm font-medium text-gray-700">State</label> |
| 116 | + <input |
| 117 | + type="text" |
| 118 | + value={state} |
| 119 | + onChange={(e) => setState(e.target.value)} |
| 120 | + 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" |
| 121 | + required |
| 122 | + /> |
| 123 | + </div> |
| 124 | + <div className="mt-2"> |
| 125 | + <label className="block text-sm font-medium text-gray-700">Zipcode</label> |
| 126 | + <input |
| 127 | + type="text" |
| 128 | + value={zipcode} |
| 129 | + onChange={(e) => setZipcode(e.target.value)} |
| 130 | + 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" |
| 131 | + required |
| 132 | + /> |
| 133 | + </div> |
| 134 | + <div className="mt-2"> |
| 135 | + <label className="block text-sm font-medium text-gray-700">Coupon Code</label> |
| 136 | + <input |
| 137 | + type="text" |
| 138 | + value={coupon} |
| 139 | + onChange={(e) => setCoupon(e.target.value)} |
| 140 | + 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" |
| 141 | + /> |
| 142 | + <button |
| 143 | + type="button" |
| 144 | + onClick={handleCouponApply} |
| 145 | + className="ml-2 bg-green-500 text-white py-1 px-2 rounded" |
| 146 | + > |
| 147 | + Apply |
| 148 | + </button> |
| 149 | + </div> |
| 150 | + <button |
| 151 | + type="submit" |
| 152 | + className="mt-4 bg-blue-500 text-white py-2 px-4 rounded" |
| 153 | + > |
| 154 | + Book Now |
| 155 | + </button> |
58 | 156 | </form>
|
59 | 157 | );
|
60 | 158 | };
|
|
0 commit comments