| 6753 |
stevensc |
1 |
import React, { useEffect, useState, useRef } from 'react'
|
|
|
2 |
import { axios } from '../../../utils'
|
|
|
3 |
import { useForm } from 'react-hook-form'
|
|
|
4 |
import { Button, Modal } from 'react-bootstrap'
|
|
|
5 |
import { useDispatch, useSelector } from 'react-redux'
|
|
|
6 |
import { addNotification } from '../../../redux/notification/notification.actions'
|
|
|
7 |
|
|
|
8 |
import Spinner from '../UI/Spinner'
|
|
|
9 |
import UbicationInput from '../../../shared/ubication-input/UbicationInput'
|
|
|
10 |
import FormErrorFeedback from '../UI/FormErrorFeedback'
|
|
|
11 |
|
|
|
12 |
const LocationModal = ({
|
|
|
13 |
closeModal,
|
|
|
14 |
isModalOpen,
|
|
|
15 |
userIdEncrypted,
|
|
|
16 |
setSettedAddress,
|
|
|
17 |
}) => {
|
|
|
18 |
const [modalLoading, setModalLoading] = useState(false)
|
|
|
19 |
const labels = useSelector(({ intl }) => intl.labels)
|
|
|
20 |
const dispatch = useDispatch()
|
|
|
21 |
|
|
|
22 |
const {
|
|
|
23 |
register,
|
|
|
24 |
errors,
|
|
|
25 |
handleSubmit,
|
|
|
26 |
setValue,
|
|
|
27 |
clearErrors,
|
|
|
28 |
getValues,
|
|
|
29 |
watch,
|
|
|
30 |
setError,
|
|
|
31 |
} = useForm()
|
|
|
32 |
|
|
|
33 |
const isAddressEmpty = useRef(true)
|
|
|
34 |
const addressKeys = useRef([
|
|
|
35 |
'address1',
|
|
|
36 |
'address2',
|
|
|
37 |
'country',
|
|
|
38 |
'state',
|
|
|
39 |
'city1',
|
|
|
40 |
'city2',
|
|
|
41 |
'postal_code',
|
|
|
42 |
'latitude',
|
|
|
43 |
'longitude',
|
|
|
44 |
])
|
|
|
45 |
|
|
|
46 |
const handleModalOpen = (event) => {
|
|
|
47 |
event && event.preventDefault()
|
|
|
48 |
Object.keys(getValues()).map(([key]) => setValue(key, ''))
|
|
|
49 |
closeModal()
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
const getAddressHandler = (addresObject) => {
|
|
|
53 |
addressKeys.current.map((addressKey) => {
|
|
|
54 |
setValue(addressKey, '')
|
|
|
55 |
})
|
|
|
56 |
const { address_components } = addresObject
|
|
|
57 |
if (address_components) {
|
|
|
58 |
address_components.map((address_component) => {
|
|
|
59 |
const address_component_name = address_component.long_name
|
|
|
60 |
const address_component_type = address_component.types[0]
|
|
|
61 |
switch (address_component_type) {
|
|
|
62 |
case 'route':
|
|
|
63 |
setValue('address1', address_component_name)
|
|
|
64 |
break
|
|
|
65 |
case 'sublocality':
|
|
|
66 |
setValue('address2', address_component_name)
|
|
|
67 |
break
|
|
|
68 |
case 'locality':
|
|
|
69 |
setValue('city1', address_component_name)
|
|
|
70 |
break
|
|
|
71 |
case 'administrative_area_level_2':
|
|
|
72 |
setValue('city2', address_component_name)
|
|
|
73 |
break
|
|
|
74 |
case 'administrative_area_level_1':
|
|
|
75 |
setValue('state', address_component_name)
|
|
|
76 |
break
|
|
|
77 |
case 'country':
|
|
|
78 |
setValue('country', address_component_name)
|
|
|
79 |
break
|
|
|
80 |
case 'postal_code':
|
|
|
81 |
setValue('postal_code', address_component_name)
|
|
|
82 |
break
|
|
|
83 |
case 'geometry':
|
|
|
84 |
setValue('latitude', address_component.latitude)
|
|
|
85 |
setValue('longitude', address_component.longitude)
|
|
|
86 |
break
|
|
|
87 |
default:
|
|
|
88 |
break
|
|
|
89 |
}
|
|
|
90 |
})
|
|
|
91 |
isAddressEmpty.current = false
|
|
|
92 |
clearErrors('formatted_address')
|
|
|
93 |
} else {
|
|
|
94 |
isAddressEmpty.current = true
|
|
|
95 |
}
|
|
|
96 |
setValue('formatted_address', addresObject.formatted_address)
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
const onSubmitHandler = async (data) => {
|
|
|
100 |
setModalLoading(true)
|
|
|
101 |
const formData = new FormData()
|
|
|
102 |
Object.entries(data).map(([key, value]) => {
|
|
|
103 |
formData.append(key, value)
|
|
|
104 |
})
|
|
|
105 |
await axios
|
|
|
106 |
.post(`/profile/my-profiles/location/${userIdEncrypted}`, formData)
|
|
|
107 |
.then((response) => {
|
|
|
108 |
const resData = response.data
|
|
|
109 |
if (resData.success) {
|
|
|
110 |
setSettedAddress(resData.data.formatted_address)
|
|
|
111 |
handleModalOpen()
|
|
|
112 |
} else {
|
|
|
113 |
const resError = resData.data
|
|
|
114 |
if (resError.constructor.name === 'Object') {
|
|
|
115 |
Object.entries(resError).map(([key, value]) => {
|
|
|
116 |
if (key in getValues()) {
|
|
|
117 |
setError(key, {
|
|
|
118 |
type: 'manual',
|
|
|
119 |
message: Array.isArray(value) ? value[0] : value,
|
|
|
120 |
})
|
|
|
121 |
}
|
|
|
122 |
})
|
|
|
123 |
} else {
|
|
|
124 |
dispatch(addNotification({ style: 'danger', msg: resError }))
|
|
|
125 |
}
|
|
|
126 |
}
|
|
|
127 |
})
|
|
|
128 |
setModalLoading(false)
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
useEffect(() => {
|
|
|
132 |
addressKeys.current.map((addressKey) => {
|
|
|
133 |
register(addressKey)
|
|
|
134 |
})
|
|
|
135 |
register('formatted_address', {
|
|
|
136 |
required: 'por favor seleccione una ubicación correcta',
|
|
|
137 |
})
|
|
|
138 |
}, [])
|
|
|
139 |
|
|
|
140 |
return (
|
|
|
141 |
<Modal show={isModalOpen} onHide={handleModalOpen}>
|
|
|
142 |
<Modal.Header closeButton>
|
|
|
143 |
<Modal.Title>{labels.location}</Modal.Title>
|
|
|
144 |
</Modal.Header>
|
|
|
145 |
<form onSubmit={handleSubmit(onSubmitHandler)}>
|
|
|
146 |
<Modal.Body>
|
|
|
147 |
<div className="form-group datefm">
|
|
|
148 |
<UbicationInput
|
|
|
149 |
onGetAddress={getAddressHandler}
|
|
|
150 |
settedQuery={watch('formatted_address')}
|
|
|
151 |
/>
|
|
|
152 |
<i className="fa fa-map-marker"></i>
|
|
|
153 |
{errors.formatted_address && (
|
|
|
154 |
<FormErrorFeedback>
|
|
|
155 |
{errors.formatted_address.message}
|
|
|
156 |
</FormErrorFeedback>
|
|
|
157 |
)}
|
|
|
158 |
</div>
|
|
|
159 |
</Modal.Body>
|
|
|
160 |
<Modal.Footer>
|
|
|
161 |
<Button size="sm" type="submit">
|
|
|
162 |
{labels.accept}
|
|
|
163 |
</Button>
|
|
|
164 |
<Button size="sm" variant="danger" onClick={handleModalOpen}>
|
|
|
165 |
{labels.cancel}
|
|
|
166 |
</Button>
|
|
|
167 |
</Modal.Footer>
|
|
|
168 |
</form>
|
|
|
169 |
{modalLoading && <Spinner />}
|
|
|
170 |
</Modal>
|
|
|
171 |
)
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
export default LocationModal
|