Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
2650 stevensc 1
import { axios } from "../../../utils";
1 www 2
import React, { useEffect, useState } from "react";
3
import { connect } from "react-redux";
4
import { addNotification } from "../../../redux/notification/notification.actions";
5
import Spinner from "../../../shared/loading-spinner/Spinner";
6
 
2650 stevensc 7
const StyledSwitch = styled.label`
8
  position: relative;
9
  display: inline-block;
10
  width: 60px;
11
  height: 34px;
12
  input {
13
    opacity: 0;
14
    width: 0;
15
    height: 0;
16
  }
17
  .slider {
18
    position: absolute;
19
    cursor: pointer;
20
    top: 0;
21
    left: 0;
22
    right: 0;
23
    bottom: 0;
24
    background-color: #ccc;
25
    -webkit-transition: 0.4s;
26
    transition: 0.4s;
27
  }
28
  .slider:before {
29
    position: absolute;
30
    content: "";
31
    height: 26px;
32
    width: 26px;
33
    left: 4px;
34
    bottom: 4px;
35
    background-color: white;
36
    -webkit-transition: 0.4s;
37
    transition: 0.4s;
38
  }
39
  input:checked + .slider {
40
    background-color: #2196f3;
41
  }
42
  input:focus + .slider {
43
    box-shadow: 0 0 1px #2196f3;
44
  }
45
  input:checked + .slider:before {
46
    -webkit-transform: translateX(26px);
47
    -ms-transform: translateX(26px);
48
    transform: translateX(26px);
49
  }
50
  .slider.round {
51
    border-radius: 34px;
52
  }
53
 
54
  .slider.round:before {
55
    border-radius: 50%;
56
  }
57
`;
58
 
1 www 59
const SocialNetworks = (props) => {
60
  // redux destructuring
61
  const { addNotification } = props;
62
 
63
  // props destructuring
64
  const { facebook, google, twitter, socialNetworkRoutes } = props;
65
 
66
  // states
67
  const [loading, setLoading] = useState(false);
68
  const [facebookConnected, setFacebookConnected] = useState(Boolean(facebook));
69
  const [twitterConnected, setTwitterConnected] = useState(Boolean(twitter));
70
  const [googleConnected, setGoogleConnected] = useState(Boolean(google));
71
 
72
  const handleAddSocialNetwork = (event, getOauthLink) => {
73
    event.preventDefault();
74
    setLoading(true);
75
    axios.get(getOauthLink).then((response) => {
76
      const resData = response.data;
77
      if (!resData.success) {
78
        return addNotification({
79
          style: "danger",
80
          msg: "Ha ocurrido un error, Por favor intente más tarde",
81
        });
82
      }
83
      const OauthLink = resData.data;
84
      window.location.replace(OauthLink);
85
    });
86
  };
87
 
88
  const handleRemoveSocialNetwork = (event, removeRoute) => {
89
    const network = event.currentTarget.dataset.network;
90
    event.preventDefault();
91
    setLoading(true);
92
    axios.post(removeRoute).then((response) => {
93
      const resData = response.data;
94
      if (!resData.success) {
95
        return addNotification({
96
          style: "danger",
97
          msg: resData.data,
98
        });
99
      }
100
      if (network === "facebook") setFacebookConnected(false);
101
      if (network === "twitter") setTwitterConnected(false);
102
      if (network === "google") setGoogleConnected(false);
103
      addNotification({
104
        style: "success",
105
        msg: resData.data,
106
      });
107
    });
108
    setLoading(false);
109
  };
110
 
111
  return (
2650 stevensc 112
    <div className="settings-container">
113
      <h2>Cambiar Configuración de las redes sociales</h2>
114
      <div className="acc-setting_content">
115
        <div className="d-flex align-items-center justify-content-between">
116
          <span>
117
            Facebook
118
          </span>
119
          <div className="input-c p-0">
120
            <label htmlFor="is_current">{facebookConnected ? 'Desconectar' : 'Conectar'}</label>
121
            <div className="form-group">
122
              <StyledSwitch className="switch">
123
                <input
124
                  type="checkbox"
125
                  name="is_current"
1 www 126
                  data-network="facebook"
127
                  onClick={(e) =>
2650 stevensc 128
                    facebookConnected
129
                      ? handleRemoveSocialNetwork(e, socialNetworkRoutes.facebook.remove)
130
                      : handleAddSocialNetwork(e, socialNetworkRoutes.facebook.add)
1 www 131
                  }
2650 stevensc 132
                />
133
                <span className="slider round"></span>
134
              </StyledSwitch>
135
            </div>
136
          </div>
137
        </div>
138
        <div className="d-flex align-items-center justify-content-between">
139
          <span>
140
            Twitter
141
          </span>
142
          <div className="input-c p-0">
143
            <label htmlFor="is_current">{twitterConnected ? 'Desconectar' : 'Conectar'}</label>
144
            <div className="form-group">
145
              <StyledSwitch className="switch">
146
                <input
147
                  type="checkbox"
148
                  name="is_current"
1 www 149
                  data-network="twitter"
150
                  onClick={(e) =>
2650 stevensc 151
                    twitterConnected
152
                      ? handleRemoveSocialNetwork(e, socialNetworkRoutes.twitter.remove)
153
                      : handleAddSocialNetwork(e, socialNetworkRoutes.twitter.add)
1 www 154
                  }
2650 stevensc 155
                />
156
                <span className="slider round"></span>
157
              </StyledSwitch>
158
            </div>
159
          </div>
160
        </div>
161
        <div className="d-flex align-items-center justify-content-between">
162
          <span>
163
            Google
164
          </span>
165
          <div className="input-c p-0">
166
            <label htmlFor="is_current">{googleConnected ? 'Desconectar' : 'Conectar'}</label>
167
            <div className="form-group">
168
              <StyledSwitch className="switch">
169
                <input
170
                  type="checkbox"
171
                  name="is_current"
1 www 172
                  data-network="google"
173
                  onClick={(e) =>
2650 stevensc 174
                    googleConnected
175
                      ? handleRemoveSocialNetwork(e, socialNetworkRoutes.google.remove)
176
                      : handleAddSocialNetwork(e, socialNetworkRoutes.google.add)
1 www 177
                  }
2650 stevensc 178
                />
179
                <span className="slider round"></span>
180
              </StyledSwitch>
181
            </div>
182
          </div>
183
        </div>
184
      </div>
185
      {
186
        loading &&
1 www 187
        <div className="spinner-container">
188
          <Spinner />
189
        </div>
2650 stevensc 190
      }
191
    </div >
1 www 192
  );
193
};
194
 
195
const mapDispatchToProps = {
196
  addNotification: (notification) => addNotification(notification),
197
};
198
 
199
export default connect(null, mapDispatchToProps)(SocialNetworks);