fix investasi api

This commit is contained in:
2025-02-28 13:55:56 +08:00
parent 88a8bff917
commit f7a5395ca9
4 changed files with 219 additions and 19 deletions

View File

@@ -13,7 +13,9 @@ export function Investasi_ComponentButtonUpdateBeranda({
async function onLoaded() {
try {
setLoading(true);
const response = await apiGetAllInvestasi(`?cat=bursa&page=1`);
const response = await apiFetchGetAllInvestasi({
page: "1",
});
if (response.success) {
onLoadData(response.data);
}

View File

@@ -0,0 +1,33 @@
const apiFetchGetAllInvestasi = async ({ page }: { page: string }) => {
try {
// Fetch token from cookie
const { token } = await fetch("/api/get-cookie").then((res) => res.json());
if (!token) {
console.error("No token found");
return null;
}
const nextPage = `?page=${page}`;
const response = await fetch(`/api/investasi${nextPage}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${token}`,
},
});
// Check if the response is OK
if (!response.ok) {
const errorData = await response.json().catch(() => null);
console.error("Failed to get all forum:", response.statusText, errorData);
throw new Error(errorData?.message || "Failed to get all forum");
}
// Return the JSON response
return await response.json();
} catch (error) {
console.error("Error get all forum", error);
throw error; // Re-throw the error to handle it in the calling function
}
};

View File

@@ -38,13 +38,15 @@ export function Investasi_ViewBerandaNew() {
useShallowEffect(() => {
setIsTriggerReload(false);
setIsShowUpdate(false);
getDataInvestasi();
handleLoadData();
}, []);
async function getDataInvestasi() {
const handleLoadData = async () => {
try {
setLoading(true);
const response = await apiGetAllInvestasi(`?cat=bursa&page=1`);
const response = await apiFetchGetAllInvestasi({
page: `${activePage}`,
});
if (response.success) {
setData(response.data);
}
@@ -53,14 +55,16 @@ export function Investasi_ViewBerandaNew() {
} finally {
setLoading(false);
}
}
};
const handleMoreData = async () => {
try {
const pageNew = activePage + 1;
const loadData = await apiGetAllInvestasi(`?cat=bursa&page=${pageNew}`);
const nextPage = activePage + 1;
const loadData = await apiFetchGetAllInvestasi({
page: `${nextPage}`,
});
if (loadData.success) {
setActivePage(pageNew);
setActivePage(nextPage);
return loadData.data;
} else {
return [];