mirror of
https://gitlab.com/foxixus/neomovies.git
synced 2025-10-27 17:38:50 +05:00
Fix favourites
This commit is contained in:
@@ -4,8 +4,7 @@ import { useEffect, useState } from 'react';
|
|||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import Image from 'next/image';
|
import Image from 'next/image';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
import { favoritesAPI } from '@/lib/favoritesApi';
|
import { neoApi, getImageUrl } from '@/lib/neoApi';
|
||||||
import { getImageUrl } from '@/lib/neoApi';
|
|
||||||
import { Loader2, HeartCrack } from 'lucide-react';
|
import { Loader2, HeartCrack } from 'lucide-react';
|
||||||
|
|
||||||
interface Favorite {
|
interface Favorite {
|
||||||
@@ -25,19 +24,22 @@ export default function FavoritesPage() {
|
|||||||
const fetchFavorites = async () => {
|
const fetchFavorites = async () => {
|
||||||
const token = localStorage.getItem('token');
|
const token = localStorage.getItem('token');
|
||||||
if (!token) {
|
if (!token) {
|
||||||
router.push('/login');
|
router.replace('/login');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await favoritesAPI.getFavorites();
|
const response = await neoApi.get('/api/v1/favorites');
|
||||||
setFavorites(response.data);
|
setFavorites(response.data);
|
||||||
} catch (error) {
|
} catch (error: any) {
|
||||||
console.error('Failed to fetch favorites:', error);
|
console.error('Failed to fetch favorites:', error);
|
||||||
localStorage.removeItem('token');
|
// Редиректим только при явном 401
|
||||||
localStorage.removeItem('userName');
|
if (error?.response?.status === 401) {
|
||||||
localStorage.removeItem('userEmail');
|
localStorage.removeItem('token');
|
||||||
router.push('/login');
|
localStorage.removeItem('userName');
|
||||||
|
localStorage.removeItem('userEmail');
|
||||||
|
router.replace('/login');
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
@@ -117,4 +119,4 @@ export default function FavoritesPage() {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,27 @@
|
|||||||
|
|
||||||
import { ThemeProvider } from 'styled-components';
|
import { ThemeProvider } from 'styled-components';
|
||||||
import { theme } from '@/styles/theme';
|
import { theme } from '@/styles/theme';
|
||||||
export function Providers({ children }: { children: React.ReactNode }) {
|
import { useEffect } from 'react';
|
||||||
return <ThemeProvider theme={theme}>{children}</ThemeProvider>;
|
import { neoApi } from '@/lib/neoApi';
|
||||||
|
|
||||||
|
function TokenBootstrap() {
|
||||||
|
useEffect(() => {
|
||||||
|
try {
|
||||||
|
const token = localStorage.getItem('token');
|
||||||
|
if (token) {
|
||||||
|
neoApi.defaults.headers.common['Authorization'] = `Bearer ${token}`;
|
||||||
|
window.dispatchEvent(new Event('auth-changed'));
|
||||||
|
}
|
||||||
|
} catch {}
|
||||||
|
}, []);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function Providers({ children }: { children: React.ReactNode }) {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<TokenBootstrap />
|
||||||
|
<ThemeProvider theme={theme}>{children}</ThemeProvider>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user