Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 583 | Rev 598 | 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'
496 stevensc 3
import { Link } from 'react-router-dom'
4
import { addNotification } from 'store/notification/notification.actions'
5 stevensc 5
import { useDispatch, useSelector } from 'react-redux'
6
import styled from 'styled-components'
7
 
8
import Spinner from '../UI/Spinner'
597 stevensc 9
import StyledContainer from '../widgets/WidgetLayout'
10
import ConfirmModal from '../modals/ConfirmModal'
5 stevensc 11
 
12
import styles from './ProfileItem.module.scss'
13
 
14
const StyledSpinnerContainer = styled.div`
15
  position: absolute;
16
  left: 0;
17
  top: 0;
18
  width: 100%;
19
  height: 100%;
20
  background: rgba(255, 255, 255, 0.4);
21
  place-items: center;
496 stevensc 22
  z-index: 50;
5 stevensc 23
`
24
const ProfileItem = ({
25
  image,
26
  name,
27
  email,
28
  network,
29
  status,
30
  fetchCallback,
489 andreina 31
  btnAcceptTitle = ' Ver',
444 andreina 32
  btnCancelTitle = 'Borrar',
33
  btnEditTitle = 'Editar',
5 stevensc 34
  btnLeaveTitle = 'Dejar',
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
  const dispatch = useDispatch()
58
 
59
  const showConfirm = (url = '') => {
60
    setIsShowConfirmation(true)
61
    confirmUrl.current = url
62
  }
63
 
496 stevensc 64
  const closeConfirm = () => {
5 stevensc 65
    setIsShowConfirmation(false)
66
    confirmUrl.current = ''
67
  }
68
 
69
  const getImpersonateUrl = async (url = '') => {
70
    try {
71
      const { data } = await axios.get(url)
72
      if (data.success) window.location.href = data.data
73
    } catch (error) {
74
      console.log('>>: error > ', error)
75
    }
76
  }
77
 
78
  const onConfirm = (url) => {
79
    setLoading(true)
80
 
81
    axios
82
      .post(url)
83
      .then((response) => {
84
        const { success, data } = response.data
85
 
86
        if (!success) {
87
          const errorMessage =
88
            typeof data === 'string'
89
              ? data
90
              : 'Error interno. Por favor, inténtelo de nuevo más tarde.'
91
 
92
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
93
          return
94
        }
95
 
96
        if (fetchCallback) fetchCallback()
97
        dispatch(addNotification({ style: 'success', msg: data }))
98
      })
526 stevensc 99
      .catch(() => {
5 stevensc 100
        dispatch(
101
          addNotification({
102
            style: 'error',
103
            msg: 'Error interno. Por favor, inténtelo de nuevo más tarde.',
104
          })
105
        )
106
      })
107
      .finally(() => {
108
        confirmUrl.current = ''
109
        setLoading(false)
110
      })
111
  }
112
 
113
  const handleUnfollow = (link_unfollow) => {
114
    setLoading(true)
115
    axios
116
      .post(link_unfollow)
117
      .then((response) => {
118
        const { data, success } = response.data
119
 
120
        if (!success) {
121
          const errorMessage =
122
            typeof data === 'string'
123
              ? data
124
              : 'Error interno. Por favor, inténtelo de nuevo más tarde.'
125
 
126
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
127
          return
128
        }
129
 
130
        if (fetchCallback) fetchCallback()
131
        dispatch(addNotification({ style: 'success', msg: data }))
132
      })
133
      .finally(() => setLoading(false))
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' },
153
    { label: btnLeaveTitle, url: link_remove, color: 'tertiary' },
154
    { label: labels.accept, url: link_accept, color: 'secondary' },
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' },
172
    { label: btnLeaveTitle, url: link_leave, color: 'tertiary' },
173
    { label: 'Personificar', url: link_impersonate, color: 'tertiary' },
174
  ]
175
 
176
  return (
597 stevensc 177
    <>
178
      <StyledContainer>
179
        <div className={styles.profile_item_header}>
180
          {image && <img src={image} alt="group image" />}
181
          <div className={styles.profile_item_header_info}>
182
            <h3 className="titulo">{name}</h3>
496 stevensc 183
 
597 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 "
206
                    >
207
                      {labels.message}
208
                    </Link>
209
                  </li>
210
                )}
211
              </ul>
212
            )}
213
          </div>
5 stevensc 214
        </div>
597 stevensc 215
        <StyledContainer.Actions>
216
          {linksOptions.map(({ label, url }) => {
217
            const breakOptions = [link_view, link_edit, link_inmail]
5 stevensc 218
 
597 stevensc 219
            if (!url) {
220
              return null
221
            }
5 stevensc 222
 
597 stevensc 223
            if (url === link_view && isTopData) {
224
              return null
225
            }
5 stevensc 226
 
597 stevensc 227
            if (url === link_inmail && isTopData) {
228
              return null
229
            }
5 stevensc 230
 
597 stevensc 231
            return (
5 stevensc 232
              <Link
597 stevensc 233
                key={url}
234
                to={url}
235
                title={label}
5 stevensc 236
                className="position-relative"
237
                onClick={(e) => {
597 stevensc 238
                  if (url === link_unfollow) {
5 stevensc 239
                    e.preventDefault()
597 stevensc 240
                    handleUnfollow(url)
5 stevensc 241
                    return
242
                  }
243
 
597 stevensc 244
                  if (url === link_admin) {
5 stevensc 245
                    e.preventDefault()
246
                    getManageUrl()
247
                    return
248
                  }
249
 
597 stevensc 250
                  if (url === link_impersonate) {
5 stevensc 251
                    e.preventDefault()
597 stevensc 252
                    getImpersonateUrl(url)
5 stevensc 253
                    return
254
                  }
255
 
597 stevensc 256
                  if (!breakOptions.includes(url)) {
5 stevensc 257
                    e.preventDefault()
597 stevensc 258
                    showConfirm(url)
5 stevensc 259
                  }
260
                }}
261
              >
597 stevensc 262
                <button className={`btn `}>{label}</button>
5 stevensc 263
              </Link>
597 stevensc 264
            )
265
          })}
266
        </StyledContainer.Actions>
267
        {loading && (
268
          <StyledSpinnerContainer>
269
            <Spinner />
270
          </StyledSpinnerContainer>
271
        )}
272
      </StyledContainer>
273
      <ConfirmModal
274
        show={isShowConfirmation}
275
        onClose={() => closeConfirm()}
276
        onAccept={() => onConfirm(confirmUrl.current)}
277
      />
278
    </>
5 stevensc 279
  )
280
}
281
 
282
export default ProfileItem