Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 1852 | Ir a la última revisión | | 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
 
9
const LocationModal = ({ show, onConfirm, onClose }) => {
10
  const labels = useSelector(({ intl }) => intl.labels)
11
 
12
  const {
13
    register,
14
    errors,
15
    handleSubmit,
16
    setValue,
17
    clearErrors,
18
    watch,
19
    reset
20
  } = useForm()
21
 
22
  const toggleModal = () => {
23
    reset()
24
    onClose()
25
  }
26
 
27
  const addressHandler = (address) => {
28
    Object.entries(address).forEach(([key, value]) => {
29
      if (value) {
30
        register(key)
31
        clearErrors(key)
32
        setValue(key, value)
33
      }
34
    })
35
  }
36
 
37
  const onSubmit = handleSubmit((data) => onConfirm(data))
38
 
39
  useEffect(() => {
40
    register('formatted_address', {
41
      required: 'por favor seleccione una ubicación correcta'
42
    })
43
  }, [])
44
 
45
  return (
46
    <Modal
47
      title={labels.location}
48
      show={show}
49
      onClose={toggleModal}
50
      onAccept={onSubmit}
51
    >
52
      <UbicationInput
53
        onGetAddress={addressHandler}
54
        settedQuery={watch('formatted_address')}
55
      />
56
      {errors.formatted_address && (
57
        <FormErrorFeedback>
58
          {errors.formatted_address.message}
59
        </FormErrorFeedback>
60
      )}
61
    </Modal>
62
  )
63
}
64
 
65
export default LocationModal