Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

import React from 'react'
import { useState } from 'react';
import { useDispatch } from 'react-redux';
import { addNotification } from '../../../../redux/notification/notification.actions';

const LikeButton = ({ likeUrl }) => {

    const [isLike, setIsLike] = useState(false);
    const [likesState, setLikesState] = useState(0);
    const dispatch = useDispatch()

    const handleClick = (url) => {
        if (url) {
            axios.post(url)
                .then((res) => {
                    const { success, data } = res.data;
                    if (!success) {
                        setIsLike(!isLike);
                        dispatch(addNotification({
                            style: "danger",
                            msg: data,
                        }));
                    } else {
                        setLikesState(data.likes)
                        setIsLike(!isLike);
                    }
                });
        }
        else {
            setIsLike(!isLike)
        }
    }

    return (
        <button
            type="button"
            className={isLike ? "btn-unlike" : "btn-like"}
            onClick={() => handleClick(likeUrl)}
        >
            <i className={isLike ? "fas fa-heart" : "far fa-heart"}></i>
            {likesState}
        </button>
    )
}

export default LikeButton