Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3432 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3719 stevensc 1
import React, { useState } from 'react';
2
import { connect } from 'react-redux';
3
 
4
import { axios } from '@utils';
5
import { addNotification } from '@app/redux/notification/notification.actions';
6
 
7
import Widget from '@components/UI/Widget';
8
import Spinner from '@components/UI/Spinner';
9
import SwitchInput from '@components/UI/SwitchInput';
10
 
11
const SocialNetworks = ({
12
  facebook,
13
  google,
14
  twitter,
15
  addNotification // Redux action
16
}) => {
17
  const [loading, setLoading] = useState(false);
18
 
19
  const addSocialNetwork = (network) => {
20
    setLoading(true);
21
 
22
    axios.get(`/account-settings/add-${network.toLowerCase()}`).then((response) => {
23
      const resData = response.data;
24
      if (!resData.success) {
25
        return addNotification({
26
          style: 'danger',
27
          msg: 'Ha ocurrido un error, Por favor intente más tarde'
28
        });
29
      }
30
      const OauthLink = resData.data;
31
      window.location.replace(OauthLink);
32
    });
33
  };
34
 
35
  const removeSocialNetwork = (network) => {
36
    setLoading(true);
37
 
38
    axios
39
      .post(`/account-settings/add-${network.toLowerCase()}`)
40
      .then((response) => {
41
        const { success, data } = response.data;
42
 
43
        if (!success) {
44
          throw new Error(data);
45
        }
46
 
47
        addNotification({ style: 'success', msg: data });
48
      })
49
      .catch((err) => addNotification({ style: 'danger', msg: err.message }))
50
      .finally(() => setLoading(false));
51
  };
52
 
53
  const socialNetworks = [
54
    {
55
      name: 'Facebook',
56
      status: facebook
57
    },
58
    {
59
      name: 'Google',
60
      status: google
61
    },
62
    {
63
      name: 'Twitter',
64
      status: twitter
65
    }
66
  ];
67
 
68
  return (
69
    <Widget>
70
      <Widget.Header title='Cambiar Configuración de las redes sociales' />
71
 
72
      <Widget.Body>
73
        {loading && <Spinner />}
74
        {!!socialNetworks.length &&
75
          socialNetworks.map(({ name, status }) => (
76
            <div key={name} className='d-flex align-items-center justify-content-between'>
77
              <span>{name}</span>
78
 
79
              <div className='d-flex align-items-center'>
80
                <label htmlFor='is_current'>{status ? 'Desconectar' : 'Conectar'}</label>
81
                <div className='form-group'>
82
                  <SwitchInput
83
                    isChecked={status}
84
                    setValue={(connect) =>
85
                      connect ? removeSocialNetwork(name) : addSocialNetwork(name)
86
                    }
87
                  />
88
                </div>
89
              </div>
90
            </div>
91
          ))}
92
      </Widget.Body>
93
    </Widget>
94
  );
95
};
96
 
97
const mapDispatchToProps = {
98
  addNotification: (notification) => addNotification(notification)
99
};
100
 
101
export default connect(null, mapDispatchToProps)(SocialNetworks);