Proyectos de Subversion LeadersLinked - SPA

Rev

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