|
1 | 1 | // pages/index.tsx |
2 | 2 |
|
3 | | -import { useEffect, useRef, useState } from 'react'; |
| 3 | +import { useEffect, useRef, useState } from "react"; |
| 4 | +import { Input } from "../ui/input"; |
| 5 | +import { Label } from "../ui/label"; |
4 | 6 |
|
5 | 7 | interface CoordinateProps { |
6 | | - |
7 | | - setLatitude: React.Dispatch<React.SetStateAction<string>>; |
8 | | - setLongitude: React.Dispatch<React.SetStateAction<string>>; |
9 | | - } |
10 | | - |
| 8 | + setLatitude: React.Dispatch<React.SetStateAction<string>>; |
| 9 | + setLongitude: React.Dispatch<React.SetStateAction<string>>; |
| 10 | +} |
11 | 11 |
|
12 | | -const SetZone : React.FC<CoordinateProps> = ({ setLatitude, setLongitude }) => { |
13 | | - const locationInputRef = useRef<HTMLInputElement>(null); |
14 | | - const latInputRef = useRef<HTMLInputElement>(null); |
15 | | - const longInputRef = useRef<HTMLInputElement>(null); |
16 | | - const placeNameRef = useRef<HTMLInputElement>(null); |
| 12 | +const SetZone: React.FC<CoordinateProps> = ({ setLatitude, setLongitude }) => { |
| 13 | + const locationInputRef = useRef<HTMLInputElement>(null); |
| 14 | + const latInputRef = useRef<HTMLInputElement>(null); |
| 15 | + const longInputRef = useRef<HTMLInputElement>(null); |
| 16 | + const placeNameRef = useRef<HTMLInputElement>(null); |
17 | 17 |
|
18 | | - useEffect(() => { |
19 | | - const loadScript = (url: string, callback: () => void) => { |
20 | | - const script = document.createElement('script'); |
21 | | - script.type = 'text/javascript'; |
22 | | - script.src = url; |
23 | | - script.onload = callback; |
24 | | - document.head.appendChild(script); |
25 | | - }; |
26 | | - const apiKey: string = process.env.NEXT_PUBLIC_GOOGLE_MAPS_API_KEY || ''; |
| 18 | + useEffect(() => { |
| 19 | + const loadScript = (url: string, callback: () => void) => { |
| 20 | + const script = document.createElement("script"); |
| 21 | + script.type = "text/javascript"; |
| 22 | + script.src = url; |
| 23 | + script.onload = callback; |
| 24 | + document.head.appendChild(script); |
| 25 | + }; |
| 26 | + const apiKey: string = process.env.NEXT_PUBLIC_GOOGLE_MAPS_API_KEY || ""; |
27 | 27 |
|
28 | | - loadScript('https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js', () => { |
29 | | - loadScript( |
30 | | - `https://maps.googleapis.com/maps/api/js?key=${apiKey}&libraries=places`, |
31 | | - initializeAutocomplete |
32 | | - ); |
33 | | - }); |
34 | | - }, []); |
| 28 | + loadScript( |
| 29 | + "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js", |
| 30 | + () => { |
| 31 | + loadScript( |
| 32 | + `https://maps.googleapis.com/maps/api/js?key=${apiKey}&libraries=places`, |
| 33 | + initializeAutocomplete |
| 34 | + ); |
| 35 | + } |
| 36 | + ); |
| 37 | + }, []); |
35 | 38 |
|
36 | | - const initializeAutocomplete = () => { |
37 | | - const autocomplete = new google.maps.places.Autocomplete(locationInputRef.current!, { |
38 | | - types: ['geocode'] |
39 | | - }); |
| 39 | + const initializeAutocomplete = () => { |
| 40 | + const autocomplete = new google.maps.places.Autocomplete( |
| 41 | + locationInputRef.current!, |
| 42 | + { |
| 43 | + types: ["geocode"], |
| 44 | + } |
| 45 | + ); |
40 | 46 |
|
41 | | - google.maps.event.addListener(autocomplete, 'place_changed', () => { |
42 | | - const place = autocomplete.getPlace(); |
43 | | - if (!place.geometry) { |
44 | | - return; |
45 | | - } |
46 | | - const lat = place.geometry.location.lat(); |
47 | | - const lng = place.geometry.location.lng(); |
48 | | - latInputRef.current!.value = lat.toString(); |
49 | | - longInputRef.current!.value = lng.toString(); |
50 | | - // placeNameRef.current!.value = place.name.toString(); |
51 | | - setLatitude(latInputRef.current!.value); |
52 | | - setLongitude(longInputRef.current!.value); |
| 47 | + google.maps.event.addListener(autocomplete, "place_changed", () => { |
| 48 | + const place = autocomplete.getPlace(); |
| 49 | + if (!place.geometry) { |
| 50 | + return; |
| 51 | + } |
| 52 | + const lat = place.geometry.location?.lat(); |
| 53 | + const lng = place.geometry.location?.lng(); |
| 54 | + latInputRef.current!.value = lat?.toString() || ""; |
| 55 | + longInputRef.current!.value = lng?.toString() || ""; |
| 56 | + // placeNameRef.current!.value = place.name.toString(); |
| 57 | + setLatitude(latInputRef.current!.value); |
| 58 | + setLongitude(longInputRef.current!.value); |
| 59 | + }); |
| 60 | + }; |
| 61 | + const handleLatitudeChange = (event: React.ChangeEvent<HTMLInputElement>) => { |
| 62 | + setLatitude(event.target.value); |
| 63 | + }; |
53 | 64 |
|
54 | | - }); |
55 | | - }; |
56 | | - const handleLatitudeChange = (event: React.ChangeEvent<HTMLInputElement>) => { |
57 | | - setLatitude(event.target.value); |
58 | | - }; |
59 | | - |
60 | | - const handleLongitudeChange = (event: React.ChangeEvent<HTMLInputElement>) => { |
61 | | - setLongitude(event.target.value); |
62 | | - }; |
| 65 | + const handleLongitudeChange = ( |
| 66 | + event: React.ChangeEvent<HTMLInputElement> |
| 67 | + ) => { |
| 68 | + setLongitude(event.target.value); |
| 69 | + }; |
63 | 70 |
|
64 | | - return ( |
65 | | - <div> |
66 | | - <input type="text" id="location" ref={locationInputRef} /> |
67 | | - <input type="text" placeholder='Latitude' id="lat" ref={latInputRef} onChange={handleLatitudeChange} /> |
68 | | - <input type="text" id="long" placeholder='Longitude' ref={longInputRef} onChange={handleLongitudeChange} /> |
69 | | - {/* <input type="text" id="placeName" ref={placeNameRef} readOnly /> */} |
70 | | - </div> |
71 | | - ); |
| 71 | + return ( |
| 72 | + <> |
| 73 | + <Label htmlFor="location" className="text-right"> |
| 74 | + STS Location |
| 75 | + </Label> |
| 76 | + <Input |
| 77 | + type="text" |
| 78 | + id="location" |
| 79 | + ref={locationInputRef} |
| 80 | + className="col-span-3" |
| 81 | + /> |
| 82 | + <Label htmlFor="location" className="text-right"> |
| 83 | + STS Location |
| 84 | + </Label> |
| 85 | + <div className="flex gap-2 col-span-3"> |
| 86 | + <Input |
| 87 | + type="text" |
| 88 | + placeholder="Latitude" |
| 89 | + id="lat" |
| 90 | + ref={latInputRef} |
| 91 | + onChange={handleLatitudeChange} |
| 92 | + className="" |
| 93 | + /> |
| 94 | + <Input |
| 95 | + type="text" |
| 96 | + id="long" |
| 97 | + placeholder="Longitude" |
| 98 | + ref={longInputRef} |
| 99 | + onChange={handleLongitudeChange} |
| 100 | + /> |
| 101 | + </div> |
| 102 | + {/* <input type="text" id="placeName" ref={placeNameRef} readOnly /> */} |
| 103 | + </> |
| 104 | + ); |
72 | 105 | }; |
73 | 106 |
|
74 | 107 | export default SetZone; |
0 commit comments