Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
3040 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 '@app/components/UI/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
    handleSubmit,
29
    setValue,
30
    register,
31
    watch,
32
    reset,
33
    formState: { errors }
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: ''
3041 stevensc 45
    },
46
    values: currentEducation
3040 stevensc 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: true })
69
    register('is_current')
70
  }, [])
71
 
72
  useEffect(() => {
73
    register('formatted_address', { required: 'Este campo es requerido' })
74
    register('description', { required: 'Este campo es requerido' })
75
    register('is_current', { required: 'Este campo es requerido' })
76
  }, [])
77
 
78
  useEffect(() => {
79
    if (!currentEducation) return
80
 
81
    const {
82
      university,
83
      degree,
84
      field_of_study,
85
      grade_or_percentage,
86
      is_current,
87
      from_year,
88
      to_year,
89
      description
90
    } = currentEducation
91
 
92
    setValue('university', university)
93
    setValue('field_of_study', field_of_study)
94
    setValue('grade_or_percentage', grade_or_percentage)
95
    setValue('description', description)
96
    setValue('from_year', from_year)
97
    setValue('is_current', is_current)
98
    setValue('to_year', to_year)
99
 
100
    const currentDegree = degrees.find(({ name }) => name === degree)
101
 
102
    if (currentDegree) {
103
      setValue('degree_id', currentDegree.value)
104
    }
105
  }, [currentEducation, degrees])
106
 
107
  useEffect(() => {
108
    if (isCurrent) {
109
      setValue('to_year', '')
110
    }
111
  }, [isCurrent])
112
 
113
  return (
114
    <Modal
115
      title={labels.education}
116
      show={show}
117
      onClose={handleCloseModal}
118
      onAccept={onSubmitHandler}
119
    >
120
      <Select
121
        name='degree_id'
122
        label={labels.degree}
123
        options={degrees}
124
        control={control}
125
        rules={{ required: 'Este campo es requerido' }}
126
        error={errors.degree_id?.message}
127
      />
128
 
129
      <Input
130
        name='university'
131
        label='Universidad'
132
        placeholder='Nombre de la universidad'
133
        control={control}
134
        rules={{ required: 'Este campo es requerido' }}
135
        error={errors.university?.message}
136
      />
137
 
138
      <Input
139
        name='field_of_study'
140
        label='Campo de estudio'
141
        placeholder='Campo de estudio'
142
        control={control}
143
        rules={{ required: 'Este campo es requerido' }}
144
        error={errors.field_of_study?.message}
145
      />
146
 
147
      <UbicationInput
148
        onGetAddress={handleAddress}
149
        error={errors.formatted_address?.message}
150
        placeholder='Ubicación'
151
      />
152
      {errors.formatted_address && (
153
        <FormErrorFeedback>
154
          {errors.formatted_address.message}
155
        </FormErrorFeedback>
156
      )}
157
 
158
      <Input
159
        name='grade_or_percentage'
160
        label='Grado o porcentaje'
161
        placeholder='Grado o porcentaje'
162
        control={control}
163
        rules={{ required: 'Este campo es requerido' }}
164
        error={errors.grade_or_percentage?.message}
165
      />
166
 
167
      <Select
168
        name='from_year'
169
        control={control}
170
        label='Desde'
171
        placeholder={labels.from_year}
172
        rules={{ required: 'Este campo es requerido' }}
173
        error={errors.from_year?.message}
174
        options={getYears().map((year) => ({ name: year, value: year }))}
175
      />
176
 
177
      {!isCurrent && (
178
        <Select
179
          name='to_year'
180
          control={control}
181
          label='Hasta'
182
          placeholder='Año hasta'
183
          rules={{ required: 'Este campo es requerido' }}
184
          error={errors.to_year?.message}
185
          options={getYears().map((year) => ({ name: year, value: year }))}
186
        />
187
      )}
188
 
189
      <label htmlFor='is_current'>{labels.education} Actual</label>
190
      <Box sx={{ display: 'block', mb: 1 }}>
191
        <SwitchInput
192
          isChecked={isCurrent}
193
          setValue={(val) => setValue('is_current', val ? 'y' : 'n')}
194
        />
195
      </Box>
196
 
197
      <CKEditor
198
        control={control}
199
        name='description'
200
        rules={{ required: 'Este campo es requerido' }}
201
        error={errors.description?.message}
202
      />
203
    </Modal>
204
  )
205
}
206
 
207
export default EducationModal