forked from UKSOURCE/hailearning.edu.vn
feat: implement initial home page with dynamic content, API, and dedicated components.
This commit is contained in:
43
api/homeApi.ts
Normal file
43
api/homeApi.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Lấy API URL từ environment variable
|
||||
*/
|
||||
const getApiUrl = (): string => {
|
||||
const apiUrl = process.env.NEXT_PUBLIC_API_URL;
|
||||
|
||||
if (!apiUrl) {
|
||||
console.warn('NEXT_PUBLIC_API_URL is not set. Using default http://localhost:3001');
|
||||
return 'http://localhost:3001';
|
||||
}
|
||||
|
||||
return apiUrl;
|
||||
};
|
||||
|
||||
/**
|
||||
* Fetch home page data từ API
|
||||
* @returns Promise<any>
|
||||
* @throws Error nếu fetch thất bại
|
||||
*/
|
||||
export const fetchHomeData = async (): Promise<any> => {
|
||||
const apiUrl = getApiUrl();
|
||||
const url = `${apiUrl}/api/home`;
|
||||
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
cache: 'no-store',
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error('Error fetching home data:', error);
|
||||
return null; // Trả về null để fallback sang dữ liệu local
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user