2025-01-05 01:43:34 +00:00
|
|
|
|
import { useState, useEffect } from 'react';
|
|
|
|
|
|
import { favoritesAPI } from '@/lib/favoritesApi';
|
|
|
|
|
|
import { Heart } from 'lucide-react';
|
|
|
|
|
|
import { toast } from 'react-hot-toast';
|
2025-07-08 00:15:55 +03:00
|
|
|
|
import cn from 'classnames';
|
2025-01-05 01:43:34 +00:00
|
|
|
|
|
|
|
|
|
|
interface FavoriteButtonProps {
|
|
|
|
|
|
mediaId: string | number;
|
|
|
|
|
|
mediaType: 'movie' | 'tv';
|
|
|
|
|
|
title: string;
|
|
|
|
|
|
posterPath: string | null;
|
|
|
|
|
|
className?: string;
|
2025-07-08 13:41:04 +03:00
|
|
|
|
showText?: boolean;
|
2025-01-05 01:43:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-08 13:41:04 +03:00
|
|
|
|
export default function FavoriteButton({ mediaId, mediaType, title, posterPath, className, showText = false }: FavoriteButtonProps) {
|
2025-07-07 18:22:51 +03:00
|
|
|
|
const token = typeof window !== 'undefined' ? localStorage.getItem('token') : null;
|
2025-01-05 01:43:34 +00:00
|
|
|
|
const [isFavorite, setIsFavorite] = useState(false);
|
|
|
|
|
|
|
|
|
|
|
|
const mediaIdString = mediaId.toString();
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
const checkFavorite = async () => {
|
2025-07-07 18:22:51 +03:00
|
|
|
|
if (!token) return;
|
2025-01-05 01:43:34 +00:00
|
|
|
|
try {
|
2025-07-07 18:22:51 +03:00
|
|
|
|
const { data } = await favoritesAPI.checkFavorite(mediaIdString);
|
2025-07-08 13:41:04 +03:00
|
|
|
|
setIsFavorite(!!data.exists);
|
2025-01-05 01:43:34 +00:00
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('Error checking favorite status:', error);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
checkFavorite();
|
2025-07-07 18:22:51 +03:00
|
|
|
|
}, [token, mediaIdString]);
|
2025-01-05 01:43:34 +00:00
|
|
|
|
|
|
|
|
|
|
const toggleFavorite = async () => {
|
2025-07-07 18:22:51 +03:00
|
|
|
|
if (!token) {
|
2025-01-05 01:43:34 +00:00
|
|
|
|
toast.error('Для добавления в избранное необходимо авторизоваться');
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (isFavorite) {
|
2025-07-07 18:22:51 +03:00
|
|
|
|
await favoritesAPI.removeFavorite(mediaIdString);
|
2025-01-05 01:43:34 +00:00
|
|
|
|
toast.success('Удалено из избранного');
|
|
|
|
|
|
setIsFavorite(false);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
await favoritesAPI.addFavorite({
|
|
|
|
|
|
mediaId: mediaIdString,
|
|
|
|
|
|
mediaType,
|
|
|
|
|
|
title,
|
2025-07-07 18:22:51 +03:00
|
|
|
|
posterPath: posterPath || '',
|
2025-01-05 01:43:34 +00:00
|
|
|
|
});
|
|
|
|
|
|
toast.success('Добавлено в избранное');
|
|
|
|
|
|
setIsFavorite(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('Error toggling favorite:', error);
|
|
|
|
|
|
toast.error('Произошла ошибка');
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-07-08 00:15:55 +03:00
|
|
|
|
const buttonClasses = cn(
|
2025-07-08 13:41:04 +03:00
|
|
|
|
'flex items-center justify-center font-semibold shadow-sm transition-colors focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2',
|
2025-07-08 00:15:55 +03:00
|
|
|
|
{
|
2025-07-08 13:41:04 +03:00
|
|
|
|
'rounded-full p-3 text-base': !showText,
|
|
|
|
|
|
'gap-2 rounded-md px-4 py-3 text-base': showText,
|
2025-07-08 00:15:55 +03:00
|
|
|
|
'bg-red-100 text-red-700 hover:bg-red-200 focus-visible:outline-red-600': isFavorite,
|
2025-07-08 13:41:04 +03:00
|
|
|
|
'bg-warm-200/80 text-warm-800 hover:bg-warm-300/90 backdrop-blur-sm': !isFavorite,
|
2025-07-08 00:15:55 +03:00
|
|
|
|
},
|
|
|
|
|
|
className
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2025-01-05 01:43:34 +00:00
|
|
|
|
return (
|
2025-07-08 00:15:55 +03:00
|
|
|
|
<button type="button" onClick={toggleFavorite} className={buttonClasses}>
|
|
|
|
|
|
<Heart size={20} className={cn({ 'fill-current': isFavorite })} />
|
2025-07-08 13:41:04 +03:00
|
|
|
|
{showText && <span>{isFavorite ? 'В избранном' : 'В избранное'}</span>}
|
2025-07-08 00:15:55 +03:00
|
|
|
|
</button>
|
2025-01-05 01:43:34 +00:00
|
|
|
|
);
|
|
|
|
|
|
}
|