Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
7110 stevensc 1
import React, { useEffect, useState } from 'react'
2
import { axios } from '../../../utils'
3
import { Link } from 'react-router-dom'
4
import { Avatar } from '@mui/material'
5
import { useSelector } from 'react-redux'
6
import ExpandLessIcon from '@mui/icons-material/ExpandLess'
7
import ExpandMoreIcon from '@mui/icons-material/ExpandMore'
8
 
9
import EmptySection from '../../UI/EmptySection'
10
 
11
const Groups = () => {
12
  const labels = useSelector(({ intl }) => intl.labels)
13
 
14
  return (
15
    <div className="sidebar__bottom">
16
      <Groups.Item
17
        url="/helpers/my-groups"
18
        title={labels.my_groups}
19
        display={true}
20
      />
21
      <Groups.Item
22
        url="/helpers/groups-suggestion"
23
        title={labels.suggest_groups}
24
      />
25
    </div>
26
  )
27
}
28
 
29
const Item = ({
30
  url = '/helpers/my-groups',
31
  title = 'Mis grupos',
32
  display = false,
33
}) => {
34
  const [widgetData, setWidgetData] = useState([])
35
  const [displayMenu, setDisplayMenu] = useState(display)
36
  const labels = useSelector(({ intl }) => intl.labels)
37
  const [lookMore, setLookMore] = useState(false)
38
 
39
  const getData = () => {
40
    axios.get(url).then((response) => {
41
      const { success, data } = response.data
42
 
43
      if (success) {
44
        setWidgetData(data.sort((a, b) => a.priority - b.priority).reverse())
45
      }
46
    })
47
  }
48
 
49
  const toggleMenu = () => {
50
    setDisplayMenu(!displayMenu)
51
  }
52
 
53
  const dataSlice = () => {
54
    let infoFollows = [...widgetData]
55
    if (!lookMore) {
56
      infoFollows = infoFollows.slice(0, 3)
57
    }
58
    return infoFollows
59
  }
60
 
61
  useEffect(() => {
62
    getData()
63
  }, [])
64
 
65
  return (
66
    <div className="sidebar__recent-item__container">
67
      <section className="sidebar__recent-item">
68
        <p>{title}</p>
69
        <button className="sidebar__recent-icon" onClick={toggleMenu}>
70
          {displayMenu ? <ExpandLessIcon /> : <ExpandMoreIcon />}
71
        </button>
72
      </section>
73
 
74
      <ul className={`helper__list ${displayMenu ? 'show' : 'hide'}`}>
75
        {widgetData.length ? (
76
          dataSlice().map(({ id, name, profile, image }) => (
77
            <li key={id}>
78
              <Link
79
                href={profile}
80
                className="helper__list-item"
81
                target="secondary"
82
              >
83
                <Avatar
84
                  src={image}
85
                  alt={`${name} group image`}
86
                  sx={{ width: '40px', height: '40px' }}
87
                />
88
                <span>{name}</span>
89
              </Link>
90
            </li>
91
          ))
92
        ) : (
93
          <EmptySection message={labels.datatable_empty} />
94
        )}
95
 
96
        {widgetData.length >= 3 && (
97
          <li
98
            className="helper__list-item justify-content-center cursor-pointer py-2"
99
            onClick={() => setLookMore(!lookMore)}
100
          >
101
            <span>{lookMore ? labels.view_less : labels.view_more}</span>
102
          </li>
103
        )}
104
      </ul>
105
    </div>
106
  )
107
}
108
 
109
Groups.Item = Item
110
 
111
export default Groups