Rev 2780 | Rev 3156 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
import React, { useMemo, useState } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import {
Avatar,
List,
ListItemAvatar,
ListItemButton,
ListItemText,
Typography
} from '@mui/material'
import { axios } from '@app/utils'
import { addNotification } from '@app/redux/notification/notification.actions'
import { useFetch } from '@hooks'
import Widget from '@app/components/UI/Widget'
import Button from '@app/components/UI/buttons/Buttons'
const PeopleYouMayKnow = () => {
const { data: peopleYouMayKnow, refetch } = useFetch(
'/helpers/people-you-may-know',
[]
)
const [lookMore, setLookMore] = useState(false)
const labels = useSelector(({ intl }) => intl.labels)
const dispatch = useDispatch()
const users = useMemo(
() => (lookMore ? peopleYouMayKnow : [...peopleYouMayKnow].slice(0, 3)),
[peopleYouMayKnow, lookMore]
)
const handleConnect = (url) => {
axios.post(url).then(({ data }) => {
if (!data.success) {
dispatch(
addNotification({
style: 'danger',
msg:
typeof data.data === 'string' ? data.data : 'Ha ocurrido un error'
})
)
return
}
dispatch(
addNotification({
style: 'success',
msg: data.data
})
)
refetch()
})
}
return (
<Widget>
<Widget.Header
title={labels.connect_with + ':'}
renderAction={() =>
peopleYouMayKnow.length > 4 && (
<Button onClick={() => setLookMore(!lookMore)}>
{lookMore ? labels.view_less : labels.view_more}
</Button>
)
}
/>
<Widget.Body>
{!users.length ? (
<Typography variant='body1' px={1}>
{labels.datatable_empty}
</Typography>
) : (
<List sx={{ width: '100%', maxHeight: 200, overflow: 'auto' }}>
{users.map(
({ id, image, link_cancel, link_request, name, profile }) => (
<ListItemButton key={id}>
<ListItemAvatar>
<Avatar alt={`${name} image`} src={image} />
</ListItemAvatar>
<ListItemText primary={name} />
{link_request && (
<Button
variant='primary'
onClick={() => handleConnect(link_request)}
>
{labels.connect}
</Button>
)}
{link_cancel && (
<Button
variant='secondary'
onClick={() => handleConnect(link_cancel)}
>
{labels.cancel}
</Button>
)}
</ListItemButton>
)
)}
</List>
)}
</Widget.Body>
</Widget>
)
}
export default PeopleYouMayKnow