| 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 = '',
 | 
        
           |  |  | 14 |   onCreate = () => null,
 | 
        
           |  |  | 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' }))
 | 
        
           |  |  | 81 |       onCreate()
 | 
        
           |  |  | 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 (
 | 
        
           |  |  | 93 |     <Modal show={show}>
 | 
        
           |  |  | 94 |       <Modal.Header>
 | 
        
           |  |  | 95 |         <Modal.Title>{labels.create_conference}</Modal.Title>
 | 
        
           |  |  | 96 |       </Modal.Header>
 | 
        
           |  |  | 97 |       <form onSubmit={onSubmit} autoComplete="new-password">
 | 
        
           |  |  | 98 |         <div className="form-group">
 | 
        
           |  |  | 99 |           <label htmlFor="first_name">Título</label>
 | 
        
           |  |  | 100 |           <input
 | 
        
           |  |  | 101 |             type="text"
 | 
        
           |  |  | 102 |             name="title"
 | 
        
           |  |  | 103 |             className="form-control"
 | 
        
           |  |  | 104 |             maxLength={128}
 | 
        
           |  |  | 105 |             ref={register({ required: 'Por favor ingrese un título' })}
 | 
        
           |  |  | 106 |           />
 | 
        
           |  |  | 107 |           {errors.title && (
 | 
        
           |  |  | 108 |             <FormErrorFeedback>{errors.title.message}</FormErrorFeedback>
 | 
        
           |  |  | 109 |           )}
 | 
        
           |  |  | 110 |         </div>
 | 
        
           |  |  | 111 |         <div className="form-group">
 | 
        
           |  |  | 112 |           <label htmlFor="first_name">Descripción</label>
 | 
        
           |  |  | 113 |           <input
 | 
        
           |  |  | 114 |             type="text"
 | 
        
           |  |  | 115 |             name="description"
 | 
        
           |  |  | 116 |             className="form-control"
 | 
        
           |  |  | 117 |             ref={register({ required: 'Por favor ingrese una descripción' })}
 | 
        
           |  |  | 118 |           />
 | 
        
           |  |  | 119 |           {errors.description && (
 | 
        
           |  |  | 120 |             <FormErrorFeedback>{errors.description.message}</FormErrorFeedback>
 | 
        
           |  |  | 121 |           )}
 | 
        
           |  |  | 122 |         </div>
 | 
        
           |  |  | 123 |         <div className="form-group">
 | 
        
           |  |  | 124 |           <label htmlFor="timezone">Tipo de conferencia</label>
 | 
        
           |  |  | 125 |           <select name="type" className="form-control" ref={register}>
 | 
        
           |  |  | 126 |             <option value="i">Inmediata</option>
 | 
        
           |  |  | 127 |             <option value="s">Programada</option>
 | 
        
           |  |  | 128 |           </select>
 | 
        
           |  |  | 129 |         </div>
 | 
        
           |  |  | 130 |         {getValues('type') === 's' && (
 | 
        
           |  |  | 131 |           <div className="form-group">
 | 
        
           |  |  | 132 |             <label htmlFor="timezone">Horario</label>
 | 
        
           |  |  | 133 |             <Datetime
 | 
        
           |  |  | 134 |               dateFormat="DD-MM-YYYY"
 | 
        
           |  |  | 135 |               onChange={(e) => {
 | 
        
           |  |  | 136 |                 if (e.toDate) {
 | 
        
           |  |  | 137 |                   handleDateTime(e.toDate())
 | 
        
           |  |  | 138 |                 }
 | 
        
           |  |  | 139 |               }}
 | 
        
           |  |  | 140 |               inputProps={{ className: 'form-control' }}
 | 
        
           |  |  | 141 |               initialValue={Date.parse(new Date())}
 | 
        
           |  |  | 142 |               closeOnSelect
 | 
        
           |  |  | 143 |             />
 | 
        
           |  |  | 144 |           </div>
 | 
        
           |  |  | 145 |         )}
 | 
        
           |  |  | 146 |         <div className="form-group">
 | 
        
           |  |  | 147 |           <label htmlFor="timezone">Zona horaria</label>
 | 
        
           |  |  | 148 |           <select
 | 
        
           |  |  | 149 |             className="form-control"
 | 
        
           |  |  | 150 |             name="timezone"
 | 
        
           |  |  | 151 |             ref={register({ required: 'Por favor elige una Zona horaria' })}
 | 
        
           |  |  | 152 |           >
 | 
        
           |  |  | 153 |             <option value="" hidden>
 | 
        
           |  |  | 154 |               Zona horaria
 | 
        
           |  |  | 155 |             </option>
 | 
        
           |  |  | 156 |             {Object.entries(timezones).map(([key, value]) => (
 | 
        
           |  |  | 157 |               <option value={key} key={key}>
 | 
        
           |  |  | 158 |                 {value}
 | 
        
           |  |  | 159 |               </option>
 | 
        
           |  |  | 160 |             ))}
 | 
        
           |  |  | 161 |           </select>
 | 
        
           |  |  | 162 |           {errors.timezone && (
 | 
        
           |  |  | 163 |             <FormErrorFeedback>{errors.timezone.message}</FormErrorFeedback>
 | 
        
           |  |  | 164 |           )}
 | 
        
           |  |  | 165 |         </div>
 | 
        
           |  |  | 166 |         <div className="form-group">
 | 
        
           |  |  | 167 |           <label htmlFor="timezone">Duración</label>
 | 
        
           |  |  | 168 |           <select className="form-control" name="duration" ref={register}>
 | 
        
           |  |  | 169 |             <option value={5}>5-min</option>
 | 
        
           |  |  | 170 |             <option value={10}>10-min</option>
 | 
        
           |  |  | 171 |             <option value={15}>15-min</option>
 | 
        
           |  |  | 172 |             <option value={20}>20-min</option>
 | 
        
           |  |  | 173 |             <option value={25}>25-min</option>
 | 
        
           |  |  | 174 |             <option value={30}>30-min</option>
 | 
        
           |  |  | 175 |             <option value={35}>35-min</option>
 | 
        
           |  |  | 176 |             <option value={40}>40-min</option>
 | 
        
           |  |  | 177 |             <option value={45}>45-min</option>
 | 
        
           |  |  | 178 |           </select>
 | 
        
           |  |  | 179 |         </div>
 | 
        
           |  |  | 180 |         <div className="form-group">
 | 
        
           |  |  | 181 |           <label htmlFor="first_name">Contraseña de ingreso</label>
 | 
        
           |  |  | 182 |           <input
 | 
        
           |  |  | 183 |             type="password"
 | 
        
           |  |  | 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 && (
 | 
        
           |  |  | 195 |             <FormErrorFeedback>{errors.password.message}</FormErrorFeedback>
 | 
        
           |  |  | 196 |           )}
 | 
        
           |  |  | 197 |         </div>
 | 
        
           |  |  | 198 |         <button className="btn btn-primary" type="submit">
 | 
        
           |  |  | 199 |           Crear
 | 
        
           |  |  | 200 |         </button>
 | 
        
           |  |  | 201 |       </form>
 | 
        
           |  |  | 202 |     </Modal>
 | 
        
           |  |  | 203 |   )
 | 
        
           |  |  | 204 | }
 | 
        
           |  |  | 205 |   | 
        
           |  |  | 206 | export default ConferenceModal
 |