Proyectos de Subversion LeadersLinked - SPA

Rev

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

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