Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 597 | Rev 599 | 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'
598 stevensc 3
import { Link, useHistory } from 'react-router-dom'
496 stevensc 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()
598 stevensc 58
  const history = useHistory()
5 stevensc 59
 
60
  const showConfirm = (url = '') => {
61
    setIsShowConfirmation(true)
62
    confirmUrl.current = url
63
  }
64
 
496 stevensc 65
  const closeConfirm = () => {
5 stevensc 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
    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}>
598 stevensc 182
            <h3>{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 (
598 stevensc 232
              <button
597 stevensc 233
                key={url}
598 stevensc 234
                onClick={() => {
597 stevensc 235
                  if (url === link_unfollow) {
598 stevensc 236
                    return handleUnfollow(url)
5 stevensc 237
                  }
238
 
597 stevensc 239
                  if (url === link_admin) {
598 stevensc 240
                    return getManageUrl()
5 stevensc 241
                  }
242
 
597 stevensc 243
                  if (url === link_impersonate) {
598 stevensc 244
                    return getImpersonateUrl(url)
5 stevensc 245
                  }
246
 
597 stevensc 247
                  if (!breakOptions.includes(url)) {
598 stevensc 248
                    return showConfirm(url)
249
                  } else {
250
                    history.push(url)
5 stevensc 251
                  }
252
                }}
253
              >
598 stevensc 254
                {label}
255
              </button>
597 stevensc 256
            )
257
          })}
258
        </StyledContainer.Actions>
259
        {loading && (
260
          <StyledSpinnerContainer>
261
            <Spinner />
262
          </StyledSpinnerContainer>
263
        )}
264
      </StyledContainer>
265
      <ConfirmModal
266
        show={isShowConfirmation}
267
        onClose={() => closeConfirm()}
268
        onAccept={() => onConfirm(confirmUrl.current)}
269
      />
270
    </>
5 stevensc 271
  )
272
}
273
 
274
export default ProfileItem