Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
11347 nelberth 1
import React, { useState } from "react";
2
import {axios} from "../../../utils";
3
import { useForm } from "react-hook-form";
4
import FormErrorFeedback from "../../../shared/form-error-feedback/FormErrorFeedback";
5
import Spinner from "../../../shared/loading-spinner/Spinner";
6
 
7
import styles from "./addGroupModal.module.scss";
8
 
9
const AddGroupTab = (props) => {
10
  // props destructuring
11
  const { onClose } = props;
12
 
13
  // states
14
  const [loading, setLoading] = useState(false);
15
 
16
  // react hook form
17
  const { register, handleSubmit, errors, setError, getValues } = useForm();
18
 
19
  const handleOnSubmit = async (data) => {
20
    setLoading(true);
21
    const formData = new FormData();
22
    Object.entries(data).map(([key, value]) => {
23
      formData.append(key, value);
24
    });
25
    const response = await axios.post("/chat/create-group", formData);
26
    const resData = await response.data;
27
     (resData);
28
    if (resData.success) {
29
      onClose();
30
    } else {
31
      if (typeof resData.data === "object") {
32
        Object.entries(resData.data).map(([key, value]) => {
33
          if (Object.keys(getValues()).includes(key)) {
34
            setError(key, {
35
              type: "manual",
36
              message: value[0],
37
            });
38
          }
39
        });
40
      }
41
    }
42
    setLoading(false);
43
  };
44
 
45
  return (
46
    <div className={styles.addGroupTab}>
47
      <form onSubmit={handleSubmit(handleOnSubmit)}>
48
        <label htmlFor="name" className={styles.label}>
49
          Nombre del grupo
50
        </label>
51
        <input
52
          type="text"
53
          name="name"
54
          id="name"
55
          maxLength="50"
56
          className={styles.input}
57
          autoFocus
58
          ref={register}
59
        />
60
        {errors.name && (
61
          <div className={styles.errorMessage}>
62
            <FormErrorFeedback>{errors.name.message}</FormErrorFeedback>
63
          </div>
64
        )}
65
        <div className={styles.buttonsContainer}>
66
          <button className={styles.button} onClick={onClose} type="button">
67
            Cancelar
68
          </button>
69
          <button className={styles.button} type="submit">
70
            Crear
71
          </button>
72
        </div>
73
      </form>
74
      {loading && (
75
        <div className="spinner-container">
76
          <Spinner />
77
        </div>
78
      )}
79
    </div>
80
  );
81
};
82
 
83
export default AddGroupTab;