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