Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 7097 | Ir a la última revisión | | 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 =
7098 stevensc 87
            typeof data === 'string' ? data : labels.error_there_was_an_error
7097 stevensc 88
 
89
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
90
          return
6734 stevensc 91
        }
7097 stevensc 92
 
6734 stevensc 93
        if (fetchCallback) fetchCallback()
7097 stevensc 94
        dispatch(addNotification({ style: 'success', msg: data }))
6734 stevensc 95
      })
7098 stevensc 96
      .catch((err) => {
7097 stevensc 97
        dispatch(
98
          addNotification({
99
            style: 'error',
100
            msg: labels.error_there_was_an_error,
101
          })
102
        )
7098 stevensc 103
        throw new Error(err)
7097 stevensc 104
      })
6734 stevensc 105
      .finally(() => {
106
        confirmUrl.current = ''
107
        setLoading(false)
108
      })
109
  }
110
 
7097 stevensc 111
  const handleUnfollow = (link_unfollow) => {
6734 stevensc 112
    setLoading(true)
7097 stevensc 113
    axios
114
      .post(link_unfollow)
115
      .then((response) => {
116
        const { data, success } = response.data
6734 stevensc 117
 
7097 stevensc 118
        if (!success) {
119
          const errorMessage =
120
            typeof data === 'string' ? data : labels.error_there_was_an_error
121
 
122
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
123
          return
124
        }
125
 
7098 stevensc 126
        if (fetchCallback) fetchCallback()
127
        dispatch(addNotification({ style: 'success', msg: data }))
7097 stevensc 128
      })
129
      .finally(() => setLoading(false))
6734 stevensc 130
  }
131
 
132
  const getManageUrl = async () => {
133
    try {
134
      const { data } = await axios.get(link_admin)
135
      if (data.success) window.open(data.data, '_backend')
136
    } catch (error) {
137
      console.log('>>: error > ', error)
138
    }
139
  }
140
 
141
  const linksOptions = [
142
    {
143
      label: btnAcceptTitle || labels.accept,
144
      url: link_view,
145
      color: 'primary',
146
    },
147
    { label: btnEditTitle || labels.edit, url: link_edit, color: 'secondary' },
148
    { label: labels.approve, url: link_approve, color: 'tertiary' },
6885 stevensc 149
    { label: btnLeaveTitle, url: link_remove, color: 'tertiary' },
6741 stevensc 150
    { label: labels.accept, url: link_accept, color: 'secondary' },
6734 stevensc 151
    { label: labels.reject, url: link_reject, color: 'tertiary' },
152
    {
153
      label: btnCancelTitle || labels.cancel,
154
      url: link_delete,
155
      color: 'tertiary',
156
    },
157
    {
158
      label: labels.message || 'Mensaje',
159
      url: link_inmail,
160
      color: 'secondary',
161
    },
162
    { label: labels.administrate, url: link_admin, color: 'secondary' },
163
    { label: labels.unfollow, url: link_unfollow, color: 'tertiary' },
164
    { label: labels.block, url: link_block, color: 'tertiary' },
165
    { label: labels.unblock, url: link_unblock, color: 'tertiary' },
166
    { label: labels.connect, url: link_request, color: 'tertiary' },
167
    { label: labels.cancel, url: link_cancel, color: 'tertiary' },
6741 stevensc 168
    { label: btnLeaveTitle, url: link_leave, color: 'tertiary' },
6734 stevensc 169
    { label: 'Personificar', url: link_impersonate, color: 'tertiary' },
170
  ]
171
 
172
  return (
173
    <div className={styles.profile_item}>
174
      <div className={styles.profile_item_header}>
175
        {image && <img src={image} alt="group image" />}
176
        <div className={styles.profile_item_header_info}>
177
          <h3>{name}</h3>
178
          {isTopData && email && <h4>{email}</h4>}
179
          {network && <h4>{network}</h4>}
180
          {status && <h4>{status}</h4>}
181
          {isTopData && (
182
            <ul>
183
              {link_view && (
184
                <li>
7086 stevensc 185
                  <Link
186
                    to={link_view}
6734 stevensc 187
                    data-link={link_view}
188
                    className="btn btn-secondary ellipsis"
189
                  >
190
                    {btnAcceptTitle}
7086 stevensc 191
                  </Link>
6734 stevensc 192
                </li>
193
              )}
194
              {link_inmail && (
195
                <li>
7086 stevensc 196
                  <Link
197
                    to={link_inmail}
6734 stevensc 198
                    data-link={link_inmail}
199
                    className="btn btn-primary"
200
                  >
6735 stevensc 201
                    {labels.message}
7086 stevensc 202
                  </Link>
6734 stevensc 203
                </li>
204
              )}
205
            </ul>
206
          )}
207
        </div>
208
      </div>
209
      <hr />
210
      <ul className="position-relative">
211
        {linksOptions.map((option) => {
212
          const breakOptions = [link_view, link_edit, link_inmail]
213
 
214
          if (!option.url) {
215
            return null
216
          }
217
 
6736 stevensc 218
          if (option.url === link_view && isTopData) {
219
            return null
220
          }
221
 
222
          if (option.url === link_inmail && isTopData) {
223
            return null
224
          }
225
 
6734 stevensc 226
          return (
227
            <li key={option.label}>
6753 stevensc 228
              <Link
229
                to={option.url}
6734 stevensc 230
                title={option.label}
231
                className="position-relative"
232
                onClick={(e) => {
233
                  if (option.url === link_unfollow) {
234
                    e.preventDefault()
235
                    handleUnfollow(option.url)
6985 stevensc 236
                    return
6734 stevensc 237
                  }
6985 stevensc 238
 
6734 stevensc 239
                  if (option.url === link_admin) {
240
                    e.preventDefault()
241
                    getManageUrl()
6985 stevensc 242
                    return
6734 stevensc 243
                  }
244
 
245
                  if (option.url === link_impersonate) {
246
                    e.preventDefault()
247
                    getImpersonateUrl(option.url)
6986 stevensc 248
                    return
6734 stevensc 249
                  }
6986 stevensc 250
 
251
                  if (!breakOptions.includes(option.url)) {
252
                    e.preventDefault()
253
                    showConfirm(option.url)
254
                  }
6734 stevensc 255
                }}
256
              >
257
                <button className={`btn btn-${option.color}`}>
258
                  {option.label}
259
                </button>
6753 stevensc 260
              </Link>
6734 stevensc 261
            </li>
262
          )
263
        })}
264
        <ConfirmationBox
265
          show={isShowConfirmation}
6753 stevensc 266
          onClose={() => closeConfirm()}
6734 stevensc 267
          onAccept={() => onConfirm(confirmUrl.current)}
268
        />
269
      </ul>
270
      {loading && (
271
        <StyledSpinnerContainer>
272
          <Spinner />
273
        </StyledSpinnerContainer>
274
      )}
275
    </div>
276
  )
277
}
278
 
279
export default ProfileItem