Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
3028 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 { getMonths, getYears } from '@app/utils/dates'
7
import { useFetchHelper } from '@hooks'
8
 
3269 stevensc 9
import CKEditor from '@components/common/ckeditor/Ckeditor'
3028 stevensc 10
import SwitchInput from '@app/components/UI/SwitchInput'
11
import FormErrorFeedback from '@app/components/UI/form/FormErrorFeedback'
12
import Select from '@app/components/UI/inputs/Select'
13
import Input from '@app/components/UI/inputs/Input'
14
import UbicationInput from '@app/components/UI/inputs/UbicationInput'
15
import Modal from '@app/components/UI/modal/Modal'
16
 
17
const ExperienceModal = ({
18
  show = false,
19
  onClose = () => {},
20
  onConfirm = () => {},
21
  currentExperience = null
22
}) => {
23
  const { data: companySizes } = useFetchHelper('company-sizes')
24
  const { data: industries } = useFetchHelper('industries')
25
  const labels = useSelector(({ intl }) => intl.labels)
26
 
27
  const {
28
    register,
29
    handleSubmit,
30
    setValue,
31
    control,
32
    watch,
33
    reset,
34
    formState: { errors }
35
  } = useForm({
36
    defaultValues: {
37
      company: '',
38
      description: '',
39
      from_month: '',
40
      from_year: '',
41
      industry_id: '',
42
      is_current: 'n',
43
      company_size_id: '',
44
      title: '',
45
      to_month: '',
46
      to_year: ''
47
    }
48
  })
49
  const isCurrent = watch('is_current', 'n') === 'y'
50
 
51
  const handleClose = () => {
52
    reset()
53
    onClose()
54
  }
55
 
56
  const addressHandler = (address) => {
57
    Object.entries(address).forEach(([key, value]) => {
58
      if (value) {
59
        register(key)
60
        setValue(key, value)
61
      }
62
    })
63
  }
64
 
65
  const onSubmit = handleSubmit((data) => onConfirm(data))
66
 
67
  useEffect(() => {
68
    register('formatted_address', { required: 'Este campo es requerido' })
69
    register('description', { required: 'Este campo es requerido' })
70
    register('is_current', { required: 'Este campo es requerido' })
71
  }, [])
72
 
73
  useEffect(() => {
74
    if (!currentExperience) return
75
 
76
    const {
77
      company,
78
      description,
79
      from_month,
80
      from_year,
81
      industry,
82
      is_current,
83
      size,
84
      title,
85
      to_month,
86
      to_year
87
    } = currentExperience
88
 
89
    setValue('company', company)
90
    setValue('description', description)
91
    setValue('from_month', from_month)
92
    setValue('from_year', from_year)
93
    setValue('is_current', is_current)
94
    setValue('title', title)
95
    setValue('to_month', to_month)
96
    setValue('to_year', to_year)
97
 
98
    const currentSize = companySizes.find(({ name }) => size.includes(name))
99
    const currentIndustry = industries.find(({ name }) => name === industry)
100
 
101
    if (currentSize) {
102
      setValue('company_size_id', currentSize.value)
103
    }
104
    if (currentIndustry) {
105
      setValue('industry_id', currentIndustry.value)
106
    }
107
  }, [currentExperience, companySizes, industries])
108
 
109
  useEffect(() => {
110
    if (isCurrent) {
111
      setValue('to_month', '')
112
      setValue('to_year', '')
113
    }
114
  }, [isCurrent])
115
 
116
  return (
117
    <Modal
118
      title={`${currentExperience ? labels.edit : labels.add} - ${
119
        labels.experience
120
      }`}
121
      show={show}
122
      onClose={handleClose}
123
      onAccept={onSubmit}
124
    >
125
      <Input
126
        name='company'
127
        placeholder='Empresa'
128
        error={errors.company?.message}
129
        control={control}
130
        rules={{ required: 'Este campo es requerido' }}
131
        label='Empresa'
132
      />
133
 
134
      <Select
135
        name='industry_id'
136
        error={errors.industry_id?.message}
137
        options={industries}
138
        placeholder='Industria'
139
        label='Industria'
140
        control={control}
141
        rules={{ required: 'Este campo es requerido' }}
142
      />
143
 
144
      <Select
145
        name='company_size_id'
146
        error={errors.company_size_id?.message}
147
        options={companySizes}
148
        placeholder='Tamaño de la Empresa'
149
        label='Tamaño de la Empresa'
150
        control={control}
151
        rules={{ required: 'Este campo es requerido' }}
152
      />
153
 
154
      <Input
155
        name='title'
156
        label='Titulo'
157
        placeholder='Titulo'
158
        rules={{ required: 'Este campo es requerido' }}
159
        control={control}
160
        error={errors.title?.message}
161
      />
162
 
163
      <UbicationInput
164
        onGetAddress={addressHandler}
165
        error={errors.formatted_address?.message}
166
        placeholder='Ubicación'
167
      />
168
 
169
      <Box sx={{ display: 'flex', gap: 1 }}>
170
        <Select
171
          name='from_month'
172
          error={errors.from_month?.message}
173
          label='Desde'
174
          placeholder='Mes desde'
175
          options={getMonths().map((month, i) => ({
176
            name: month,
177
            value: i + 1
178
          }))}
179
          control={control}
180
          rules={{ required: 'Este campo es requerido' }}
181
        />
182
 
183
        <Select
184
          error={errors.from_year?.message}
185
          options={getYears().map((year) => ({
186
            name: year,
187
            value: year
188
          }))}
189
          placeholder='Año desde'
190
          name='from_year'
191
          rules={{ required: 'Este campo es requerido' }}
192
          control={control}
193
          label='Desde'
194
        />
195
      </Box>
196
 
197
      {!isCurrent && (
198
        <Box sx={{ display: 'flex', gap: 1 }}>
199
          <Select
200
            error={errors.to_month?.message}
201
            options={getMonths().map((month, i) => ({
202
              name: month,
203
              value: i + 1
204
            }))}
205
            placeholder='Mes hasta'
206
            label='Hasta'
207
            name='to_month'
208
            rules={{
209
              required: isCurrent ? false : 'Este campo es requerido',
210
              validate: (value) => {
211
                const currentMonth = new Date().getMonth() + 1
212
                const currentYear = new Date().getFullYear()
213
                const toYear = watch('to_year')
214
 
215
                return (
216
                  (toYear === currentYear && value < currentMonth) ||
217
                  'La fecha no puede ser superior a la actual'
218
                )
219
              }
220
            }}
221
            control={control}
222
          />
223
 
224
          <Select
225
            error={errors.to_year?.message}
226
            options={getYears().map((year) => ({
227
              name: year,
228
              value: year
229
            }))}
230
            placeholder='Año hasta'
231
            name='to_year'
232
            rules={{
233
              required: isCurrent ? false : 'Este campo es requerido'
234
            }}
235
            control={control}
236
            label='Hasta'
237
          />
238
        </Box>
239
      )}
240
 
241
      <label htmlFor='is_current'> Trabajo Actual</label>
242
      <Box sx={{ display: 'block', mb: 1 }}>
243
        <SwitchInput
244
          isChecked={isCurrent}
245
          setValue={(val) => setValue('is_current', val ? 'y' : 'n')}
246
        />
247
      </Box>
248
 
249
      <CKEditor
250
        defaultValue={currentExperience?.description ?? ''}
251
        onChange={(value) => setValue('description', value)}
252
      />
253
      {errors.description && (
254
        <FormErrorFeedback>Este campo es requerido</FormErrorFeedback>
255
      )}
256
    </Modal>
257
  )
258
}
259
 
260
export default ExperienceModal