Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
6734 stevensc 1
import React, { useRef, useState } from 'react'
2
import { axios } from '../../utils'
3
import { useSelector } from 'react-redux'
4
import { addNotification } from '../../redux/notification/notification.actions'
5
import styled from 'styled-components'
6
 
7
import Spinner from '../UI/Spinner'
8
import ConfirmationBox from '../UI/ConfirmBox'
9
 
10
import styles from './ProfileItem.module.scss'
11
 
12
const StyledSpinnerContainer = styled.div`
13
  position: absolute;
14
  left: 0;
15
  top: 0;
16
  padding: 100px;
17
  background: rgba(255, 255, 255, 0.4);
18
  border-radius: 50%;
19
  display: flex;
20
  justify-content: center;
21
  align-items: center;
22
  z-index: 300;
23
`
24
const ProfileItem = ({
25
  image,
26
  name,
27
  email,
28
  network,
29
  status,
30
  fetchCallback,
31
  btnAcceptTitle = 'Ver perfil',
32
  btnCancelTitle = 'Borrar perfil',
33
  btnEditTitle = 'Editar perfil',
6739 stevensc 34
  btnLeaveTitle = 'Dejar',
6734 stevensc 35
  link_remove,
36
  link_view,
37
  link_edit,
38
  link_delete,
39
  link_cancel,
40
  link_block,
41
  link_reject,
42
  link_accept,
43
  link_inmail,
44
  link_request,
45
  link_unblock,
46
  link_unfollow,
47
  link_approve,
48
  link_leave,
49
  link_admin,
50
  link_impersonate,
51
  isTopData,
52
}) => {
53
  const [isShowConfirmation, setIsShowConfirmation] = useState(false)
54
  const [loading, setLoading] = useState(false)
55
  const confirmUrl = useRef('')
56
  const labels = useSelector(({ intl }) => intl.labels)
57
 
58
  const handleConfirm = (url = '') => {
59
    setIsShowConfirmation(!isShowConfirmation)
60
    confirmUrl.current = url
61
  }
62
 
63
  const getImpersonateUrl = async (url = '') => {
64
    try {
65
      const { data } = await axios.get(url)
66
      if (data.success) window.location.href = data.data
67
    } catch (error) {
68
      console.log('>>: error > ', error)
69
    }
70
  }
71
 
72
  const onConfirm = (url) => {
73
    setLoading(true)
74
    axios
75
      .post(url)
76
      .then(({ data }) => {
77
        if (!data.success) {
78
          const errorMsg =
79
            typeof data.data === 'string'
80
              ? data.data
81
              : 'Ha ocurrido un error, Por favor intente más tarde'
82
          addNotification({
83
            style: 'danger',
84
            msg: errorMsg,
85
          })
86
        }
87
        const msg = data.data
88
        addNotification({ style: 'success', msg })
89
        if (fetchCallback) fetchCallback()
90
      })
91
      .catch((error) => console.log('>>: error > ', error))
92
      .finally(() => {
93
        confirmUrl.current = ''
94
        setLoading(false)
95
      })
96
  }
97
 
98
  const handleUnfollow = async (link_unfollow) => {
99
    setLoading(true)
100
    await axios.post(link_unfollow).then(({ data }) => {
101
      if (data.success) fetchCallback()
102
 
103
      typeof data.data === 'string' &&
104
        addNotification({
105
          style: 'danger',
106
          msg: data.data,
107
        })
108
    })
109
    setLoading(false)
110
  }
111
 
112
  const getManageUrl = async () => {
113
    try {
114
      const { data } = await axios.get(link_admin)
115
      if (data.success) window.open(data.data, '_backend')
116
    } catch (error) {
117
      console.log('>>: error > ', error)
118
    }
119
  }
120
 
121
  const linksOptions = [
122
    {
123
      label: btnAcceptTitle || labels.accept,
124
      url: link_view,
125
      color: 'primary',
126
    },
127
    { label: btnEditTitle || labels.edit, url: link_edit, color: 'secondary' },
128
    { label: labels.approve, url: link_approve, color: 'tertiary' },
129
    { label: labels.accept, url: link_remove, color: 'tertiary' },
130
    { label: labels.remove, url: link_accept, color: 'secondary' },
131
    { label: labels.reject, url: link_reject, color: 'tertiary' },
132
    {
133
      label: btnCancelTitle || labels.cancel,
134
      url: link_delete,
135
      color: 'tertiary',
136
    },
137
    {
138
      label: labels.message || 'Mensaje',
139
      url: link_inmail,
140
      color: 'secondary',
141
    },
142
    { label: labels.administrate, url: link_admin, color: 'secondary' },
143
    { label: labels.unfollow, url: link_unfollow, color: 'tertiary' },
144
    { label: labels.block, url: link_block, color: 'tertiary' },
145
    { label: labels.unblock, url: link_unblock, color: 'tertiary' },
146
    { label: labels.connect, url: link_request, color: 'tertiary' },
147
    { label: labels.cancel, url: link_cancel, color: 'tertiary' },
6739 stevensc 148
    { label: labels.leave, url: btnLeaveTitle, color: 'tertiary' },
6734 stevensc 149
    { label: 'Personificar', url: link_impersonate, color: 'tertiary' },
150
  ]
151
 
152
  return (
153
    <div className={styles.profile_item}>
154
      <div className={styles.profile_item_header}>
155
        {image && <img src={image} alt="group image" />}
156
        <div className={styles.profile_item_header_info}>
157
          <h3>{name}</h3>
158
          {isTopData && email && <h4>{email}</h4>}
159
          {network && <h4>{network}</h4>}
160
          {status && <h4>{status}</h4>}
161
          {isTopData && (
162
            <ul>
163
              {link_view && (
164
                <li>
165
                  <a
166
                    href={link_view}
167
                    data-link={link_view}
168
                    className="btn btn-secondary ellipsis"
169
                  >
170
                    {btnAcceptTitle}
171
                  </a>
172
                </li>
173
              )}
174
              {link_inmail && (
175
                <li>
176
                  <a
177
                    href={link_inmail}
178
                    data-link={link_inmail}
179
                    className="btn btn-primary"
180
                  >
6735 stevensc 181
                    {labels.message}
6734 stevensc 182
                  </a>
183
                </li>
184
              )}
185
            </ul>
186
          )}
187
        </div>
188
      </div>
189
      <hr />
190
      <ul className="position-relative">
191
        {linksOptions.map((option) => {
192
          const breakOptions = [link_view, link_edit, link_inmail]
193
 
194
          if (!option.url) {
195
            return null
196
          }
197
 
6736 stevensc 198
          if (option.url === link_view && isTopData) {
199
            return null
200
          }
201
 
202
          if (option.url === link_inmail && isTopData) {
203
            return null
204
          }
205
 
6734 stevensc 206
          return (
207
            <li key={option.label}>
208
              <a
209
                href={option.url}
210
                title={option.label}
211
                className="position-relative"
212
                onClick={(e) => {
213
                  if (!breakOptions.includes(option.url)) {
214
                    e.preventDefault()
215
                    handleConfirm(option.url)
216
                  }
217
 
218
                  if (option.url === link_unfollow) {
219
                    e.preventDefault()
220
                    handleUnfollow(option.url)
221
                  }
222
                  if (option.url === link_admin) {
223
                    e.preventDefault()
224
                    getManageUrl()
225
                  }
226
 
227
                  if (option.url === link_impersonate) {
228
                    e.preventDefault()
229
                    getImpersonateUrl(option.url)
230
                  }
231
                }}
232
              >
233
                <button className={`btn btn-${option.color}`}>
234
                  {option.label}
235
                </button>
236
              </a>
237
            </li>
238
          )
239
        })}
240
        <ConfirmationBox
241
          show={isShowConfirmation}
242
          onClose={() => handleConfirm()}
243
          onAccept={() => onConfirm(confirmUrl.current)}
244
        />
245
      </ul>
246
      {loading && (
247
        <StyledSpinnerContainer>
248
          <Spinner />
249
        </StyledSpinnerContainer>
250
      )}
251
    </div>
252
  )
253
}
254
 
255
export default ProfileItem