| 3719 |
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 = false, onConfirm = () => {}, onClose = () => {} }) => {
|
|
|
10 |
const labels = useSelector(({ intl }) => intl.labels);
|
|
|
11 |
|
|
|
12 |
const {
|
|
|
13 |
register,
|
|
|
14 |
handleSubmit,
|
|
|
15 |
setValue,
|
|
|
16 |
clearErrors,
|
|
|
17 |
watch,
|
|
|
18 |
reset,
|
|
|
19 |
formState: { errors }
|
|
|
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 title={labels.location} show={show} onClose={toggleModal} onAccept={onSubmit}>
|
|
|
47 |
<UbicationInput onGetAddress={addressHandler} settedQuery={watch('formatted_address')} />
|
|
|
48 |
{errors.formatted_address && (
|
|
|
49 |
<FormErrorFeedback>{errors.formatted_address.message}</FormErrorFeedback>
|
|
|
50 |
)}
|
|
|
51 |
</Modal>
|
|
|
52 |
);
|
|
|
53 |
};
|
|
|
54 |
|
|
|
55 |
export default LocationModal;
|