Skip to content

Commit 0f5d497

Browse files
committed
feat: Redirect customer to confirmation page after booking and add 'Back to Profile' button (instead of generic landing page)
- Updated BookingForm to redirect to confirmation page after booking. - Modified confirmation page to include a Back to Profile button. - the Back to Profile button routes the user back to their profile page.
1 parent d35ebc8 commit 0f5d497

8 files changed

+175
-48
lines changed

components/BookingForm.tsx

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
// ./components/BookingForm.tsx
2+
23
import React, { useState, useEffect } from 'react';
34
import { databases } from '../lib/appwrite.config';
5+
import { BookingFormProps } from '../types/appwrite.type';
46
import { useRouter } from 'next/router';
57

6-
interface BookingFormProps {
7-
providerId: string;
8-
serviceId: string;
9-
selectedDate: Date;
10-
}
11-
12-
const BookingForm: React.FC<BookingFormProps> = ({ providerId, serviceId, selectedDate }) => {
8+
const BookingForm: React.FC<BookingFormProps> = ({ providerId, serviceId, selectedDate, onSubmit }) => {
139
const [address, setAddress] = useState('');
1410
const [city, setCity] = useState('');
1511
const [state, setState] = useState('');

components/ServiceDetails.tsx

+29-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// ./components/ServiceDetails.tsx
2+
23
import React, { useState, useEffect, useCallback } from 'react';
34
import BookingForm from './BookingForm';
45
import AvailabilityCalendar from './AvailabilityCalendar';
56
import { Service, ReviewCardProps } from '../types/appwrite.type';
67
import ReviewCard from './ReviewCard';
78
import ReviewForm from './ReviewForm';
89
import { fetchReviewsForService } from './DataReviewConsumerView';
10+
import { useRouter } from 'next/router';
911

1012
interface ServiceDetailsProps {
1113
service: Service;
@@ -44,6 +46,7 @@ const ServiceDetails: React.FC<ServiceDetailsProps> = ({ service, onBack }) => {
4446
const [isBookingSectionVisible, setIsBookingSectionVisible] = useState(false);
4547
const [isReviewFormVisible, setIsReviewFormVisible] = useState(false);
4648
const [reviews, setReviews] = useState<ReviewCardProps[]>([]);
49+
const router = useRouter();
4750

4851
const toggleBookingSection = () => {
4952
setIsBookingSectionVisible(!isBookingSectionVisible);
@@ -63,22 +66,33 @@ const ServiceDetails: React.FC<ServiceDetailsProps> = ({ service, onBack }) => {
6366
}, [service.$id]);
6467

6568
useEffect(() => {
66-
const fetchReviews = async () => {
67-
try {
68-
const fetchedReviews = await fetchReviewsForService(service.$id);
69-
setReviews(fetchedReviews);
70-
} catch (error) {
71-
console.error('Error fetching reviews:', error);
72-
}
73-
};
74-
7569
fetchReviews();
7670
}, [service.$id, fetchReviews]);
7771

7872
const handleDateChange = (date: Date) => {
7973
setSelectedDate(date);
8074
};
8175

76+
const handleFormSubmit = async (formData: any) => {
77+
try {
78+
const response = await fetch('/api/bookings/create', {
79+
method: 'POST',
80+
headers: {
81+
'Content-Type': 'application/json',
82+
},
83+
body: JSON.stringify(formData),
84+
});
85+
86+
if (response.ok) {
87+
router.push('/customerProfile#bookings');
88+
} else {
89+
console.error('Failed to create booking.');
90+
}
91+
} catch (error) {
92+
console.error('Error creating booking:', error);
93+
}
94+
};
95+
8296
return (
8397
<div>
8498
<h2 className="text-3xl font-bold mb-2">{service.name}</h2>
@@ -115,7 +129,12 @@ const ServiceDetails: React.FC<ServiceDetailsProps> = ({ service, onBack }) => {
115129
<h2 className="text-2xl font-bold mb-4">Booking: {service.name}</h2>
116130
<AvailabilityCalendar availableDates={availableDates} onDateChange={handleDateChange} />
117131
{selectedDate && (
118-
<BookingForm providerId={service.providerId} serviceId={service.$id} selectedDate={selectedDate} />
132+
<BookingForm
133+
providerId={service.providerId}
134+
serviceId={service.$id}
135+
selectedDate={selectedDate}
136+
onSubmit={handleFormSubmit}
137+
/>
119138
)}
120139
</div>
121140
)}

pages/customerBooking.tsx

+27-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,27 @@ const CustomerBooking = () => {
3535
setSelectedDate(date);
3636
};
3737

38+
const handleBookingSubmit = async (formData: any) => {
39+
try {
40+
const response = await fetch('/api/bookings/create', {
41+
method: 'POST',
42+
headers: {
43+
'Content-Type': 'application/json',
44+
},
45+
body: JSON.stringify(formData),
46+
});
47+
48+
if (response.ok) {
49+
const responseData = await response.json();
50+
router.push('/customerProfile?view=bookings');
51+
} else {
52+
console.error('Failed to create booking.');
53+
}
54+
} catch (error) {
55+
console.error('Error creating booking:', error);
56+
}
57+
};
58+
3859
if (!providerId || !serviceId) {
3960
return <div>Loading...</div>; // Or handle the missing IDs appropriately
4061
}
@@ -45,7 +66,12 @@ const CustomerBooking = () => {
4566
<h1 className="text-2xl font-bold mb-6">Book a Service</h1>
4667
<AvailabilityCalendar availableDates={availableDates} onDateChange={handleDateChange} />
4768
{selectedDate && (
48-
<BookingForm providerId={providerId as string} serviceId={serviceId as string} selectedDate={selectedDate} />
69+
<BookingForm
70+
providerId={providerId as string}
71+
serviceId={serviceId as string}
72+
selectedDate={selectedDate}
73+
onSubmit={handleBookingSubmit}
74+
/>
4975
)}
5076
<button
5177
onClick={() => router.push('/customerProfile')}

pages/customerProfile.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,4 @@ const CustomerProfile = () => {
140140
);
141141
};
142142

143-
export default CustomerProfile;
143+
export default CustomerProfile;

pages/index.tsx

+74-15
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,44 @@
11
// pages/index.tsx
22
import Head from 'next/head';
33
import Link from 'next/link';
4-
import Header from '../components/Header';
4+
import { useEffect, useState } from 'react';
5+
import { useRouter } from 'next/router';
6+
import { account } from '../lib/appwrite.config';
57

68
export default function Home() {
9+
const router = useRouter();
10+
const [userType, setUserType] = useState<string | null>(null);
11+
12+
useEffect(() => {
13+
const fetchSession = async () => {
14+
try {
15+
const session = JSON.parse(localStorage.getItem('appwriteSession') || '{}');
16+
const type = localStorage.getItem('userType');
17+
18+
if (session && type) {
19+
setUserType(type);
20+
} else {
21+
setUserType(null);
22+
}
23+
} catch (error) {
24+
console.error('Error fetching session:', error);
25+
}
26+
};
27+
28+
fetchSession();
29+
}, []);
30+
31+
const handleLogout = async () => {
32+
try {
33+
await account.deleteSession('current');
34+
localStorage.removeItem('appwriteSession');
35+
localStorage.removeItem('userType');
36+
router.push('/customer-login');
37+
} catch (error) {
38+
console.error('Error logging out:', error);
39+
}
40+
};
41+
742
return (
843
<div className="min-h-[81vh] flex flex-col justify-center items-center homepage-background">
944
<Head>
@@ -13,20 +48,44 @@ export default function Home() {
1348
</Head>
1449

1550
<main className="relative z-10 flex flex-col md:flex-row justify-center items-center py-8 w-full flex-1">
16-
<div className="flex flex-col items-center space-y-4 p-8 bg-white bg-opacity-80 rounded-md shadow-lg md:w-3/2">
17-
<h1 className="text-4xl font-bold mb-2">ProBooker</h1>
18-
<h2 className="text-2xl mb-6">Connect with the pros, book with confidence</h2>
19-
<Link href="/customer-start" legacyBehavior>
20-
<a className="w-full bg-blue-500 text-white py-3 px-6 rounded-lg text-center hover:bg-blue-600">
21-
Get Started as a Customer
22-
</a>
23-
</Link>
24-
<Link href="/provider-start" legacyBehavior>
25-
<a className="w-full bg-green-500 text-white py-3 px-6 rounded-lg text-center hover:bg-green-600">
26-
Join as a Pro
27-
</a>
28-
</Link>
29-
</div>
51+
{userType ? (
52+
<div className="flex flex-col items-center space-y-4 p-8 bg-white bg-opacity-80 rounded-md shadow-lg md:w-3/2">
53+
<h1 className="text-4xl font-bold mb-2">Welcome, {userType}</h1>
54+
{userType === 'Customer' && (
55+
<>
56+
<p>Customer-specific content here.</p>
57+
{/* Add other customer-specific components or content */}
58+
</>
59+
)}
60+
{userType === 'Provider' && (
61+
<>
62+
<p>Provider-specific content here.</p>
63+
{/* Add other provider-specific components or content */}
64+
</>
65+
)}
66+
<button
67+
onClick={handleLogout}
68+
className="mt-4 bg-red-500 text-white py-2 px-4 rounded"
69+
>
70+
Logout
71+
</button>
72+
</div>
73+
) : (
74+
<div className="flex flex-col items-center space-y-4 p-8 bg-white bg-opacity-80 rounded-md shadow-lg md:w-3/2">
75+
<h1 className="text-4xl font-bold mb-2">ProBooker</h1>
76+
<h2 className="text-2xl mb-6">Connect with the pros, book with confidence</h2>
77+
<Link href="/customer-start" legacyBehavior>
78+
<a className="w-full bg-blue-500 text-white py-3 px-6 rounded-lg text-center hover:bg-blue-600">
79+
Get Started as a Customer
80+
</a>
81+
</Link>
82+
<Link href="/provider-start" legacyBehavior>
83+
<a className="w-full bg-green-500 text-white py-3 px-6 rounded-lg text-center hover:bg-green-600">
84+
Join as a Pro
85+
</a>
86+
</Link>
87+
</div>
88+
)}
3089
</main>
3190
</div>
3291
);

pages/payment-confirmation.tsx

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
11
// pages/payment-confirmation.tsx
2-
// This is the Payment Confirmation Page
3-
// A page to display the payment confirmation details
4-
// This page is only accessible to logged in users.
5-
6-
// pages/payment-confirmation.tsx
7-
// This is the Payment Confirmation Page
8-
// A page to display the payment confirmation details
9-
// This page is only accessible to logged in users.
102

113
import { useRouter } from 'next/router';
124
import React, { useEffect, useState } from 'react';
@@ -49,15 +41,14 @@ const PaymentConfirmation = () => {
4941
{/* Remove the amount display */}
5042
{/* <p>Amount: ${bookingDetails.servicePrice - bookingDetails.discount}</p> */}
5143
<button
52-
onClick={() => router.push('/')}
44+
onClick={() => router.push('/customerProfile#bookings')}
5345
className="mt-4 bg-green-500 text-white py-2 px-4 rounded"
5446
>
55-
Back to Home
47+
Back to Profile
5648
</button>
5749
</main>
5850
</div>
5951
);
6052
};
6153

6254
export default PaymentConfirmation;
63-

pages/providerBooking.tsx

+38-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ./pages/providerBooking.tsx
2+
13
import React, { useEffect, useState } from 'react';
24
import { useRouter } from 'next/router';
35
import AvailabilityCalendar from '../components/AvailabilityCalendar';
@@ -8,6 +10,7 @@ const ProviderBooking = () => {
810
const router = useRouter();
911
const { providerId, serviceId } = router.query;
1012
const [availableDates, setAvailableDates] = useState<Date[]>([]);
13+
const [selectedDate, setSelectedDate] = useState<Date | null>(null);
1114

1215
useEffect(() => {
1316
const fetchAvailability = async () => {
@@ -20,6 +23,30 @@ const ProviderBooking = () => {
2023
fetchAvailability();
2124
}, [providerId]);
2225

26+
const handleDateChange = (date: Date) => {
27+
setSelectedDate(date);
28+
};
29+
30+
const handleFormSubmit = async (formData: any) => {
31+
try {
32+
const response = await fetch('/api/bookings/create', {
33+
method: 'POST',
34+
headers: {
35+
'Content-Type': 'application/json',
36+
},
37+
body: JSON.stringify(formData),
38+
});
39+
40+
if (response.ok) {
41+
router.push('/providerProfile/viewBookings');
42+
} else {
43+
console.error('Failed to create booking.');
44+
}
45+
} catch (error) {
46+
console.error('Error creating booking:', error);
47+
}
48+
};
49+
2350
if (!providerId || !serviceId) {
2451
return <div>Loading...</div>;
2552
}
@@ -28,10 +55,17 @@ const ProviderBooking = () => {
2855
<div className="flex min-h-screen provider-background">
2956
<main className="w-full p-8 content-background">
3057
<h1 className="text-2xl font-bold mb-6">Manage Bookings</h1>
31-
<AvailabilityCalendar availableDates={availableDates} />
32-
<BookingForm providerId={providerId as string} serviceId={serviceId as string} selectedDate={new Date()} />
58+
<AvailabilityCalendar availableDates={availableDates} onDateChange={handleDateChange} />
59+
{selectedDate && (
60+
<BookingForm
61+
providerId={providerId as string}
62+
serviceId={serviceId as string}
63+
selectedDate={selectedDate}
64+
onSubmit={handleFormSubmit}
65+
/>
66+
)}
3367
<button
34-
onClick={() => router.push('/serviceProfile')}
68+
onClick={() => router.push('/providerProfile')}
3569
className="mt-4 bg-blue-500 text-white py-2 px-4 rounded"
3670
>
3771
Back to Profile
@@ -42,3 +76,4 @@ const ProviderBooking = () => {
4276
};
4377

4478
export default ProviderBooking;
79+

types/appwrite.type.ts

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export interface BookingFormProps {
8989
providerId: string;
9090
serviceId: string;
9191
selectedDate: Date;
92+
onSubmit: (formData: any) => Promise<void>;
9293
}
9394

9495
export interface Availability extends Models.Document {

0 commit comments

Comments
 (0)