/
Percival
/
react_final_project
Обзор
Документация
Войти
/
Percival
/
react_final_project
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/features/toggle-like/ui/LikeButton.tsx
52 строки
1 KB
Rituparn
Added code
23 ноя 2025, 19:02
23 ноя 2025, 19:02
2aec7c7
Код
Авторство
О чём код?
import s from './LikeButton.module.css'; import LikeSvg from './../../../shared/assets/icons/like.svg'; // Fixed import path import classNames from 'classnames'; import { useAppSelector } from '../../../app/store/utils'; // Fixed import path import { userSelectors } from '../../../app/store/slices/user'; // Fixed import path import { useSetLikeProductMutation, useDeleteLikeProductMutation, IErrorResponse, } from '../../../app/store/api/productsApi'; // Fixed import path import { toast } from 'react-toastify'; type TLikeButtonProps = { product: Product; }; export const LikeButton = ({ product }: TLikeButtonProps) => { const accessToken = useAppSelector(userSelectors.getAccessToken); const user = useAppSelector(userSelectors.getUser); const [setLike] = useSetLikeProductMutation(); const [deleteLike] = useDeleteLikeProductMutation(); const isLike = product?.likes.some((l) => l.userId === user?.id); const toggleLike = async () => { if (!accessToken) { toast.warning('Вы не авторизованы'); return; } let response; if (isLike) { response = await deleteLike({ id: `${product.id}` }); } else { response = await setLike({ id: `${product.id}` }); } if (response.error) { const error = response.error as IErrorResponse; toast.error(error.data.message); } }; return ( <button className={classNames(s['card__favorite'], { [s['card__favorite_is-active']]: isLike, })} onClick={toggleLike}> <LikeSvg /> </button> ); };