fixes for public listed articles

This commit is contained in:
2025-11-11 18:47:49 +05:30
parent b2a7df5760
commit 661f8c915b

View File

@@ -18,15 +18,8 @@ export const ArticleProvider: React.FC<{ children: React.ReactNode }> = ({ child
setLoading(true); setLoading(true);
setError(null); setError(null);
const res = await api.get<ArticleModel[]>('/articles', { const res = await api.get<ArticleModel[]>('/articles', { params: { skip: 0, limit: 10 } });
params: { skip: 0, limit: 10 }, const formatted = res.data.map((a) => ({ ...a, id: a._id || undefined }));
});
const formatted = res.data.map((a) => ({
...a,
id: a._id || undefined,
}));
setArticles(formatted); setArticles(formatted);
} catch (err: any) { } catch (err: any) {
console.error('Failed to fetch articles:', err); console.error('Failed to fetch articles:', err);
@@ -38,7 +31,13 @@ export const ArticleProvider: React.FC<{ children: React.ReactNode }> = ({ child
/** 🔹 Auto-fetch articles whenever user logs in/out */ /** 🔹 Auto-fetch articles whenever user logs in/out */
useEffect(() => { useEffect(() => {
if (token) fetchArticles(); // Always load once on mount
// If endpoint requires JWT, fallback safely
if (!token) {
fetchArticles().catch(() => setLoading(false)); // try anyway (handles both public/protected)
} else {
fetchArticles();
}
}, [token]); }, [token]);
return ( return (