Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 1519 | Rev 1521 | 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,
  Box,
  List,
  ListItem,
  ListItemAvatar,
  ListItemButton,
  ListItemSecondaryAction,
  ListItemText,
  Typography
} from '@mui/material'

import { axios } from '../../../utils'
import { addNotification } from '../../../redux/notification/notification.actions'
import useFetch from '../../../hooks/useFetch'

import WidgetWrapper from '../WidgetLayout'
import { ButtonPrimary, ButtonSecondary } from '@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]
  )

  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 (
    <WidgetWrapper>
      <Box
        display='flex'
        alignItems='baseline'
        justifyContent='space-between'
        padding={1}
      >
        <Typography variant='h3'>{`${labels.connect_with}:`}</Typography>

        {peopleYouMayKnow.length >= 4 && (
          <span onClick={() => setLookMore(!lookMore)}>
            {lookMore ? labels.view_less : labels.view_more}
          </span>
        )}
      </Box>

      {!users.length ? (
        <Typography variant='body1'>{labels.datatable_empty}</Typography>
      ) : (
        <List sx={{ width: '100%', maxHeight: 300, overflow: 'auto' }}>
          {users.map(
            ({ id, image, link_cancel, link_request, name, profile }) => (
              <ListItem key={id} disablePadding disableRipple>
                <ListItemButton disableRipple>
                  <ListItemAvatar>
                    <Avatar alt={`${name} image`} src={image} />
                  </ListItemAvatar>

                  <ListItemText primary={name} />

                  <ListItemSecondaryAction sx={{ position: 'relative' }}>
                    {link_request && (
                      <ButtonPrimary
                        label={labels.connect}
                        onClick={() => handleConnect(link_request)}
                      />
                    )}
                    {link_cancel && (
                      <ButtonSecondary
                        label={labels.cancel}
                        onClick={() => handleConnect(link_cancel)}
                      />
                    )}
                  </ListItemSecondaryAction>
                </ListItemButton>
              </ListItem>
            )
          )}
        </List>
      )}
    </WidgetWrapper>
  )
}

export default PeopleYouMayKnow