File size: 9,951 Bytes
b89a86e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useState } from "react";
import { useLocation } from "@/hooks/use-location";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Separator } from "@/components/ui/separator";
import { MapPin, ExternalLink, Copy, Share, Loader2, AlertCircle } from "lucide-react";
import { useToast } from "@/hooks/use-toast";

interface LocationDetectorProps {
  onLocationDetected: (locationData: {
    latitude: number;
    longitude: number;
    googleMapsUrl: string;
    locationDetectedAutomatically: boolean;
  }) => void;
  onManualLocationSubmit: (manualData: {
    street: string;
    city: string;
    state: string;
    pinCode: string;
    country: string;
    locationDetectedAutomatically: false;
  }) => void;
}

export const LocationDetector: React.FC<LocationDetectorProps> = ({
  onLocationDetected,
  onManualLocationSubmit
}) => {
  const { location, error, isLoading, getLocation, copyToClipboard } = useLocation();
  const { toast } = useToast();
  const [showManualEntry, setShowManualEntry] = useState(false);
  const [manualLocation, setManualLocation] = useState({
    street: "",
    city: "",
    state: "",
    pinCode: "",
    country: ""
  });

  const handleGetLocation = () => {
    getLocation();
  };

  const handleUseDetectedLocation = () => {
    if (location) {
      onLocationDetected({
        latitude: location.latitude,
        longitude: location.longitude,
        googleMapsUrl: location.googleMapsUrl,
        locationDetectedAutomatically: true
      });
      toast({
        title: "Location detected!",
        description: "Your location has been automatically detected and saved."
      });
    }
  };

  const handleCopyUrl = async () => {
    if (location) {
      try {
        await copyToClipboard(location.googleMapsUrl);
        toast({
          title: "Link copied!",
          description: "Google Maps link has been copied to your clipboard."
        });
      } catch (err) {
        toast({
          variant: "destructive",
          title: "Failed to copy",
          description: "Could not copy the link to clipboard."
        });
      }
    }
  };

  const handleManualSubmit = () => {
    if (!manualLocation.street || !manualLocation.city || !manualLocation.state || 
        !manualLocation.pinCode || !manualLocation.country) {
      toast({
        variant: "destructive",
        title: "Missing information",
        description: "Please fill in all required fields."
      });
      return;
    }

    onManualLocationSubmit({
      ...manualLocation,
      locationDetectedAutomatically: false
    });
    toast({
      title: "Manual location saved!",
      description: "Your location information has been saved."
    });
  };

  return (
    <Card className="w-full max-w-md mx-auto glass-card border-0">
      <CardHeader>
        <CardTitle className="flex items-center gap-2">
          <MapPin className="h-5 w-5 text-primary" />
          Location Detection
        </CardTitle>
      </CardHeader>
      <CardContent className="space-y-4">
        {/* Automatic Location Detection */}
        <div className="space-y-3">
          <Button 
            onClick={handleGetLocation}
            disabled={isLoading}
            className="w-full"
            data-testid="button-get-location"
          >
            {isLoading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
            <MapPin className="mr-2 h-4 w-4" />
            {isLoading ? "Getting Location..." : "Get My Location Automatically"}
          </Button>

          {location && (
            <div className="p-4 bg-green-50 dark:bg-green-900/20 rounded-lg space-y-3" data-testid="location-detected">
              <div className="text-sm">
                <p className="font-semibold text-green-700 dark:text-green-300">
                  Location detected successfully!
                </p>
                <p className="text-green-600 dark:text-green-400" data-testid="coordinates">
                  Latitude: {location.latitude.toFixed(6)}, Longitude: {location.longitude.toFixed(6)}
                </p>
                {location.accuracy && (
                  <p className="text-xs text-green-600 dark:text-green-400">
                    Accuracy: ±{Math.round(location.accuracy)} meters
                  </p>
                )}
              </div>

              <div className="flex gap-2">
                <Button
                  size="sm"
                  onClick={() => window.open(location.googleMapsUrl, '_blank')}
                  data-testid="button-open-maps"
                >
                  <ExternalLink className="mr-1 h-3 w-3" />
                  Open in Maps
                </Button>
                <Button
                  size="sm"
                  variant="outline"
                  onClick={handleCopyUrl}
                  data-testid="button-copy-link"
                >
                  <Copy className="mr-1 h-3 w-3" />
                  Copy Link
                </Button>
              </div>

              <Button
                onClick={handleUseDetectedLocation}
                className="w-full"
                data-testid="button-use-location"
              >
                Use This Location
              </Button>
            </div>
          )}

          {error && (
            <div className="p-4 bg-red-50 dark:bg-red-900/20 rounded-lg" data-testid="location-error">
              <div className="flex items-start gap-2">
                <AlertCircle className="h-4 w-4 text-red-500 mt-0.5" />
                <div className="text-sm">
                  <p className="font-semibold text-red-700 dark:text-red-300">
                    Location Detection Failed
                  </p>
                  <p className="text-red-600 dark:text-red-400">{error.message}</p>
                </div>
              </div>
            </div>
          )}
        </div>

        <div className="flex items-center gap-2">
          <Separator className="flex-1" />
          <span className="text-xs text-muted-foreground">OR</span>
          <Separator className="flex-1" />
        </div>

        {/* Manual Entry Option */}
        <div className="space-y-3">
          {!showManualEntry ? (
            <Button
              variant="outline"
              onClick={() => setShowManualEntry(true)}
              className="w-full"
              data-testid="button-manual-entry"
            >
              Enter Location Manually
            </Button>
          ) : (
            <div className="space-y-3" data-testid="manual-entry-form">
              <h4 className="text-sm font-semibold">Manual Location Entry</h4>
              
              <div className="grid grid-cols-1 gap-3">
                <div>
                  <Label htmlFor="street">Street Address</Label>
                  <Input
                    id="street"
                    value={manualLocation.street}
                    onChange={(e) => setManualLocation(prev => ({...prev, street: e.target.value}))}
                    placeholder="Enter street address"
                    data-testid="input-street"
                  />
                </div>
                
                <div className="grid grid-cols-2 gap-2">
                  <div>
                    <Label htmlFor="city">City</Label>
                    <Input
                      id="city"
                      value={manualLocation.city}
                      onChange={(e) => setManualLocation(prev => ({...prev, city: e.target.value}))}
                      placeholder="City"
                      data-testid="input-city"
                    />
                  </div>
                  <div>
                    <Label htmlFor="state">State</Label>
                    <Input
                      id="state"
                      value={manualLocation.state}
                      onChange={(e) => setManualLocation(prev => ({...prev, state: e.target.value}))}
                      placeholder="State"
                      data-testid="input-state"
                    />
                  </div>
                </div>
                
                <div className="grid grid-cols-2 gap-2">
                  <div>
                    <Label htmlFor="pinCode">PIN Code</Label>
                    <Input
                      id="pinCode"
                      value={manualLocation.pinCode}
                      onChange={(e) => setManualLocation(prev => ({...prev, pinCode: e.target.value}))}
                      placeholder="PIN Code"
                      data-testid="input-pincode"
                    />
                  </div>
                  <div>
                    <Label htmlFor="country">Country</Label>
                    <Input
                      id="country"
                      value={manualLocation.country}
                      onChange={(e) => setManualLocation(prev => ({...prev, country: e.target.value}))}
                      placeholder="Country"
                      data-testid="input-country"
                    />
                  </div>
                </div>
              </div>
              
              <div className="flex gap-2">
                <Button
                  onClick={handleManualSubmit}
                  className="flex-1"
                  data-testid="button-submit-manual"
                >
                  Use Manual Location
                </Button>
                <Button
                  variant="outline"
                  onClick={() => setShowManualEntry(false)}
                  data-testid="button-cancel-manual"
                >
                  Cancel
                </Button>
              </div>
            </div>
          )}
        </div>
      </CardContent>
    </Card>
  );
};