Proyectos de Subversion LeadersLinked - Backend

Rev

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