Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3677 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3719 stevensc 1
import React, { useRef, useState } from 'react';
2
import { axios } from '../../utils';
3
import { Avatar } from '@mui/material';
4
import { useNavigate } from 'react-router-dom';
5
import { addNotification } from '@store/notification/notification.actions';
6
import { useDispatch, useSelector } from 'react-redux';
7
import styled from 'styled-components';
8
 
9
import Spinner from '../UI/Spinner';
10
import WidgetWrapper from '../widgets/WidgetLayout';
11
import ConfirmModal from '../modals/ConfirmModal';
12
import Button from '../UI/buttons/Buttons';
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;
22
  z-index: 50;
23
`;
24
 
25
const StyledItemContainer = styled(WidgetWrapper)`
26
  display: flex;
27
  flex-direction: column;
28
  justify-content: center;
29
  height: fit-content;
30
  border-radius: 5px;
31
`;
32
 
33
const StyledHeader = styled.div`
34
  align-items: center;
35
  display: flex;
36
  gap: 0.5rem;
37
  justify-content: flex-start;
38
  position: relative;
39
  padding: 0.5rem;
40
`;
41
 
42
const StyledContent = styled.div`
43
  display: flex;
44
  flex-direction: column;
45
  text-align: center;
46
  ul {
47
    align-items: center;
48
    display: flex;
49
    justify-content: center;
50
    gap: 0.5rem;
51
  }
52
`;
53
 
54
const Actions = styled.div`
55
  display: flex;
56
  flex-wrap: wrap;
57
  justify-content: space-around;
58
  border-top: 1px solid rgb(211, 211, 211);
59
  padding: 0.5rem;
60
  gap: 0.5rem;
61
`;
62
 
63
const StyledAvatar = styled(Avatar)`
64
  height: 60px !important;
65
  width: 60px !important;
66
`;
67
 
68
const ProfileItem = ({
69
  image,
70
  name,
71
  email,
72
  network,
73
  status,
74
  fetchCallback,
75
  link_remove,
76
  link_view,
77
  link_edit,
78
  link_delete,
79
  link_cancel,
80
  link_block,
81
  link_reject,
82
  link_accept,
83
  link_inmail,
84
  link_request,
85
  link_unblock,
86
  link_unfollow,
87
  link_approve,
88
  link_leave,
89
  link_admin,
90
  link_impersonate,
91
  btnAcceptTitle,
92
  btnCancelTitle,
93
  btnEditTitle,
94
  btnLeaveTitle,
95
  btnRemoveLabel,
96
  isTopData
97
}) => {
98
  const [isShowConfirmation, setIsShowConfirmation] = useState(false);
99
  const [loading, setLoading] = useState(false);
100
  const confirmUrl = useRef('');
101
  const labels = useSelector(({ intl }) => intl.labels);
102
  const dispatch = useDispatch();
103
  const navigate = useNavigate();
104
 
105
  const showConfirm = (url = '') => {
106
    setIsShowConfirmation(true);
107
    confirmUrl.current = url;
108
  };
109
 
110
  const closeConfirm = () => {
111
    setIsShowConfirmation(false);
112
    confirmUrl.current = '';
113
  };
114
 
115
  const getImpersonateUrl = async (url = '') => {
116
    try {
117
      const response = await axios.get(url);
118
      const { data, success } = response.data;
119
 
120
      if (success) {
121
        window.location.href = data;
122
      }
123
    } catch (error) {
124
      console.log('>>: error > ', error);
125
    }
126
  };
127
 
128
  const onConfirm = (url) => {
129
    setLoading(true);
130
    axios
131
      .post(url)
132
      .then((response) => {
133
        const { success, data } = response.data;
134
 
135
        if (!success) {
136
          const errorMessage =
137
            typeof data === 'string'
138
              ? data
139
              : 'Error interno. Por favor, inténtelo de nuevo más tarde.';
140
 
141
          dispatch(addNotification({ style: 'danger', msg: errorMessage }));
142
          return;
143
        }
144
 
145
        if (fetchCallback) fetchCallback();
146
        dispatch(addNotification({ style: 'success', msg: data }));
147
      })
148
      .catch(() => {
149
        dispatch(
150
          addNotification({
151
            style: 'error',
152
            msg: 'Error interno. Por favor, inténtelo de nuevo más tarde.'
153
          })
154
        );
155
      })
156
      .finally(() => {
157
        confirmUrl.current = '';
158
        setLoading(false);
159
      });
160
  };
161
 
162
  const handleUnfollow = (link_unfollow) => {
163
    setLoading(true);
164
    axios
165
      .post(link_unfollow)
166
      .then((response) => {
167
        const { data, success } = response.data;
168
 
169
        if (!success) {
170
          const errorMessage =
171
            typeof data === 'string'
172
              ? data
173
              : 'Error interno. Por favor, inténtelo de nuevo más tarde.';
174
 
175
          dispatch(addNotification({ style: 'danger', msg: errorMessage }));
176
          return;
177
        }
178
 
179
        if (fetchCallback) fetchCallback();
180
        dispatch(addNotification({ style: 'success', msg: data }));
181
      })
182
      .finally(() => setLoading(false));
183
  };
184
 
185
  const getManageUrl = () => {
186
    axios
187
      .get(link_admin)
188
      .then((response) => {
189
        const { data, success } = response.data;
190
 
191
        if (!success) {
192
          throw new Error(data);
193
        }
194
 
195
        setTimeout(() => {
196
          window.open(data, '_backend');
197
        }, 0);
198
      })
199
      .catch((error) => {
200
        console.log('>>: error > ', error);
201
      });
202
  };
203
 
204
  const navigateTo = (url) => {
205
    navigate(url);
206
  };
207
 
208
  return (
209
    <>
210
      <StyledItemContainer>
211
        <StyledHeader>
212
          {image && <StyledAvatar src={image} alt={`${name} image`} />}
213
          <StyledContent>
214
            <h2>{name}</h2>
215
            {isTopData && email && <h4>{email}</h4>}
216
            {network && <span>{network}</span>}
217
            {status && <span>{status}</span>}
218
            {isTopData && (
219
              <ul>
220
                {link_view && (
221
                  <li>
222
                    <Button color='primary' onClick={() => navigateTo(link_view)}>
223
                      {btnAcceptTitle || labels.view_profile}
224
                    </Button>
225
                  </li>
226
                )}
227
                {link_inmail && (
228
                  <li>
229
                    <Button color='secondary' onClick={() => navigateTo(link_inmail)}>
230
                      {labels.message}
231
                    </Button>
232
                  </li>
233
                )}
234
              </ul>
235
            )}
236
          </StyledContent>
237
        </StyledHeader>
238
        <Actions>
239
          {link_view && !isTopData ? (
240
            <Button color='primary' onClick={() => navigateTo(link_view)}>
241
              {btnAcceptTitle || labels.view_profile}
242
            </Button>
243
          ) : null}
244
 
245
          {link_edit ? (
246
            <Button onClick={() => navigateTo(link_edit)} color='secondary'>
247
              {btnEditTitle || labels.edit}
248
            </Button>
249
          ) : null}
250
 
251
          {link_accept ? (
252
            <Button onClick={() => onConfirm(link_accept)} color='secondary'>
253
              {labels.accept}
254
            </Button>
255
          ) : null}
256
 
257
          {link_inmail && !isTopData ? (
258
            <Button color='secondary' onClick={() => navigateTo(link_inmail)}>
259
              {labels.message}
260
            </Button>
261
          ) : null}
262
 
263
          {link_admin ? (
264
            <Button color='secondary' onClick={() => getManageUrl(link_admin)}>
265
              {labels.administrate}
266
            </Button>
267
          ) : null}
268
 
269
          {link_approve ? (
270
            <Button color='secondary' onClick={() => onConfirm(link_approve)}>
271
              {labels.approve}
272
            </Button>
273
          ) : null}
274
 
275
          {link_unblock ? (
276
            <Button color='secondary' onClick={() => onConfirm(link_unblock)}>
277
              {labels.unblock}
278
            </Button>
279
          ) : null}
280
 
281
          {link_request ? (
282
            <Button color='secondary' onClick={() => onConfirm(link_request)}>
283
              {labels.connect}
284
            </Button>
285
          ) : null}
286
 
287
          {link_impersonate ? (
288
            <Button color='secondary' onClick={() => getImpersonateUrl(link_impersonate)}>
289
              Personificar
290
            </Button>
291
          ) : null}
292
 
293
          {link_remove ? (
294
            <Button color='info' onClick={() => showConfirm(link_remove)}>
295
              {btnRemoveLabel}
296
            </Button>
297
          ) : null}
298
 
299
          {link_reject ? (
300
            <Button color='info' onClick={() => showConfirm(link_reject)}>
301
              {labels.reject}
302
            </Button>
303
          ) : null}
304
 
305
          {link_delete ? (
306
            <Button color='info' onClick={() => showConfirm(link_delete)}>
307
              {btnCancelTitle || labels.cancel}
308
            </Button>
309
          ) : null}
310
 
311
          {link_unfollow ? (
312
            <Button color='info' onClick={() => handleUnfollow(link_unfollow)}>
313
              {labels.unfollow}
314
            </Button>
315
          ) : null}
316
 
317
          {link_block ? (
318
            <Button color='info' onClick={() => showConfirm(link_block)}>
319
              {labels.block}
320
            </Button>
321
          ) : null}
322
 
323
          {link_cancel ? (
324
            <Button color='info' onClick={() => showConfirm(link_cancel)}>
325
              {labels.cancel}
326
            </Button>
327
          ) : null}
328
 
329
          {link_leave ? (
330
            <Button color='info' onClick={() => showConfirm(link_leave)}>
331
              {btnLeaveTitle || labels.group_leave}
332
            </Button>
333
          ) : null}
334
        </Actions>
335
        {loading && (
336
          <StyledSpinnerContainer>
337
            <Spinner />
338
          </StyledSpinnerContainer>
339
        )}
340
      </StyledItemContainer>
341
      <ConfirmModal
342
        show={isShowConfirmation}
343
        onClose={() => closeConfirm()}
344
        onAccept={() => onConfirm(confirmUrl.current)}
345
      />
346
    </>
347
  );
348
};
349
 
350
export default ProfileItem;