Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev 6551 Rev 6829
Línea 8... Línea 8...
8
}) => {
8
}) => {
9
  const [addresObject, setAddressObject] = useState({})
9
  const [addresObject, setAddressObject] = useState({})
10
  const [query, setQuery] = useState('')
10
  const [query, setQuery] = useState('')
11
  const [error, setError] = useState('')
11
  const [error, setError] = useState('')
12
  const autoCompleteRef = useRef(null)
12
  const autoCompleteRef = useRef(null)
-
 
13
  let autoComplete
-
 
14
 
-
 
15
  function handleScriptLoad(
-
 
16
    updateQuery,
-
 
17
    autoCompleteRef,
-
 
18
    setAddresObject,
-
 
19
    setError
-
 
20
  ) {
-
 
21
    autoComplete = new window.google.maps.places.Autocomplete(
-
 
22
      autoCompleteRef.current,
-
 
23
      { types: ['(cities)'] }
-
 
24
    )
-
 
25
    autoComplete.setFields([
-
 
26
      'address_components',
-
 
27
      'formatted_address',
-
 
28
      'geometry',
-
 
29
    ])
-
 
30
    autoComplete.addListener('place_changed', () =>
-
 
31
      handlePlaceSelect(updateQuery, setAddresObject, setError)
-
 
32
    )
-
 
33
  }
-
 
34
 
-
 
35
  async function handlePlaceSelect(updateQuery, setAddresObject, setError) {
-
 
36
    const addressObject = autoComplete.getPlace()
-
 
37
    const query = addressObject.formatted_address
-
 
38
    if (query) {
-
 
39
      setError('')
-
 
40
      updateQuery(query)
-
 
41
      setAddresObject({
-
 
42
        ...addressObject,
-
 
43
        address_components: [
-
 
44
          ...addressObject.address_components,
-
 
45
          {
-
 
46
            latitude: addressObject.geometry.location.lat(),
-
 
47
            longitude: addressObject.geometry.location.lng(),
-
 
48
            types: ['geometry'],
-
 
49
          },
-
 
50
        ],
-
 
51
      })
-
 
52
    } else {
-
 
53
      setError('$error_msg')
-
 
54
    }
-
 
55
  }
Línea 13... Línea 56...
13
 
56
 
14
  useEffect(() => {
57
  useEffect(() => {
15
    handleScriptLoad(setQuery, autoCompleteRef, setAddressObject, setError)
58
    handleScriptLoad(setQuery, autoCompleteRef, setAddressObject, setError)