Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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