Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 1849 | Rev 2802 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1849 stevensc 1
import React, { useEffect } from 'react'
2
import { useSelector } from 'react-redux'
3
import { useForm } from 'react-hook-form'
4
 
5
import Modal from 'components/UI/modal/Modal'
6
import UbicationInput from 'components/UI/inputs/UbicationInput'
7
import FormErrorFeedback from 'components/UI/form/FormErrorFeedback'
8
 
1852 stevensc 9
const LocationModal = ({
10
  show = false,
11
  onConfirm = () => {},
12
  onClose = () => {}
13
}) => {
1849 stevensc 14
  const labels = useSelector(({ intl }) => intl.labels)
15
 
16
  const {
17
    register,
18
    errors,
19
    handleSubmit,
20
    setValue,
21
    clearErrors,
22
    watch,
23
    reset
24
  } = useForm()
25
 
26
  const toggleModal = () => {
27
    reset()
28
    onClose()
29
  }
30
 
31
  const addressHandler = (address) => {
32
    Object.entries(address).forEach(([key, value]) => {
33
      if (value) {
34
        register(key)
35
        clearErrors(key)
36
        setValue(key, value)
37
      }
38
    })
39
  }
40
 
41
  const onSubmit = handleSubmit((data) => onConfirm(data))
42
 
43
  useEffect(() => {
44
    register('formatted_address', {
45
      required: 'por favor seleccione una ubicación correcta'
46
    })
47
  }, [])
48
 
49
  return (
50
    <Modal
51
      title={labels.location}
52
      show={show}
53
      onClose={toggleModal}
54
      onAccept={onSubmit}
55
    >
56
      <UbicationInput
57
        onGetAddress={addressHandler}
58
        settedQuery={watch('formatted_address')}
59
      />
60
      {errors.formatted_address && (
61
        <FormErrorFeedback>
62
          {errors.formatted_address.message}
63
        </FormErrorFeedback>
64
      )}
65
    </Modal>
66
  )
67
}
68
 
69
export default LocationModal