| 3719 |
stevensc |
1 |
import React, { useEffect } from 'react';
|
|
|
2 |
import { useSelector } from 'react-redux';
|
|
|
3 |
import { useForm } from 'react-hook-form';
|
|
|
4 |
import { Box } from '@mui/material';
|
|
|
5 |
|
|
|
6 |
import { getYears } from '@utils/dates';
|
|
|
7 |
import { useFetchHelper } from '@hooks';
|
|
|
8 |
|
|
|
9 |
import CKEditor from '@components/common/ckeditor/Ckeditor';
|
|
|
10 |
import Modal from '@app/components/UI/modal/Modal';
|
|
|
11 |
import Select from '@app/components/UI/inputs/Select';
|
|
|
12 |
import FormErrorFeedback from '@app/components/UI/form/FormErrorFeedback';
|
|
|
13 |
import Input from '@app/components/UI/inputs/Input';
|
|
|
14 |
import SwitchInput from '@app/components/UI/SwitchInput';
|
|
|
15 |
import UbicationInput from '@app/components/UI/inputs/UbicationInput';
|
|
|
16 |
|
|
|
17 |
const EducationModal = ({
|
|
|
18 |
show = false,
|
|
|
19 |
currentEducation = null,
|
|
|
20 |
onClose = () => {},
|
|
|
21 |
onConfirm = () => {}
|
|
|
22 |
}) => {
|
|
|
23 |
const { data: degrees } = useFetchHelper('degrees');
|
|
|
24 |
const labels = useSelector(({ intl }) => intl.labels);
|
|
|
25 |
|
|
|
26 |
const {
|
|
|
27 |
control,
|
|
|
28 |
formState: { errors },
|
|
|
29 |
handleSubmit,
|
|
|
30 |
setValue,
|
|
|
31 |
register,
|
|
|
32 |
watch,
|
|
|
33 |
reset
|
|
|
34 |
} = useForm({
|
|
|
35 |
defaultValues: {
|
|
|
36 |
university: '',
|
|
|
37 |
degree_id: '',
|
|
|
38 |
field_of_study: '',
|
|
|
39 |
grade_or_percentage: '',
|
|
|
40 |
formatted_address: '',
|
|
|
41 |
is_current: 'n',
|
|
|
42 |
from_year: '',
|
|
|
43 |
to_year: '',
|
|
|
44 |
description: ''
|
|
|
45 |
},
|
|
|
46 |
values: currentEducation
|
|
|
47 |
});
|
|
|
48 |
const isCurrent = watch('is_current', 'n') === 'y';
|
|
|
49 |
|
|
|
50 |
const handleAddress = (address) => {
|
|
|
51 |
Object.entries(address).forEach(([key, value]) => {
|
|
|
52 |
if (value) {
|
|
|
53 |
register(key);
|
|
|
54 |
setValue(key, value);
|
|
|
55 |
}
|
|
|
56 |
});
|
|
|
57 |
};
|
|
|
58 |
|
|
|
59 |
const handleCloseModal = () => {
|
|
|
60 |
reset();
|
|
|
61 |
onClose();
|
|
|
62 |
};
|
|
|
63 |
|
|
|
64 |
const onSubmitHandler = handleSubmit((data) => onConfirm(data));
|
|
|
65 |
|
|
|
66 |
useEffect(() => {
|
|
|
67 |
register('formatted_address', { required: 'Este campo es requerido' });
|
|
|
68 |
register('description', { required: 'Este campo es requerido' });
|
|
|
69 |
register('is_current');
|
|
|
70 |
}, []);
|
|
|
71 |
|
|
|
72 |
useEffect(() => {
|
|
|
73 |
if (!currentEducation) return;
|
|
|
74 |
const currentDegree = degrees.find(({ name }) => name === currentEducation.degree);
|
|
|
75 |
if (currentDegree) setValue('degree_id', currentDegree.value);
|
|
|
76 |
}, [currentEducation, degrees]);
|
|
|
77 |
|
|
|
78 |
return (
|
|
|
79 |
<Modal
|
|
|
80 |
title={labels.education}
|
|
|
81 |
show={show}
|
|
|
82 |
onClose={handleCloseModal}
|
|
|
83 |
onAccept={onSubmitHandler}
|
|
|
84 |
>
|
|
|
85 |
<Select
|
|
|
86 |
name='degree_id'
|
|
|
87 |
label={labels.degree}
|
|
|
88 |
options={degrees}
|
|
|
89 |
control={control}
|
|
|
90 |
rules={{ required: 'Este campo es requerido' }}
|
|
|
91 |
error={errors.degree_id?.message}
|
|
|
92 |
/>
|
|
|
93 |
|
|
|
94 |
<Input
|
|
|
95 |
name='university'
|
|
|
96 |
label='Universidad'
|
|
|
97 |
placeholder='Nombre de la universidad'
|
|
|
98 |
control={control}
|
|
|
99 |
rules={{ required: 'Este campo es requerido' }}
|
|
|
100 |
error={errors.university?.message}
|
|
|
101 |
/>
|
|
|
102 |
|
|
|
103 |
<Input
|
|
|
104 |
name='field_of_study'
|
|
|
105 |
label='Campo de estudio'
|
|
|
106 |
placeholder='Campo de estudio'
|
|
|
107 |
control={control}
|
|
|
108 |
rules={{ required: 'Este campo es requerido' }}
|
|
|
109 |
error={errors.field_of_study?.message}
|
|
|
110 |
/>
|
|
|
111 |
|
|
|
112 |
<UbicationInput
|
|
|
113 |
onGetAddress={handleAddress}
|
|
|
114 |
error={errors.formatted_address?.message}
|
|
|
115 |
placeholder='Ubicación'
|
|
|
116 |
/>
|
|
|
117 |
{errors.formatted_address && (
|
|
|
118 |
<FormErrorFeedback>{errors.formatted_address.message}</FormErrorFeedback>
|
|
|
119 |
)}
|
|
|
120 |
|
|
|
121 |
<Input
|
|
|
122 |
name='grade_or_percentage'
|
|
|
123 |
label='Grado o porcentaje'
|
|
|
124 |
placeholder='Grado o porcentaje'
|
|
|
125 |
control={control}
|
|
|
126 |
rules={{ required: 'Este campo es requerido' }}
|
|
|
127 |
error={errors.grade_or_percentage?.message}
|
|
|
128 |
/>
|
|
|
129 |
|
|
|
130 |
<Select
|
|
|
131 |
name='from_year'
|
|
|
132 |
control={control}
|
|
|
133 |
label='Desde'
|
|
|
134 |
placeholder={labels.from_year}
|
|
|
135 |
rules={{ required: 'Este campo es requerido' }}
|
|
|
136 |
error={errors.from_year?.message}
|
|
|
137 |
options={getYears().map((year) => ({ label: year, value: year }))}
|
|
|
138 |
/>
|
|
|
139 |
|
|
|
140 |
{!isCurrent && (
|
|
|
141 |
<Select
|
|
|
142 |
name='to_year'
|
|
|
143 |
control={control}
|
|
|
144 |
label='Hasta'
|
|
|
145 |
placeholder='Año hasta'
|
|
|
146 |
rules={{ required: 'Este campo es requerido' }}
|
|
|
147 |
error={errors.to_year?.message}
|
|
|
148 |
options={getYears().map((year) => ({ label: year, value: year }))}
|
|
|
149 |
/>
|
|
|
150 |
)}
|
|
|
151 |
|
|
|
152 |
<label htmlFor='is_current'>{labels.education} Actual</label>
|
|
|
153 |
<Box sx={{ display: 'block', mb: 1 }}>
|
|
|
154 |
<SwitchInput
|
|
|
155 |
isChecked={isCurrent}
|
|
|
156 |
setValue={(val) => setValue('is_current', val ? 'y' : 'n')}
|
|
|
157 |
/>
|
|
|
158 |
</Box>
|
|
|
159 |
|
|
|
160 |
<CKEditor
|
|
|
161 |
control={control}
|
|
|
162 |
name='description'
|
|
|
163 |
rules={{ required: 'Este campo es requerido' }}
|
|
|
164 |
error={errors.description?.message}
|
|
|
165 |
/>
|
|
|
166 |
</Modal>
|
|
|
167 |
);
|
|
|
168 |
};
|
|
|
169 |
|
|
|
170 |
export default EducationModal;
|