mirror of
https://gitlab.com/foxixus/neomovies.git
synced 2025-10-28 09:58:49 +05:00
Update 35 files
- /src/api.ts - /src/lib/utils.ts - /src/lib/neoApi.ts - /src/lib/mongodb.ts - /src/lib/favoritesApi.ts - /src/lib/models/Favorite.ts - /src/hooks/useTMDBMovies.ts - /src/hooks/useImageLoader.ts - /src/hooks/useMovies.ts - /src/types/movie.ts - /src/components/SearchResults.tsx - /src/components/SettingsContent.tsx - /src/components/MovieCard.tsx - /src/components/FavoriteButton.tsx - /src/components/admin/MovieSearch.tsx - /src/app/page.tsx - /src/app/movie/[id]/page.tsx - /src/app/movie/[id]/MovieContent.tsx - /src/app/api/movies/upcoming/route.ts - /src/app/api/movies/search/route.ts - /src/app/api/movies/top-rated/route.ts - /src/app/api/movies/[id]/route.ts - /src/app/api/movies/popular/route.ts - /src/app/api/favorites/route.ts - /src/app/api/favorites/check/[mediaId]/route.ts - /src/app/api/favorites/[mediaId]/route.ts - /src/app/tv/[id]/TVShowContent.tsx - /src/app/tv/[id]/TVShowPage.tsx - /src/app/tv/[id]/page.tsx - /src/app/favorites/page.tsx - /src/configs/auth.ts - /next.config.js - /package.json - /README.md - /package-lock.json
This commit is contained in:
101
src/components/FavoriteButton.tsx
Normal file
101
src/components/FavoriteButton.tsx
Normal file
@@ -0,0 +1,101 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useSession } from 'next-auth/react';
|
||||
import { favoritesAPI } from '@/lib/favoritesApi';
|
||||
import { Heart } from 'lucide-react';
|
||||
import styled from 'styled-components';
|
||||
import { toast } from 'react-hot-toast';
|
||||
|
||||
const Button = styled.button<{ $isFavorite: boolean }>`
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
padding: 0.5rem 1rem;
|
||||
border-radius: 0.5rem;
|
||||
background: ${props => props.$isFavorite ? 'rgba(255, 0, 0, 0.1)' : 'rgba(255, 255, 255, 0.1)'};
|
||||
border: none;
|
||||
color: ${props => props.$isFavorite ? '#ff4444' : '#fff'};
|
||||
cursor: pointer;
|
||||
transition: all 0.2s;
|
||||
|
||||
&:hover {
|
||||
background: ${props => props.$isFavorite ? 'rgba(255, 0, 0, 0.2)' : 'rgba(255, 255, 255, 0.2)'};
|
||||
}
|
||||
|
||||
svg {
|
||||
width: 1.2rem;
|
||||
height: 1.2rem;
|
||||
fill: ${props => props.$isFavorite ? '#ff4444' : 'none'};
|
||||
stroke: ${props => props.$isFavorite ? '#ff4444' : '#fff'};
|
||||
transition: all 0.2s;
|
||||
}
|
||||
`;
|
||||
|
||||
interface FavoriteButtonProps {
|
||||
mediaId: string | number;
|
||||
mediaType: 'movie' | 'tv';
|
||||
title: string;
|
||||
posterPath: string | null;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export default function FavoriteButton({ mediaId, mediaType, title, posterPath, className }: FavoriteButtonProps) {
|
||||
const { data: session, status } = useSession();
|
||||
const [isFavorite, setIsFavorite] = useState(false);
|
||||
|
||||
// Преобразуем mediaId в строку для сравнения
|
||||
const mediaIdString = mediaId.toString();
|
||||
|
||||
useEffect(() => {
|
||||
const checkFavorite = async () => {
|
||||
// Проверяем только если пользователь авторизован
|
||||
if (status !== 'authenticated' || !session?.user?.email) return;
|
||||
|
||||
try {
|
||||
const response = await favoritesAPI.getFavorites();
|
||||
const favorites = response.data;
|
||||
const isFav = favorites.some(
|
||||
fav => fav.mediaId === mediaIdString && fav.mediaType === mediaType
|
||||
);
|
||||
setIsFavorite(isFav);
|
||||
} catch (error) {
|
||||
console.error('Error checking favorite status:', error);
|
||||
}
|
||||
};
|
||||
|
||||
checkFavorite();
|
||||
}, [session?.user?.email, mediaIdString, mediaType, status]);
|
||||
|
||||
const toggleFavorite = async () => {
|
||||
if (!session?.user?.email) {
|
||||
toast.error('Для добавления в избранное необходимо авторизоваться');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
if (isFavorite) {
|
||||
await favoritesAPI.removeFavorite(mediaIdString, mediaType);
|
||||
toast.success('Удалено из избранного');
|
||||
setIsFavorite(false);
|
||||
} else {
|
||||
await favoritesAPI.addFavorite({
|
||||
mediaId: mediaIdString,
|
||||
mediaType,
|
||||
title,
|
||||
posterPath: posterPath || undefined,
|
||||
});
|
||||
toast.success('Добавлено в избранное');
|
||||
setIsFavorite(true);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error toggling favorite:', error);
|
||||
toast.error('Произошла ошибка');
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Button type="button" onClick={toggleFavorite} $isFavorite={isFavorite} className={className}>
|
||||
<Heart />
|
||||
{isFavorite ? 'В избранном' : 'В избранное'}
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user