Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3432 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3719 stevensc 1
import React, { useState } from 'react';
2
import { useDispatch, useSelector } from 'react-redux';
3
import { useForm } from 'react-hook-form';
4
import Datetime from 'react-datetime';
5
 
6
import { axios } from '../../utils';
7
import { useFetchHelper } from '@hooks';
8
import { addNotification } from 'store/notification/notification.actions';
9
 
10
import Modal from '@components/UI/modal/Modal';
11
import FormErrorFeedback from '@components/UI/form/FormErrorFeedback';
12
 
13
import 'react-datetime/css/react-datetime.css';
14
 
15
const ConferenceModal = ({ show = false, zoomUrl = '', onClose = () => null }) => {
16
  const dt = new Date();
17
  const [date, setDate] = useState({
18
    year: dt.toLocaleString('default', { year: 'numeric' }),
19
    month: dt.toLocaleString('default', { month: '2-digit' }),
20
    day: dt.toLocaleString('default', { day: '2-digit' })
21
  });
22
  const [time, setTime] = useState(
23
    dt.toLocaleString('es', {
24
      hour: 'numeric',
25
      minute: '2-digit',
26
      second: '2-digit'
27
    })
28
  );
29
  const { data: timezones } = useFetchHelper('timezones');
30
  const labels = useSelector(({ intl }) => intl.labels);
31
  const dispatch = useDispatch();
32
 
33
  const {
34
    handleSubmit,
35
    register,
36
    reset,
37
    getValues,
38
    formState: { errors }
39
  } = useForm({
40
    mode: 'all'
41
  });
42
 
43
  const handleDateTime = (value) => {
44
    setDate({
45
      ...date,
46
      year: new Intl.DateTimeFormat('es', { year: 'numeric' }).format(value),
47
      month: new Intl.DateTimeFormat('es', { month: '2-digit' }).format(value),
48
      day: new Intl.DateTimeFormat('es', { day: '2-digit' }).format(value)
49
    });
50
    setTime(
51
      new Intl.DateTimeFormat('es', {
52
        hour: 'numeric',
53
        minute: '2-digit',
54
        second: 'numeric'
55
      }).format(value)
56
    );
57
  };
58
 
59
  const onSubmit = handleSubmit(async (conference) => {
60
    try {
61
      const formData = new FormData();
62
 
63
      Object.entries(conference).forEach(([key, value]) => formData.append(key, value));
64
      formData.append('date', `${date.year}-${date.month}-${date.day}`);
65
      formData.append('time', time);
66
 
67
      const response = await axios.post(zoomUrl, formData);
68
      const { data, success } = response.data;
69
 
70
      if (!success && typeof data === 'string') {
71
        dispatch(addNotification({ msg: data, style: 'danger' }));
72
        return;
73
      }
74
 
75
      if (!success && typeof data === 'object') {
76
        Object.entries(data).forEach(([key, value]) => {
77
          dispatch(addNotification({ msg: `${key}: ${value[0]}`, style: 'danger' }));
78
        });
79
        return;
80
      }
81
 
82
      dispatch(addNotification({ msg: data, style: 'success' }));
83
      onClose();
84
      reset();
85
    } catch (err) {
86
      dispatch(addNotification({ style: 'danger', msg: err.message }));
87
    }
88
  });
89
 
90
  const generatePassword = () => {
91
    return Math.random().toString(36).slice(-6);
92
  };
93
 
94
  return (
95
    <Modal title={labels.create_conference} show={show} onClose={onClose} onAccept={onSubmit}>
96
      <div className='form-group'>
97
        <label htmlFor='first_name'>Título</label>
98
        <input
99
          type='text'
100
          name='title'
101
          className='form-control'
102
          maxLength={128}
103
          ref={register({ required: 'Por favor ingrese un título' })}
104
        />
105
        {errors.title && <FormErrorFeedback>{errors.title.message}</FormErrorFeedback>}
106
      </div>
107
 
108
      <div className='form-group'>
109
        <label htmlFor='first_name'>Descripción</label>
110
        <input
111
          type='text'
112
          name='description'
113
          className='form-control'
114
          ref={register({ required: 'Por favor ingrese una descripción' })}
115
        />
116
        {errors.description && <FormErrorFeedback>{errors.description.message}</FormErrorFeedback>}
117
      </div>
118
 
119
      <div className='form-group'>
120
        <label htmlFor='timezone'>Tipo de conferencia</label>
121
        <select name='type' className='form-control' ref={register}>
122
          <option value='i'>Inmediata</option>
123
          <option value='s'>Programada</option>
124
        </select>
125
      </div>
126
 
127
      {getValues('type') === 's' && (
128
        <div className='form-group'>
129
          <label htmlFor='timezone'>Horario</label>
130
          <Datetime
131
            dateFormat='DD-MM-YYYY'
132
            onChange={(e) => {
133
              if (e.toDate) {
134
                handleDateTime(e.toDate());
135
              }
136
            }}
137
            inputProps={{ className: 'form-control' }}
138
            initialValue={Date.parse(new Date())}
139
            closeOnSelect
140
          />
141
        </div>
142
      )}
143
 
144
      <div className='form-group'>
145
        <label htmlFor='timezone'>Zona horaria</label>
146
        <select
147
          className='form-control'
148
          name='timezone'
149
          ref={register({ required: 'Por favor elige una Zona horaria' })}
150
          defaultValue='America/Montevideo'
151
        >
152
          <option value='' hidden>
153
            Zona horaria
154
          </option>
155
          {timezones.map(({ name, value }) => (
156
            <option value={value} key={value}>
157
              {name}
158
            </option>
159
          ))}
160
        </select>
161
        {errors.timezone && <FormErrorFeedback>{errors.timezone.message}</FormErrorFeedback>}
162
      </div>
163
 
164
      <div className='form-group'>
165
        <label htmlFor='timezone'>Duración</label>
166
        <select className='form-control' name='duration' ref={register}>
167
          <option value={5}>5-min</option>
168
          <option value={10}>10-min</option>
169
          <option value={15}>15-min</option>
170
          <option value={20}>20-min</option>
171
          <option value={25}>25-min</option>
172
          <option value={30}>30-min</option>
173
          <option value={35}>35-min</option>
174
          <option value={40}>40-min</option>
175
          <option value={45}>45-min</option>
176
        </select>
177
      </div>
178
 
179
      <div className='form-group'>
180
        <label htmlFor='first_name'>Contraseña de ingreso</label>
181
        <input
182
          defaultValue={generatePassword()}
183
          type='text'
184
          name='password'
185
          className='form-control'
186
          ref={register({
187
            required: 'Por favor ingrese una contraseña',
188
            maxLength: {
189
              value: 6,
190
              message: 'La contraseña debe tener al menos 6 digitos'
191
            }
192
          })}
193
        />
194
        {errors.password && <FormErrorFeedback>{errors.password.message}</FormErrorFeedback>}
195
      </div>
196
    </Modal>
197
  );
198
};
199
 
200
export default ConferenceModal;