Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
16275 stevensc 1
import React, { useState } from "react";
16274 stevensc 2
import Modal from "react-bootstrap/Modal";
16275 stevensc 3
import { useDispatch } from "react-redux";
4
import Datetime from "react-datetime";
5
import { axios } from "../../../../utils";
6
import { addNotification } from "../../../../redux/notification/notification.actions";
16276 stevensc 7
import { useForm } from "react-hook-form";
16253 stevensc 8
 
9
const ConferenceModal = ({
16266 stevensc 10
  isShow = false,
16253 stevensc 11
  timezones = {},
12
  zoomUrl = "",
16266 stevensc 13
  onClose = () => null,
16253 stevensc 14
  onCreate = () => null,
15
}) => {
16275 stevensc 16
  const dt = new Date();
17
  const { handleSubmit, register, errors, reset } = useForm({ mode: "all" });
18
  const [date, setDate] = useState({
19
    year: dt.toLocaleString("default", { year: "numeric" }),
20
    month: dt.toLocaleString("default", { month: "2-digit" }),
21
    day: dt.toLocaleString("default", { day: "2-digit" }),
22
  });
23
  const [time, setTime] = useState(
24
    dt.toLocaleString("es", {
25
      hour: "numeric",
26
      minute: "2-digit",
27
      second: "2-digit",
28
    })
29
  );
30
  const [coferenceType, setConferenceType] = useState(1);
31
 
32
  const dispatch = useDispatch();
33
 
34
  const handleChange = (value) => {
35
    setConferenceType(value);
36
  };
37
 
38
  const handleDateTime = (value) => {
39
    setDate({
40
      ...date,
41
      year: new Intl.DateTimeFormat("es", { year: "numeric" }).format(value),
42
      month: new Intl.DateTimeFormat("es", { month: "2-digit" }).format(value),
43
      day: new Intl.DateTimeFormat("es", { day: "2-digit" }).format(value),
44
    });
45
    setTime(
46
      new Intl.DateTimeFormat("es", {
47
        hour: "numeric",
48
        minute: "2-digit",
49
        second: "numeric",
50
      }).format(value)
51
    );
52
  };
53
 
54
  const onSubmit = async (data) => {
55
    try {
56
      const formData = new FormData();
57
 
58
      Object.entries(data).forEach(([key, value]) =>
59
        formData.append(key, value)
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.message}`);
85
      dispatch(
86
        addNotification({ msg: "Ha ocurrido un error", style: "danger" })
87
      );
88
    }
89
  };
90
 
16253 stevensc 91
  return (
16266 stevensc 92
    <Modal show={isShow} onHide={onClose}>
16253 stevensc 93
      <Modal.Header closeButton>
94
        <Modal.Title>Crear Conferencia</Modal.Title>
95
      </Modal.Header>
16264 stevensc 96
      <Modal.Body>
16275 stevensc 97
        <form onSubmit={handleSubmit(onSubmit)} autoComplete="new-password">
16264 stevensc 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}
16275 stevensc 105
              ref={register({ required: "Por favor ingrese un título" })}
16264 stevensc 106
            />
16275 stevensc 107
            {errors.title && (
108
              <FormErrorFeedback>{errors.title.message}</FormErrorFeedback>
109
            )}
16264 stevensc 110
          </div>
111
          <div className="form-group">
112
            <label htmlFor="first_name">Descripción</label>
16275 stevensc 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>
121
                {errors.description.message}
122
              </FormErrorFeedback>
123
            )}
16264 stevensc 124
          </div>
125
          <div className="form-group">
126
            <label htmlFor="timezone">Tipo de conferencia</label>
16275 stevensc 127
            <select
128
              name="type"
129
              className="form-control"
130
              onChange={({ target }) => handleChange(target.value)}
131
              ref={register}
132
            >
16264 stevensc 133
              <option value="i">Inmediata</option>
134
              <option value="s">Programada</option>
135
            </select>
136
          </div>
16275 stevensc 137
          {coferenceType === "s" && (
138
            <div className="form-group">
139
              <label htmlFor="timezone">Horario</label>
140
              <Datetime
141
                dateFormat="DD-MM-YYYY"
142
                onChange={(e) => e.toDate && handleDateTime(e.toDate())}
143
                inputProps={{ className: "form-control" }}
144
                initialValue={Date.parse(new Date())}
145
                closeOnSelect
146
              />
147
            </div>
148
          )}
16264 stevensc 149
          <div className="form-group">
150
            <label htmlFor="timezone">Zona horaria</label>
16275 stevensc 151
            <select
152
              className="form-control"
153
              name="timezone"
154
              ref={register({ required: "Por favor elige una Zona horaria" })}
155
            >
16264 stevensc 156
              <option value="" hidden>
157
                Zona horaria
158
              </option>
16275 stevensc 159
              {Object.entries(timezones).map(([key, value]) => (
160
                <option value={key} key={key}>
161
                  {value}
162
                </option>
163
              ))}
16264 stevensc 164
            </select>
16275 stevensc 165
            {errors.timezone && (
166
              <FormErrorFeedback>{errors.timezone.message}</FormErrorFeedback>
167
            )}
16264 stevensc 168
          </div>
169
          <div className="form-group">
170
            <label htmlFor="timezone">Duración</label>
16275 stevensc 171
            <select className="form-control" name="duration" ref={register}>
16264 stevensc 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>
16275 stevensc 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
              })}
196
            />
197
            {errors.password && (
198
              <FormErrorFeedback>{errors.password.message}</FormErrorFeedback>
199
            )}
16264 stevensc 200
          </div>
201
          <button className="btn btn-primary" type="submit">
202
            Crear
203
          </button>
204
        </form>
205
      </Modal.Body>
16253 stevensc 206
    </Modal>
207
  );
208
};
209
 
210
export default ConferenceModal;