Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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