Proyectos de Subversion LeadersLinked - SPA

Rev

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