Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 7826 | Rev 7858 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 7826 Rev 7853
Línea 1... Línea 1...
1
import React, { useEffect, useRef } from "react";
1
import React, { useEffect, useRef } from "react";
-
 
2
import { useState } from "react";
Línea 2... Línea 3...
2
 
3
 
Línea 3... Línea 4...
3
let autoComplete;
4
let autoComplete;
4
 
-
 
5
const loadScript = (url, callback) => {
-
 
6
    let script = document.createElement("script");
5
 
7
    script.type = "text/javascript";
-
 
8
 
6
function SearchLocationInput({ googleApiKey, setValue, value }) {
9
    if (script.readyState) {
-
 
10
        script.onreadystatechange = function () {
7
    
11
            if (script.readyState === "loaded" || script.readyState === "complete") {
-
 
12
                script.onreadystatechange = null;
-
 
13
                callback();
-
 
14
            }
-
 
15
        };
-
 
16
    } else {
-
 
Línea -... Línea 8...
-
 
8
    const autoCompleteRef = useRef(null);
-
 
9
    const [locationObject, setLocationObject] = useState()
-
 
10
 
-
 
11
    const loadScript = (url, callback) => {
-
 
12
        let script = document.createElement("script");
-
 
13
        script.type = "text/javascript";
-
 
14
    
-
 
15
        if (script.readyState) {
-
 
16
            script.onreadystatechange = function () {
-
 
17
                if (script.readyState === "loaded" || script.readyState === "complete") {
-
 
18
                    script.onreadystatechange = null;
-
 
19
                    callback();
-
 
20
                }
-
 
21
            };
-
 
22
        } else {
17
        script.onload = () => callback();
23
            script.onload = () => callback();
18
    }
24
        }
19
 
25
    
20
    script.src = url;
26
        script.src = url;
21
    document.getElementsByTagName("head")[0].appendChild(script);
27
        document.getElementsByTagName("head")[0].appendChild(script);
22
};
28
    };
23
 
29
    
24
function handleScriptLoad(updateQuery, autoCompleteRef) {
30
    function handleScriptLoad(updateQuery, autoCompleteRef) {
25
    autoComplete = new window.google.maps.places.Autocomplete(
31
        autoComplete = new window.google.maps.places.Autocomplete(
26
        autoCompleteRef.current,
32
            autoCompleteRef.current,
27
        { types: ["(cities)"] }
33
            { types: ["(cities)"] }
28
    );
34
        );
29
    autoComplete.setFields(["address_components", "formatted_address"]);
35
        autoComplete.setFields(["address_components", "formatted_address"]);
30
    autoComplete.addListener("place_changed", () =>
-
 
31
        handlePlaceSelect(updateQuery)
-
 
32
    );
-
 
33
}
-
 
34
 
-
 
35
async function handlePlaceSelect(updateQuery) {
-
 
36
    const addressObject = autoComplete.getPlace();
-
 
37
    const query = addressObject.formatted_address;
36
        autoComplete.addListener("place_changed", () =>
Línea 38... Línea 37...
38
    updateQuery(query);
37
            handlePlaceSelect(updateQuery)
-
 
38
        );
-
 
39
    }
-
 
40
 
-
 
41
    async function handlePlaceSelect(updateQuery) {
-
 
42
        let locationObject = {
-
 
43
            formatted_address: '',
-
 
44
            address1: '',
-
 
45
            address2: '',
-
 
46
            country: '',
-
 
47
            state: '',
-
 
48
            city1: '',
-
 
49
            city2: '',
-
 
50
            postal_code: '',
-
 
51
            latitude: 0,
-
 
52
            longitude: 0,
-
 
53
            location_search: ''
-
 
54
        }
-
 
55
        const place = autoComplete.getPlace();
-
 
56
 
-
 
57
        locationObject.formatted_address = place.formatted_address;
-
 
58
        locationObject.latitude = place.geometry.location.lat();
-
 
59
        locationObject.longitude = place.geometry.location.lng();
-
 
60
        const arrAddress = place.address_components;
-
 
61
 
-
 
62
        arrAddress.forEach((address_component) => {
-
 
63
            if (address_component.types[0] == "route") {
-
 
64
                locationObject.address1 = address_component.long_name;
-
 
65
            }
-
 
66
            if (address_component.types[0] == "sublocality") {
-
 
67
                locationObject.address2 = address_component.long_name;
-
 
68
            }
-
 
69
            if (address_component.types[0] == "locality") {
-
 
70
                locationObject.city1 = address_component.long_name;
-
 
71
            }
-
 
72
            if (address_component.types[0] == "administrative_area_level_2") {
-
 
73
                locationObject.city2 = address_component.long_name;
-
 
74
            }
-
 
75
            if (address_component.types[0] == "administrative_area_level_1") {
-
 
76
                locationObject.state = address_component.long_name;
-
 
77
            }
-
 
78
            if (address_component.types[0] == "country") {
-
 
79
                locationObject.country = address_component.long_name;
-
 
80
            }
Línea 39... Línea 81...
39
    console.log(addressObject);
81
            if (address_component.types[0] == "postal_code") {
-
 
82
                locationObject.postal_code = address_component.long_name;
-
 
83
            }
Línea 40... Línea 84...
40
}
84
        })
41
 
85
 
42
function SearchLocationInput({ googleApiKey, setValue, value }) {
86
        setLocationObject(locationObject)
43
 
87
        updateQuery(locationObject.formatted_address);