Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16256 | Rev 16259 | 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
          )}
16257 stevensc 161
 
16253 stevensc 162
          <div className="form-group">
163
            <label htmlFor="timezone">Duración</label>
164
            <select className="form-control" name="duration" ref={register}>
165
              <option value={5}>5-min</option>
166
              <option value={10}>10-min</option>
167
              <option value={15}>15-min</option>
168
              <option value={20}>20-min</option>
169
              <option value={25}>25-min</option>
170
              <option value={30}>30-min</option>
171
              <option value={35}>35-min</option>
172
              <option value={40}>40-min</option>
173
              <option value={45}>45-min</option>
174
            </select>
175
          </div>
176
          <div className="form-group">
177
            <label htmlFor="first_name">Contraseña de ingreso</label>
178
            <input
179
              type="password"
180
              name="password"
181
              className="form-control"
182
              ref={register({
183
                required: "Por favor ingrese una contraseña",
184
                maxLength: {
185
                  value: 6,
186
                  message: "La contraseña debe tener al menos 6 digitos",
187
                },
188
              })}
189
            />
190
            {errors.password && (
191
              <FormErrorFeedback>{errors.password.message}</FormErrorFeedback>
192
            )}
193
          </div>
194
          <button className="btn btn-primary" type="submit">
195
            Crear
196
          </button>
197
        </form>
198
      </Modal.Body>
199
    </Modal>
200
  );
201
};
202
 
203
export default ConferenceModal;