This commit is contained in:
2025-03-25 13:44:58 +07:00
parent b5019b41b4
commit 27057c8588
2 changed files with 24 additions and 15 deletions

View File

@@ -4,6 +4,7 @@ import { JobDetailDataType } from "@/types/job";
// Hàm chung để gọi API // Hàm chung để gọi API
const apiRequest = async (endpoint: string, method: string = "GET", body?: object) => { const apiRequest = async (endpoint: string, method: string = "GET", body?: object) => {
try {
const response = await fetch(`http://localhost:5000${endpoint}`, { const response = await fetch(`http://localhost:5000${endpoint}`, {
method, method,
headers: { headers: {
@@ -14,6 +15,10 @@ const apiRequest = async (endpoint: string, method: string = "GET", body?: objec
if (!response.ok) throw new Error(`Error: ${response.statusText}`); if (!response.ok) throw new Error(`Error: ${response.statusText}`);
return response.json(); return response.json();
} catch (error) {
console.error("API Request failed:", error);
throw error;
}
}; };
// API cho bài viết // API cho bài viết
export const fetchListArticles = () => apiRequest("/list_article"); export const fetchListArticles = () => apiRequest("/list_article");

View File

@@ -8,7 +8,7 @@ import { ArticleListDataType } from "@/types/article";
import { fetchListArticles } from "@/api/apiService"; import { fetchListArticles } from "@/api/apiService";
export default function Home() { export default function Home() {
const [articleList, setArticleList] = useState<ArticleListDataType | null>( const [articlesList, setArticleList] = useState<ArticleListDataType | null>(
null null
); );
@@ -24,9 +24,13 @@ export default function Home() {
homePageEffect.startCarousel("#navheight", true, 3000); homePageEffect.startCarousel("#navheight", true, 3000);
const fetchArticleNews = async () => { const fetchArticleNews = async () => {
try {
const data = await fetchListArticles(); const data = await fetchListArticles();
setArticleList(data.list); setArticleList(data.list);
setLoadingUI(false); setLoadingUI(false);
} catch (error) {
console.log("Failed to fetch articles", error);
}
}; };
fetchArticleNews(); fetchArticleNews();
@@ -606,9 +610,9 @@ export default function Home() {
<div className="image-right bg-gray-100 h-[370px] rounded-[12px]"></div> <div className="image-right bg-gray-100 h-[370px] rounded-[12px]"></div>
</div> </div>
</div> </div>
) : Array.isArray(articleList) && articleList.length > 0 ? ( ) : Array.isArray(articlesList) && articlesList.length > 0 ? (
<div className="content-item-article" id="js-article-new"> <div className="content-item-article" id="js-article-new">
{articleList.slice(0, 1).map((item) => ( {articlesList.slice(0, 1).map((item) => (
<div className="flex" key={item.id}> <div className="flex" key={item.id}>
<div className="info"> <div className="info">
<div className="tag-blog flex items-center"> <div className="tag-blog flex items-center">