Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3432 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
2781 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
23
      .get(`/account-settings/add-${network.toLowerCase()}`)
24
      .then((response) => {
25
        const resData = response.data
26
        if (!resData.success) {
27
          return addNotification({
28
            style: 'danger',
29
            msg: 'Ha ocurrido un error, Por favor intente más tarde'
30
          })
31
        }
32
        const OauthLink = resData.data
33
        window.location.replace(OauthLink)
34
      })
35
  }
36
 
37
  const removeSocialNetwork = (network) => {
38
    setLoading(true)
39
 
40
    axios
41
      .post(`/account-settings/add-${network.toLowerCase()}`)
42
      .then(({ data: responseData }) => {
43
        const { success, data } = responseData
44
 
45
        if (!success) {
46
          throw new Error(data)
47
        }
48
 
49
        addNotification({ style: 'success', msg: data })
50
      })
51
      .catch((err) => addNotification({ style: 'danger', msg: err.message }))
52
      .finally(() => setLoading(false))
53
  }
54
 
55
  const socialNetworks = [
56
    {
57
      name: 'Facebook',
58
      status: facebook
59
    },
60
    {
61
      name: 'Google',
62
      status: google
63
    },
64
    {
65
      name: 'Twitter',
66
      status: twitter
67
    }
68
  ]
69
 
70
  return (
71
    <Widget>
72
      <Widget.Header title='Cambiar Configuración de las redes sociales' />
73
 
74
      <Widget.Body>
75
        {loading && <Spinner />}
76
        {!!socialNetworks.length &&
77
          socialNetworks.map(({ name, status }) => (
78
            <div
79
              key={name}
80
              className='d-flex align-items-center justify-content-between'
81
            >
82
              <span>{name}</span>
83
 
84
              <div className='d-flex align-items-center'>
85
                <label htmlFor='is_current'>
86
                  {status ? 'Desconectar' : 'Conectar'}
87
                </label>
88
                <div className='form-group'>
89
                  <SwitchInput
90
                    isChecked={status}
91
                    setValue={(connect) =>
92
                      connect
93
                        ? removeSocialNetwork(name)
94
                        : addSocialNetwork(name)
95
                    }
96
                  />
97
                </div>
98
              </div>
99
            </div>
100
          ))}
101
      </Widget.Body>
102
    </Widget>
103
  )
104
}
105
 
106
const mapDispatchToProps = {
107
  addNotification: (notification) => addNotification(notification)
108
}
109
 
110
export default connect(null, mapDispatchToProps)(SocialNetworks)