Proyectos de Subversion LeadersLinked - SPA

Rev

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