Proyectos de Subversion LeadersLinked - SPA

Rev

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