Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 1 | Rev 3730 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
import React from "react";
2
import { useState } from "react";
3
import { useForm } from "react-hook-form";
4
import {axios} from "../../../../../utils";
5
import Spinner from "../../../../loading-spinner/Spinner";
6
import { profileTypes } from "../../../Profile.types";
7
import { Button, Modal } from "react-bootstrap";
8
import styled from "styled-components";
9
 
10
const StyledSpinnerContainer = styled.div`
11
  position: absolute;
12
  left: 0;
13
  top: 0;
14
  width: 100%;
15
  height: 100%;
16
  background: rgba(255, 255, 255, 0.4);
17
  display: flex;
18
  justify-content: center;
19
  align-items: center;
20
  z-index: 300;
21
`;
22
 
23
const SocialNetworks = (props) => {
24
  // props destructuring
25
  const {
26
    facebook,
27
    twitter,
28
    instagram,
29
    entityId,
30
    profileId,
31
    profileType,
32
    addNotification,
33
  } = props;
34
 
35
  // react hook form
36
  const { register, handleSubmit, setValue, getValues, setError } = useForm();
37
 
38
  // states
39
  const [isModalOpen, setIsModalOpen] = useState(false);
40
  const [modalLoading, setModalLoading] = useState(false);
41
  const [settedFacebook, setSettedtedFacebook] = useState(facebook);
42
  const [settedTwitter, setSettedTwitter] = useState(twitter);
43
  const [settedInstagram, setSettedInstagram] = useState(instagram);
44
 
45
  const handleModalOpen = (event) => {
46
    event && event.preventDefault();
47
    setIsModalOpen(!isModalOpen);
48
  };
49
 
50
  const handleEdit = (event) => {
51
    event.preventDefault();
52
    handleModalOpen();
53
    setValue("facebook", settedFacebook);
54
    setValue("twitter", settedTwitter);
55
    setValue("instagram", settedInstagram);
56
  };
57
 
58
  const onSubmitHandler = async (data) => {
59
    setModalLoading(true);
60
    let postPath = "";
61
    switch (profileType) {
62
      case profileTypes.USER:
63
        postPath = `/profile/my-profiles/social-network/${profileId}`;
64
        break;
65
      case profileTypes.COMPANY:
66
        postPath = `/my-company/${profileId}/profile/social-network`;
67
        break;
68
      default:
69
        break;
70
    }
71
    const formData = new FormData();
72
    Object.entries(data).map(([key, value]) => {
73
      formData.append(key, value);
74
    });
75
    await axios.post(postPath, formData).then((response) => {
76
      const resData = response.data;
77
       (resData);
78
      if (resData.success) {
79
        setSettedtedFacebook(resData.data.facebook);
80
        setSettedTwitter(resData.data.twitter);
81
        setSettedInstagram(resData.data.instagram);
82
        setIsModalOpen();
83
      } else {
84
        const resError = resData.data;
85
        if (resError.constructor.name === "Object") {
86
          Object.entries(resError).map(([key, value]) => {
87
            if (key in getValues()) {
88
              setError(key, {
89
                type: "manual",
90
                message: Array.isArray(value) ? value[0] : value,
91
              });
92
            }
93
          });
94
        } else {
95
          addNotification({
96
            style: "danger",
97
            msg: resError,
98
          });
99
        }
100
      }
101
    });
102
    setModalLoading(false);
103
  };
104
 
105
  return (
106
    <React.Fragment>
107
      <div className="user-social-network-ov">
108
        <h3>
109
          Redes Sociales
110
          <a
111
            href="#"
112
            title=""
113
            className="btn-social-network-edit"
114
            onClick={handleEdit}
115
          >
116
            <i className="fa fa-pencil"></i>
117
          </a>
118
        </h3>
119
      </div>
120
 
121
      <ul id="social-networks" className="social_links">
122
        {settedFacebook && (
123
          <li>
124
            <a href={settedFacebook} target="_blank" title="">
125
              <i className="fa fa-facebook-square"></i>{" "}
126
              {settedFacebook.length > 32
127
                ? `${settedFacebook.substr(0, 32)}...`
128
                : settedFacebook}
129
            </a>
130
          </li>
131
        )}
132
        {settedTwitter && (
133
          <li>
134
            <a href={settedTwitter} title="" target="_blank">
135
              <i className="fa fa-twitter"></i>
136
              {settedTwitter.length > 32
137
                ? `${settedTwitter.substr(0, 32)}...`
138
                : settedTwitter}
139
            </a>
140
          </li>
141
        )}
142
        {settedInstagram && (
143
          <li>
144
            <a href={settedInstagram} target="_blank" title="">
145
              <i className="fa fa-instagram"></i>{" "}
146
              {settedInstagram.length > 32
147
                ? `${settedInstagram.substr(0, 32)}...`
148
                : settedInstagram}
149
            </a>
150
          </li>
151
        )}
152
      </ul>
153
 
154
      {/* modal */}
155
      <Modal
156
        show={isModalOpen}
157
        onHide={handleModalOpen}
158
        style={{ overflowY: "scroll" }}
159
      >
160
        <Modal.Header closeButton>
161
          <Modal.Title>Redes Sociales</Modal.Title>
162
        </Modal.Header>
163
        <form onSubmit={handleSubmit(onSubmitHandler)}>
164
          <Modal.Body>
165
            <div className="form-group">
166
              <input
167
                type="text"
168
                name="facebook"
169
                placeholder="Perfil de Facebook"
170
                ref={register}
171
                defaultValue={settedFacebook}
172
              />
173
            </div>
174
            <div className="form-group">
175
              <input
176
                type="text"
177
                name="twitter"
178
                placeholder="Perfil de Twitter"
179
                ref={register}
180
                defaultValue={settedTwitter}
181
              />
182
            </div>
183
            <div className="form-group">
184
              <input
185
                type="text"
186
                name="instagram"
187
                placeholder="Perfil de Instagram"
188
                ref={register}
189
                defaultValue={settedInstagram}
190
              />
191
            </div>
192
          </Modal.Body>
193
          <Modal.Footer>
194
            <Button type="submit">Enviar</Button>
195
            <Button variant="danger" onClick={handleModalOpen}>
1010 steven 196
              Cancelar
1 www 197
            </Button>
198
          </Modal.Footer>
199
        </form>
200
        {modalLoading && (
201
          <StyledSpinnerContainer>
202
            <Spinner />
203
          </StyledSpinnerContainer>
204
        )}
205
      </Modal>
206
    </React.Fragment>
207
  );
208
};
209
 
210
export default SocialNetworks;