Proyectos de Subversion LeadersLinked - Backend

Rev

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

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