Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
4638 stevensc 1
import React from "react";
2
import { useState, useEffect } from "react";
3
import { Button, Modal } from "react-bootstrap";
4
import { useForm } from "react-hook-form";
5
import styled from "styled-components";
6
import { axios } from "../../../../../../../utils";
7
import FormErrorFeedback from "../../../../../../../shared/form-error-feedback/FormErrorFeedback";
8
import Spinner from "../../../../../../../shared/loading-spinner/Spinner";
9
import EditIcon from '@mui/icons-material/EditOutlined'
10
 
11
const StyledSpinnerContainer = styled.div`
12
  position: absolute;
13
  left: 0;
14
  top: 0;
15
  width: 100%;
16
  height: 100%;
17
  background: rgba(255, 255, 255, 0.4);
18
  display: flex;
19
  justify-content: center;
20
  align-items: center;
21
  z-index: 300;
22
`;
23
 
24
const Website = (props) => {
25
  // props destructuring
26
  const { groupId, website, addNotification } = props;
27
  // react hook form
28
  const {
29
    register,
30
    errors,
31
    handleSubmit,
32
    setValue,
33
    clearErrors,
34
    getValues,
35
    setError,
36
  } = useForm();
37
 
38
  // states
39
  const [isModalOpen, setIsModalOpen] = useState(false);
40
  const [loading, setLoading] = useState(false);
41
  const [settedWebsite, setSettedWebsite] = useState(website);
42
 
43
  const handleModalOpen = (event) => {
44
    event && event.preventDefault();
45
    setIsModalOpen(!isModalOpen);
46
  };
47
 
48
  const onSubmitHandler = async (data) => {
49
    setLoading(true);
50
    const formData = new FormData();
51
    Object.entries(data).map(([key, value]) => {
52
      formData.append(key, value);
53
    });
54
    await axios
55
      .post(`/group/my-groups/website/${groupId}`, formData)
56
      .then((response) => {
57
        const resData = response.data;
58
        (resData);
59
        if (resData.success) {
60
          setSettedWebsite(resData.data);
61
          handleModalOpen();
62
        } else {
63
          const resError = resData.data;
64
          if (resError.constructor.name === "Object") {
65
            Object.entries(resError).map(([key, value]) => {
66
              if (key in getValues()) {
67
                setError(key, {
68
                  type: "manual",
69
                  message: Array.isArray(value) ? value[0] : value,
70
                });
71
              }
72
            });
73
          } else {
74
            addNotification({
75
              style: "danger",
76
              msg: resError,
77
            });
78
          }
79
        }
80
      });
81
    setLoading(false);
82
  };
83
  return (
84
    <React.Fragment>
85
      <div className="experience__user-card">
86
        <div className="card__options-container">
87
          <h4>
88
            Página web
89
          </h4>
90
          <button className='button-icon' onClick={handleModalOpen}>
91
            <EditIcon />
92
          </button>
93
        </div>
94
        <a
95
          href={`http://${settedWebsite}`}
96
          target="_blank" rel="noreferrer"
97
        >
98
          <p>
99
            {settedWebsite}
100
          </p>
101
        </a>
102
      </div>
103
 
104
      {/* modal */}
105
      <Modal
106
        show={isModalOpen}
107
        onHide={handleModalOpen}
108
        style={{ overflowY: "scroll" }}
109
      >
110
        <Modal.Header closeButton>
111
          <Modal.Title>Página web</Modal.Title>
112
        </Modal.Header>
113
        <form onSubmit={handleSubmit(onSubmitHandler)}>
114
          <Modal.Body>
115
            <input
116
              type="text"
117
              name="website"
118
              id="website"
119
              ref={register}
120
              defaultValue={settedWebsite}
121
            />
122
            {errors.website && (
123
              <FormErrorFeedback>{errors.website.message}</FormErrorFeedback>
124
            )}
125
          </Modal.Body>
126
          <Modal.Footer>
127
            <Button type="submit">Enviar</Button>
128
            <Button variant="danger" onClick={handleModalOpen}>
129
              Cancelar
130
            </Button>
131
          </Modal.Footer>
132
        </form>
133
        {loading && (
134
          <StyledSpinnerContainer>
135
            <Spinner />
136
          </StyledSpinnerContainer>
137
        )}
138
      </Modal>
139
    </React.Fragment>
140
  );
141
};
142
 
143
export default Website;