Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
5 stevensc 1
import React, { useRef, useState } from 'react'
2
import { axios } from '../../utils'
3
import { useDispatch, 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
import { Link } from 'react-router-dom'
12
 
13
const StyledSpinnerContainer = styled.div`
14
  position: absolute;
15
  left: 0;
16
  top: 0;
17
  width: 100%;
18
  height: 100%;
19
  background: rgba(255, 255, 255, 0.4);
20
  place-items: center;
21
  z-index: 50;
446 andreina 22
   .h3_profile{
444 andreina 23
    color: #16283c;
24
    font-size: 1.1rem;
25
    font-weight: 600;
26
  }
5 stevensc 27
`
28
const ProfileItem = ({
29
  image,
30
  name,
31
  email,
32
  network,
33
  status,
34
  fetchCallback,
444 andreina 35
  btnAcceptTitle = 'Ver',
36
  btnCancelTitle = 'Borrar',
37
  btnEditTitle = 'Editar',
5 stevensc 38
  btnLeaveTitle = 'Dejar',
39
  link_remove,
40
  link_view,
41
  link_edit,
42
  link_delete,
43
  link_cancel,
44
  link_block,
45
  link_reject,
46
  link_accept,
47
  link_inmail,
48
  link_request,
49
  link_unblock,
50
  link_unfollow,
51
  link_approve,
52
  link_leave,
53
  link_admin,
54
  link_impersonate,
55
  isTopData,
56
}) => {
57
  const [isShowConfirmation, setIsShowConfirmation] = useState(false)
58
  const [loading, setLoading] = useState(false)
59
  const confirmUrl = useRef('')
60
  const labels = useSelector(({ intl }) => intl.labels)
61
  const dispatch = useDispatch()
62
 
63
  const showConfirm = (url = '') => {
64
    setIsShowConfirmation(true)
65
    confirmUrl.current = url
66
  }
67
 
68
  const closeConfirm = (url = '') => {
69
    setIsShowConfirmation(false)
70
    confirmUrl.current = ''
71
  }
72
 
73
  const getImpersonateUrl = async (url = '') => {
74
    try {
75
      const { data } = await axios.get(url)
76
      if (data.success) window.location.href = data.data
77
    } catch (error) {
78
      console.log('>>: error > ', error)
79
    }
80
  }
81
 
82
  const onConfirm = (url) => {
83
    setLoading(true)
84
 
85
    axios
86
      .post(url)
87
      .then((response) => {
88
        const { success, data } = response.data
89
 
90
        if (!success) {
91
          const errorMessage =
92
            typeof data === 'string'
93
              ? data
94
              : 'Error interno. Por favor, inténtelo de nuevo más tarde.'
95
 
96
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
97
          return
98
        }
99
 
100
        if (fetchCallback) fetchCallback()
101
        dispatch(addNotification({ style: 'success', msg: data }))
102
      })
103
      .catch((err) => {
104
        dispatch(
105
          addNotification({
106
            style: 'error',
107
            msg: 'Error interno. Por favor, inténtelo de nuevo más tarde.',
108
          })
109
        )
110
        throw new Error(err)
111
      })
112
      .finally(() => {
113
        confirmUrl.current = ''
114
        setLoading(false)
115
      })
116
  }
117
 
118
  const handleUnfollow = (link_unfollow) => {
119
    setLoading(true)
120
    axios
121
      .post(link_unfollow)
122
      .then((response) => {
123
        const { data, success } = response.data
124
 
125
        if (!success) {
126
          const errorMessage =
127
            typeof data === 'string'
128
              ? data
129
              : 'Error interno. Por favor, inténtelo de nuevo más tarde.'
130
 
131
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
132
          return
133
        }
134
 
135
        if (fetchCallback) fetchCallback()
136
        dispatch(addNotification({ style: 'success', msg: data }))
137
      })
138
      .finally(() => setLoading(false))
139
  }
140
 
141
  const getManageUrl = async () => {
142
    try {
143
      const { data } = await axios.get(link_admin)
144
      if (data.success) window.open(data.data, '_backend')
145
    } catch (error) {
146
      console.log('>>: error > ', error)
147
    }
148
  }
149
 
150
  const linksOptions = [
151
    {
152
      label: btnAcceptTitle || labels.accept,
153
      url: link_view,
154
      color: 'primary',
155
    },
156
    { label: btnEditTitle || labels.edit, url: link_edit, color: 'secondary' },
157
    { label: labels.approve, url: link_approve, color: 'tertiary' },
158
    { label: btnLeaveTitle, url: link_remove, color: 'tertiary' },
159
    { label: labels.accept, url: link_accept, color: 'secondary' },
160
    { label: labels.reject, url: link_reject, color: 'tertiary' },
161
    {
162
      label: btnCancelTitle || labels.cancel,
163
      url: link_delete,
164
      color: 'tertiary',
165
    },
166
    {
167
      label: labels.message || 'Mensaje',
168
      url: link_inmail,
169
      color: 'secondary',
170
    },
171
    { label: labels.administrate, url: link_admin, color: 'secondary' },
172
    { label: labels.unfollow, url: link_unfollow, color: 'tertiary' },
173
    { label: labels.block, url: link_block, color: 'tertiary' },
174
    { label: labels.unblock, url: link_unblock, color: 'tertiary' },
175
    { label: labels.connect, url: link_request, color: 'tertiary' },
176
    { label: labels.cancel, url: link_cancel, color: 'tertiary' },
177
    { label: btnLeaveTitle, url: link_leave, color: 'tertiary' },
178
    { label: 'Personificar', url: link_impersonate, color: 'tertiary' },
179
  ]
180
 
181
  return (
182
    <div className={styles.profile_item}>
183
      <div className={styles.profile_item_header}>
184
        {image && <img src={image} alt="group image" />}
185
        <div className={styles.profile_item_header_info}>
445 andreina 186
          <h3 className='h3_profile'>{name}</h3>
5 stevensc 187
          {isTopData && email && <h4>{email}</h4>}
188
          {network && <h4>{network}</h4>}
189
          {status && <h4>{status}</h4>}
190
          {isTopData && (
191
            <ul>
192
              {link_view && (
193
                <li>
194
                  <Link
195
                    to={link_view}
196
                    data-link={link_view}
197
                    className="btn btn-secondary ellipsis"
198
                  >
199
                    {btnAcceptTitle}
200
                  </Link>
201
                </li>
202
              )}
203
              {link_inmail && (
204
                <li>
205
                  <Link
206
                    to={link_inmail}
207
                    data-link={link_inmail}
208
                    className="btn btn-primary"
209
                  >
210
                    {labels.message}
211
                  </Link>
212
                </li>
213
              )}
214
            </ul>
215
          )}
216
        </div>
217
      </div>
447 andreina 218
 
5 stevensc 219
      <ul className="position-relative">
220
        {linksOptions.map((option) => {
221
          const breakOptions = [link_view, link_edit, link_inmail]
222
 
223
          if (!option.url) {
224
            return null
225
          }
226
 
227
          if (option.url === link_view && isTopData) {
228
            return null
229
          }
230
 
231
          if (option.url === link_inmail && isTopData) {
232
            return null
233
          }
234
 
235
          return (
236
            <li key={option.label}>
237
              <Link
238
                to={option.url}
239
                title={option.label}
240
                className="position-relative"
241
                onClick={(e) => {
242
                  if (option.url === link_unfollow) {
243
                    e.preventDefault()
244
                    handleUnfollow(option.url)
245
                    return
246
                  }
247
 
248
                  if (option.url === link_admin) {
249
                    e.preventDefault()
250
                    getManageUrl()
251
                    return
252
                  }
253
 
254
                  if (option.url === link_impersonate) {
255
                    e.preventDefault()
256
                    getImpersonateUrl(option.url)
257
                    return
258
                  }
259
 
260
                  if (!breakOptions.includes(option.url)) {
261
                    e.preventDefault()
262
                    showConfirm(option.url)
263
                  }
264
                }}
265
              >
444 andreina 266
                <button className={`btn `}>
5 stevensc 267
                  {option.label}
268
                </button>
269
              </Link>
270
            </li>
271
          )
272
        })}
273
        <ConfirmationBox
274
          show={isShowConfirmation}
275
          onClose={() => closeConfirm()}
276
          onAccept={() => onConfirm(confirmUrl.current)}
277
        />
278
      </ul>
279
      {loading && (
280
        <StyledSpinnerContainer>
281
          <Spinner />
282
        </StyledSpinnerContainer>
283
      )}
284
    </div>
285
  )
286
}
287
 
288
export default ProfileItem