Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 4111 | Rev 5104 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

import React, { useEffect, useState } from "react";
import { useDispatch } from "react-redux";
import { addNotification } from "../../../redux/notification/notification.actions";
import { axios } from "../../../utils";

const PeopleYouMayKnow = () => {

  const [peopleYouMayKnow, setPeopleYouMayKnow] = useState([]);
  const [lookMore, setLookMore] = useState(false);
  const dispatch = useDispatch()

  const handleConnect = (url) => {
    axios.post(url)
      .then(({ data }) => {
        if (!data.success) {
          return dispatch(addNotification({
            style: 'danger',
            msg: typeof data.data === 'string'
              ? data.data
              : 'Ha ocurrido un error'
          }))
        }

        dispatch(addNotification({
          style: 'success',
          msg: data.data
        }))
        return getSuggestion()
      })
  }

  const getSuggestion = async (url = `/helpers/people-you-may-know`) => {
    try {
      const { data: response } = await axios.get(url)
      if (response.success) setPeopleYouMayKnow(response.data);
    } catch (error) {
      console.log(error);
    }
  }

  useEffect(() => {
    getSuggestion()
  }, []);

  const dataSlice = () => {
    let infoFollows = [...peopleYouMayKnow]
    if (!lookMore) {
      infoFollows = infoFollows.slice(0, 3)
    }
    return infoFollows
  }

  return (
    <div className='peopleYouMayKnow'>
      <div className="sd-title d-flex align-items-center justify-content-between">
        <h3>Conecta con:</h3>
        {
          peopleYouMayKnow.length >= 4 &&
          <label onClick={() => setLookMore(!lookMore)}>
            {lookMore ? 'Ver menos' : 'Ver mas'}
          </label>
        }
      </div>
      <div className='suggest-list'>
        {peopleYouMayKnow.length
          ? dataSlice().map(({ id, image, link_cancel, link_request, name, profile }) =>
            <div className='user' key={id}>
              <div className="w-100 d-flex align-items-center" style={{ gap: '.5rem' }}>
                <a href={profile} target="_blank" rel="noreferrer">
                  <img src={image} alt={`${name} profile image`} />
                </a>
                <h4 className="break-ellipsis">{name}</h4>
              </div>
              {link_request &&
                <button
                  className="btn btn-primary"
                  onClick={() => handleConnect(link_request)}
                >
                  Conectar
                </button>
              }
              {link_cancel &&
                <button
                  className="btn btn-secondary"
                  onClick={() => handleConnect(link_cancel)}
                >
                  Cancelar
                </button>
              }
            </div>
          )
          : <div className="view-more">Sin sugerencias</div>
        }
      </div>
    </div >
  );
};

export default PeopleYouMayKnow;