| 3719 |
stevensc |
1 |
import React, { useMemo, useState } from 'react';
|
|
|
2 |
import { Link } from 'react-router-dom';
|
|
|
3 |
import { useSelector } from 'react-redux';
|
|
|
4 |
import {
|
|
|
5 |
Avatar,
|
|
|
6 |
Button,
|
|
|
7 |
List,
|
|
|
8 |
ListItem,
|
|
|
9 |
ListItemAvatar,
|
|
|
10 |
ListItemButton,
|
|
|
11 |
ListItemText
|
|
|
12 |
} from '@mui/material';
|
|
|
13 |
|
|
|
14 |
import { useFetch } from '@hooks';
|
|
|
15 |
import colors from '@styles/config/colors';
|
|
|
16 |
|
|
|
17 |
import Widget from '@components/UI/Widget';
|
|
|
18 |
import Spinner from '@components/UI/Spinner';
|
|
|
19 |
import EmptySection from '@components/UI/EmptySection';
|
|
|
20 |
|
|
|
21 |
const SuggestWidget = ({ title, url }) => {
|
|
|
22 |
const { data, isLoading } = useFetch(url, []);
|
|
|
23 |
const [lookMore, setLookMore] = useState(false);
|
|
|
24 |
const labels = useSelector(({ intl }) => intl.labels);
|
|
|
25 |
|
|
|
26 |
const items = useMemo(() => (lookMore ? data : [...data].slice(0, 3)), [lookMore, data]);
|
|
|
27 |
|
|
|
28 |
return (
|
|
|
29 |
<Widget>
|
|
|
30 |
<Widget.Header
|
|
|
31 |
title={title}
|
|
|
32 |
renderAction={() => {
|
|
|
33 |
if (data.length <= 3) return null;
|
|
|
34 |
|
|
|
35 |
return (
|
|
|
36 |
<Button onClick={() => setLookMore(!lookMore)}>
|
|
|
37 |
{lookMore ? labels.view_less : labels.view_more}
|
|
|
38 |
</Button>
|
|
|
39 |
);
|
|
|
40 |
}}
|
|
|
41 |
/>
|
|
|
42 |
<Widget.Body styles={data.length > 0 && { padding: '0 !important' }}>
|
|
|
43 |
{isLoading && <Spinner />}
|
|
|
44 |
|
|
|
45 |
{!isLoading && data.length === 0 ? (
|
|
|
46 |
<EmptySection message={labels?.datatable_empty} />
|
|
|
47 |
) : (
|
|
|
48 |
<List sx={{ maxHeight: '225px', overflow: 'auto' }}>
|
|
|
49 |
{items.map(({ name, profile, image, id }) => (
|
|
|
50 |
<ListItem key={id} sx={{ borderTop: `1px solid ${colors.border.primary}` }}>
|
|
|
51 |
<ListItemButton LinkComponent={Link} to={profile} disableRipple>
|
|
|
52 |
<ListItemAvatar>
|
|
|
53 |
<Avatar src={image} alt={`${name} profile avatar`} />
|
|
|
54 |
</ListItemAvatar>
|
|
|
55 |
|
|
|
56 |
<ListItemText primary={name} />
|
|
|
57 |
</ListItemButton>
|
|
|
58 |
</ListItem>
|
|
|
59 |
))}
|
|
|
60 |
</List>
|
|
|
61 |
)}
|
|
|
62 |
</Widget.Body>
|
|
|
63 |
</Widget>
|
|
|
64 |
);
|
|
|
65 |
};
|
|
|
66 |
|
|
|
67 |
export default SuggestWidget;
|