Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16254 | Ir a la última revisión | | Ultima modificación | Ver Log |

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