Proyectos de Subversion LeadersLinked - Backend

Rev

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

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