Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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