Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
3719 stevensc 1
import React, { useMemo, useState } from 'react';
2
import { useDispatch, useSelector } from 'react-redux';
3
import { Avatar, List, ListItemAvatar, ListItemButton, ListItemText } from '@mui/material';
4
 
5
import { axios } from '@utils';
6
import { useFetch } from '@hooks';
7
import { addNotification } from '@store/notification/notification.actions';
8
 
9
import Widget from '@components/UI/Widget';
10
import Button from '@components/UI/buttons/Buttons';
11
 
12
const PeopleYouMayKnow = () => {
13
  const { data: peopleYouMayKnow, refetch } = useFetch('/helpers/people-you-may-know', []);
14
  const [lookMore, setLookMore] = useState(false);
15
  const labels = useSelector(({ intl }) => intl.labels);
16
  const dispatch = useDispatch();
17
 
18
  const users = useMemo(
19
    () => (lookMore ? peopleYouMayKnow : [...peopleYouMayKnow].slice(0, 3)),
20
    [peopleYouMayKnow, lookMore]
21
  );
22
 
23
  const handleConnect = (url) => {
24
    axios.post(url).then(({ data }) => {
25
      if (!data.success) {
26
        dispatch(
27
          addNotification({
28
            style: 'danger',
29
            msg: typeof data.data === 'string' ? data.data : 'Ha ocurrido un error'
30
          })
31
        );
32
        return;
33
      }
34
 
35
      dispatch(
36
        addNotification({
37
          style: 'success',
38
          msg: data.data
39
        })
40
      );
41
      refetch();
42
    });
43
  };
44
 
45
  if (!users.length) {
46
    return null;
47
  }
48
 
49
  return (
50
    <Widget>
51
      <Widget.Header
52
        title={labels.connect_with + ':'}
53
        renderAction={() =>
54
          peopleYouMayKnow.length > 4 && (
55
            <Button onClick={() => setLookMore(!lookMore)}>
56
              {lookMore ? labels.view_less : labels.view_more}
57
            </Button>
58
          )
59
        }
60
      />
61
 
62
      <Widget.Body>
63
        <List sx={{ width: '100%', maxHeight: 200, overflow: 'auto' }}>
64
          {users.map(({ id, image, link_cancel, link_request, name }) => (
65
            <ListItemButton key={id}>
66
              <ListItemAvatar>
67
                <Avatar alt={`${name} image`} src={image} />
68
              </ListItemAvatar>
69
 
70
              <ListItemText primary={name} />
71
 
72
              {link_request && (
73
                <Button color='primary' onClick={() => handleConnect(link_request)}>
74
                  {labels.connect}
75
                </Button>
76
              )}
77
              {link_cancel && (
78
                <Button color='secondary' onClick={() => handleConnect(link_cancel)}>
79
                  {labels.cancel}
80
                </Button>
81
              )}
82
            </ListItemButton>
83
          ))}
84
        </List>
85
      </Widget.Body>
86
    </Widget>
87
  );
88
};
89
 
90
export default PeopleYouMayKnow;