From 27057c85883eef67a16d9038eeaf66f64217b1c0 Mon Sep 17 00:00:00 2001 From: Tieptk Date: Tue, 25 Mar 2025 13:44:58 +0700 Subject: [PATCH] up 25/03 --- src/api/apiService.ts | 23 ++++++++++++++--------- src/app/page.tsx | 16 ++++++++++------ 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/api/apiService.ts b/src/api/apiService.ts index 2017e79..520a1b0 100644 --- a/src/api/apiService.ts +++ b/src/api/apiService.ts @@ -4,16 +4,21 @@ import { JobDetailDataType } from "@/types/job"; // Hàm chung để gọi API const apiRequest = async (endpoint: string, method: string = "GET", body?: object) => { - const response = await fetch(`http://localhost:5000${endpoint}`, { - method, - headers: { - "Content-Type": "application/json", - }, - body: body ? JSON.stringify(body) : null, - }); + try { + const response = await fetch(`http://localhost:5000${endpoint}`, { + method, + headers: { + "Content-Type": "application/json", + }, + body: body ? JSON.stringify(body) : null, + }); - if (!response.ok) throw new Error(`Error: ${response.statusText}`); - return response.json(); + if (!response.ok) throw new Error(`Error: ${response.statusText}`); + return response.json(); + } catch (error) { + console.error("API Request failed:", error); + throw error; + } }; // API cho bài viết export const fetchListArticles = () => apiRequest("/list_article"); diff --git a/src/app/page.tsx b/src/app/page.tsx index 9fe53e6..6454264 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -8,7 +8,7 @@ import { ArticleListDataType } from "@/types/article"; import { fetchListArticles } from "@/api/apiService"; export default function Home() { - const [articleList, setArticleList] = useState( + const [articlesList, setArticleList] = useState( null ); @@ -24,9 +24,13 @@ export default function Home() { homePageEffect.startCarousel("#navheight", true, 3000); const fetchArticleNews = async () => { - const data = await fetchListArticles(); - setArticleList(data.list); - setLoadingUI(false); + try { + const data = await fetchListArticles(); + setArticleList(data.list); + setLoadingUI(false); + } catch (error) { + console.log("Failed to fetch articles", error); + } }; fetchArticleNews(); @@ -606,9 +610,9 @@ export default function Home() {
- ) : Array.isArray(articleList) && articleList.length > 0 ? ( + ) : Array.isArray(articlesList) && articlesList.length > 0 ? (
- {articleList.slice(0, 1).map((item) => ( + {articlesList.slice(0, 1).map((item) => (