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,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");

View File

@@ -8,7 +8,7 @@ import { ArticleListDataType } from "@/types/article";
import { fetchListArticles } from "@/api/apiService";
export default function Home() {
const [articleList, setArticleList] = useState<ArticleListDataType | null>(
const [articlesList, setArticleList] = useState<ArticleListDataType | null>(
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() {
<div className="image-right bg-gray-100 h-[370px] rounded-[12px]"></div>
</div>
</div>
) : Array.isArray(articleList) && articleList.length > 0 ? (
) : Array.isArray(articlesList) && articlesList.length > 0 ? (
<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="info">
<div className="tag-blog flex items-center">