-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHeroForm.tsx
More file actions
303 lines (274 loc) · 11.1 KB
/
Copy pathHeroForm.tsx
File metadata and controls
303 lines (274 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import React, { useState } from 'react';
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
import { Calendar } from '@/components/ui/calendar';
import { MapPin, Calendar as CalendarIcon, Phone } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import UserLocationMap from '@/components/UserLocationMap';
import { cn } from '@/lib/utils';
import { format } from 'date-fns';
import { useToast } from '@/hooks/use-toast';
import { supabase } from '@/integrations/supabase/client';
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from '@/components/ui/dialog';
const HeroForm: React.FC = () => {
const [startDate, setStartDate] = useState<Date>();
const [location, setLocation] = useState<string>('');
const [showSuggestions, setShowSuggestions] = useState(false);
const [phoneNumber, setPhoneNumber] = useState<string>('');
const [serviceType, setServiceType] = useState<string>('');
const [referralCode, setReferralCode] = useState<string>('');
const [hdfu, setHdfu] = useState<string>('');
const [mapCenter, setMapCenter] = useState({ lat: 32.7266, lng: 74.8570 });
const [showSuccessDialog, setShowSuccessDialog] = useState(false);
const { toast } = useToast();
const [dateOpen, setDateOpen] = useState(false);
const [isSubmitting, setIsSubmitting] = useState(false);
const locationList = [
'Katra, Jammu',
'Jaanipur, Jammu',
'High Court, Jammu',
'Satwari Chowk, Jammu',
'Gandhinagar, Jammu',
'Ganghiyal Industrial Area, Jammu',
'Bahu Plaza, Jammu',
'RaghuNath Nagar, Jammu',
'Bus Stand, Jammu',
'Bari Brahmana, Jammu',
'Sainik Colony, Jammu',
];
const locationCoords: any = {
'katra, jammu': { lat: 32.9917, lng: 74.9319 },
'jaanipur, jammu': { lat: 32.7496, lng: 74.8373 },
'high court, jammu': { lat: 32.7294, lng: 74.8648 },
'satwari chowk, jammu': { lat: 32.6887, lng: 74.8371 },
'gandhinagar, jammu': { lat: 32.7266, lng: 74.8570 },
'ganghiyal industrial area, jammu': { lat: 32.6749, lng: 74.8090 },
'bahu plaza, jammu': { lat: 32.7150, lng: 74.8600 },
'raghunath nagar, jammu': { lat: 32.7300, lng: 74.8605 },
'bus stand, jammu': { lat: 32.7357, lng: 74.8733 },
'bari brahmana, jammu': { lat: 32.6204, lng: 74.9072 },
'sainik colony, jammu': { lat: 32.6927, lng: 74.8905 },
};
const handleLocationSelect = (value: string) => {
setLocation(value);
setShowSuggestions(false);
const key = value.toLowerCase();
if (locationCoords[key]) {
setMapCenter(locationCoords[key]);
}
};
const filteredLocations = locationList.filter((loc) =>
loc.toLowerCase().includes(location.toLowerCase())
);
const handleSubmit = async () => {
if (!startDate || !location || !phoneNumber || !serviceType) {
toast({
title: "Error",
description: "Please fill in all fields",
variant: "destructive",
});
return;
}
// Prevent multiple submissions
if (isSubmitting) return;
setIsSubmitting(true);
try {
const { error } = await supabase
.from('User Request')
.insert({
Name: 'User', // Default name as requested
DateOfService: format(startDate, 'yyyy-MM-dd'),
Location: location,
Phone: phoneNumber,
ServiceType: serviceType,
hdfu: hdfu,
ReferalCode: referralCode?.trim() || null,
});
if (error) {
console.error('Error submitting form:', error);
throw error;
}
setShowSuccessDialog(true);
// Reset form
setStartDate(undefined);
setLocation('');
setPhoneNumber('');
setServiceType('');
setHdfu('');
setReferralCode('');
} catch (error: any) {
console.error('Error submitting form:', error);
toast({
title: "Error",
description: error.message,
variant: "destructive",
});
} finally {
setIsSubmitting(false);
}
};
return(
<section id="hero-form" className="hero-pattern from-primary/5 to-background py-16">
<div className="container mx-auto px-4">
<h1 className="text-4xl font-bold mb-8 text-center">Find Professional Services</h1>
<div className="grid grid-cols-1 lg:grid-cols-2 gap-8 items-start">
{/* FORM */}
<div className="bg-white p-8 rounded-2xl shadow-2xl">
<h2 className="text-2xl font-semibold mb-6 text-primary">Book a Service</h2>
<div className="space-y-6">
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<div className="space-y-2">
<label className="text-sm font-medium text-sky-900">Start Date</label>
<Popover open={dateOpen} onOpenChange={setDateOpen}>
<PopoverTrigger asChild>
<Button
variant="outline"
onClick={() => setDateOpen(!dateOpen)}
className={cn(
"w-full justify-start text-left font-normal bg-white",
!startDate && "text-muted-foreground"
)}
>
<CalendarIcon className="mr-2 h-4 w-4" />
{startDate ? format(startDate, "PPP") : <span>Select date</span>}
</Button>
</PopoverTrigger>
<PopoverContent className="w-auto p-0" align="start">
<Calendar
mode="single"
selected={startDate}
onSelect={(date) => {
setStartDate(date);
setDateOpen(false);
}}
initialFocus
className="p-3 pointer-events-auto"
disabled={(date) => date <= new Date(new Date().setHours(0, 0, 0, 0))}
/>
</PopoverContent>
</Popover>
</div>
<div className="space-y-2">
<label className="text-sm font-medium text-sky-900">Referral Code</label>
<Input
type="text"
placeholder="Enter referral code (Optional)"
className="bg-white"
value={referralCode}
onChange={(e) => setReferralCode(e.target.value)}
/>
</div>
</div>
{/* LOCATION AUTOCOMPLETE */}
<div className="space-y-2 relative">
<label className="text-sm font-medium text-sky-900">Location</label>
<Input
placeholder="Enter location"
className="bg-white"
value={location}
onChange={(e) => {
setLocation(e.target.value);
setShowSuggestions(true);
}}
/>
{showSuggestions && location && filteredLocations.length > 0 && (
<div className="absolute left-0 right-0 top-full mt-1 border rounded-md bg-white max-h-60 overflow-auto shadow-lg z-50">
{filteredLocations.map((loc) => (
<div
key={loc}
className="p-2 hover:bg-gray-100 cursor-pointer"
onClick={() => handleLocationSelect(loc)}
>
{loc}
</div>
))}
</div>
)}
</div>
<div className="space-y-2">
<label className="text-sm font-medium text-sky-900">Phone Number</label>
<div className="relative">
<Phone className="absolute left-3 top-1/2 transform -translate-y-1/2 h-4 w-4 text-gray-500" />
<Input
type="tel"
placeholder="Enter your phone number"
className="pl-10 bg-white"
value={phoneNumber}
onChange={(e) => {
const value = e.target.value.replace(/\D/g, '');
if (value.length <= 10) {
setPhoneNumber(value);
}
}}
/>
</div>
{phoneNumber && phoneNumber.length < 10 && (
<p className="text-sm text-red-500">Please enter a valid 10-digit phone number</p>
)}
</div>
<div className="space-y-2">
<label className="text-sm font-medium text-sky-900">Service Type</label>
<Select onValueChange={setServiceType} value={serviceType}>
<SelectTrigger className="bg-white">
<SelectValue placeholder="Select service type" />
</SelectTrigger>
<SelectContent>
<SelectItem value="carpenter">Carpenter - 850rs/day</SelectItem>
<SelectItem value="mason">Mason - 850rs/day</SelectItem>
<SelectItem value="helper">Helper - 650rs/day</SelectItem>
<SelectItem value="painter">Painter - 700rs/day</SelectItem>
<SelectItem value="welder">Welder - 1200rs/day</SelectItem>
<SelectItem value="labour">Labour - 650rs/day</SelectItem>
<SelectItem value="steelcutter">Steel Cutter - 850rs/day</SelectItem>
<SelectItem value="tiles">Tiles and Floor Work - 1000rs/day</SelectItem>
<SelectItem value="plumber">Plumber - 1200rs/day</SelectItem>
<SelectItem value="electrician">Electrician - 1200rs/day</SelectItem>
</SelectContent>
</Select>
</div>
<div className="space-y-2">
<label className="text-sm font-medium text-sky-900">How did you find Gobuild?</label>
<Select onValueChange={setHdfu} value={hdfu}>
<SelectTrigger className="bg-white">
<SelectValue placeholder="Select" />
</SelectTrigger>
<SelectContent>
<SelectItem value="social media">Social Media</SelectItem>
<SelectItem value="ads">Ads and Billboards</SelectItem>
<SelectItem value="newspaper">Newspaper</SelectItem>
<SelectItem value="pamphlets">Pamphlets</SelectItem>
<SelectItem value="friends">Friends</SelectItem>
<SelectItem value="others">Others</SelectItem>
</SelectContent>
</Select>
</div>
<Button
className="w-full animate-pulse-shadow text-white font-semibold text-lg"
onClick={handleSubmit}
disabled={isSubmitting}
>
{isSubmitting ? "Submitting..." : "Book Now"}
</Button>
</div>
</div>
{/* MAP */}
<div className="bg-white rounded-2xl shadow-2xl overflow-hidden h-[625px]">
<UserLocationMap center={mapCenter} />
</div>
</div>
</div>
<Dialog open={showSuccessDialog} onOpenChange={setShowSuccessDialog}>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle>Booking Successful!</DialogTitle>
<DialogDescription>
We will contact you in 15 minutes through phone or sms.
</DialogDescription>
</DialogHeader>
</DialogContent>
</Dialog>
</section>
)
}
export default HeroForm;