diff --git a/frontend/app/page.tsx b/frontend/app/page.tsx index 8ed6906..6d013a9 100644 --- a/frontend/app/page.tsx +++ b/frontend/app/page.tsx @@ -4,26 +4,53 @@ import BlogCard from "./components/BlogCard"; import { useEffect, useState } from "react"; import Link from "next/link"; - const usersAPI = process.env.NEXT_PUBLIC_USERS_API_URL; +const blogsAPI = process.env.NEXT_PUBLIC_BLOGS_API_URL; + export default function Home() { - const [blogs, setBlogs] = useState([]); + const [blogs, setBlogs] = useState([]); + const [categories, setCategories] = useState([]); const [loading, setLoading] = useState(true); + const [selectedCategory, setSelectedCategory] = useState(null); + useEffect(() => { - const fetchData = async () => { + const fetchBlogs = async () => { try { const response = await fetch(`${usersAPI}/blogs`); - const data = await response.json(); - setBlogs(data); + const blogsData = await response.json(); + const blogsWithCategories = await Promise.all( + blogsData.map(async (blog : any) => { + const categoriesResponse = await fetch(`${blogsAPI}/blogs/${blog.id}/categories`); + const categoriesData = await categoriesResponse.json(); + return { ...blog, categories: categoriesData }; + }) + ); + setBlogs(blogsWithCategories); } catch (error) { console.error('Error fetching blogs:', error); } finally { setLoading(false); } }; - fetchData(); + + const fetchCategories = async () => { + try { + const response = await fetch(`${blogsAPI}/categories`); + const data = await response.json(); + setCategories(data); + } catch (error) { + console.error('Error fetching categories:', error); + } + }; + + fetchBlogs(); + fetchCategories(); }, []); + const filteredBlogs = selectedCategory + ? blogs.filter(blog => blog.categories.some((category : any) => category.id === selectedCategory.id)) + : blogs; + return (
+
+ +
{loading ? (
) : (
- {blogs && blogs.map((blog, index) => ( + {filteredBlogs.map((blog, index) => (
- + +
+ {blog.categories.map((category: any) => ( + + {category.name} + + ))} +

))}