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
|
"use client";
import { useState } from "react";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import * as z from "zod";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
// Form validation schema
const shareFormSchema = z.object({
email: z.string().email("Please enter a valid email address"),
senderName: z.string().min(1, "Please enter your name"),
expiryHours: z.coerce.number().min(0).optional(),
});
type ShareFormValues = z.infer<typeof shareFormSchema>;
interface ShareLocationFormProps {
location: {
latitude: number;
longitude: number;
accuracy?: number;
} | null;
}
export default function ShareLocationForm({ location }: ShareLocationFormProps) {
const [isSubmitting, setIsSubmitting] = useState(false);
const form = useForm<ShareFormValues>({
resolver: zodResolver(shareFormSchema),
defaultValues: {
email: "",
senderName: "",
expiryHours: 24,
},
});
const onSubmit = async (values: ShareFormValues) => {
if (!location) {
toast.error("No location data available to share");
return;
}
setIsSubmitting(true);
try {
const response = await fetch("/api/share-location", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
...values,
latitude: location.latitude,
longitude: location.longitude,
accuracy: location.accuracy,
}),
});
const data = await response.json();
if (!response.ok) {
if (data.emailError) {
toast.error(`Email error: ${data.message || 'Failed to send notification email'}`);
toast.info("Location was generated but notification couldn't be sent");
return;
}
throw new Error(data.message || data.error || "Failed to share location");
}
if (data.data?.devMode) {
toast.success("Dev mode: Email would be sent (check server logs)");
form.reset();
return;
}
toast.success(`Location shared with ${values.email}`);
form.reset();
} catch (error) {
console.error("Error sharing location:", error);
toast.error("Failed to share location. Please try again.");
} finally {
setIsSubmitting(false);
}
};
return (
<Card>
<CardHeader>
<CardTitle className="text-xl">Share Your Location</CardTitle>
</CardHeader>
<CardContent>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
<FormField
control={form.control}
name="email"
render={({ field }) => (
<FormItem>
<FormLabel>Email Address</FormLabel>
<FormControl>
<Input placeholder="[email protected]" {...field} />
</FormControl>
<FormDescription>
The email address to share your location with.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="senderName"
render={({ field }) => (
<FormItem>
<FormLabel>Your Name</FormLabel>
<FormControl>
<Input placeholder="Your Name" {...field} />
</FormControl>
<FormDescription>
Your name will be included in the email.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="expiryHours"
render={({ field }) => (
<FormItem>
<FormLabel>Expiry Time (hours)</FormLabel>
<FormControl>
<Input
type="number"
min="0"
placeholder="24"
{...field}
/>
</FormControl>
<FormDescription>
How long the location share will be valid (0 for no expiry)
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<Button
type="submit"
className="w-full"
disabled={isSubmitting || !location}
>
{isSubmitting ? "Sharing..." : "Share Location"}
</Button>
</form>
</Form>
</CardContent>
</Card>
);
}
|