diff --git a/src/app_modules/home/fun/get/api_home.ts b/src/app_modules/home/fun/get/api_home.ts index 8600b3aa..c9c58610 100644 --- a/src/app_modules/home/fun/get/api_home.ts +++ b/src/app_modules/home/fun/get/api_home.ts @@ -1,17 +1,73 @@ export const apiGetDataHome = async ({ path }: { path?: string }) => { - const { token } = await fetch("/api/get-cookie").then((res) => res.json()); - if (!token) return await token.json().catch(() => null); + // Fetch token from cookie + try { + const { token } = await fetch("/api/get-cookie").then((res) => res.json()); + if (!token) { + console.error("No token found"); + return null; + } - const response = await fetch(`/api/new/home${path ? path : ""}`, { - headers: { - "Content-Type": "application/json", - Accept: "application/json", - "Access-Control-Allow-Origin": "*", - Authorization: `Bearer ${token}`, - }, - }); + const response = await fetch(`/api/new/home${path ? path : ""}`, { + headers: { + "Content-Type": "application/json", + Accept: "application/json", + Authorization: `Bearer ${token}`, + }, + }); - if (!response.ok) return null; - const data: Record = await response.json(); - return data; + // if (!response.ok) return null; + // const data: Record = await response.json(); + // return data; + + if (!response.ok) { + const errorData = await response.json().catch(() => null); + console.error( + "Error get admin contact:", + errorData?.message || "Unknown error" + ); + + return null; + } + + const result = await response.json(); + return result; + } catch (error) { + console.error("Error get admin contact:", error); + throw error; // Re-throw the error to handle it in the calling function + } +}; + +export const apiGetNotifikasiHome = async () => { + // Fetch token from cookie + try { + const { token } = await fetch("/api/get-cookie").then((res) => res.json()); + if (!token) { + console.error("No token found"); + return null; + } + + const response = await fetch(`/api/notifikasi/count`, { + headers: { + "Content-Type": "application/json", + Accept: "application/json", + Authorization: `Bearer ${token}`, + }, + }); + + if (!response.ok) { + const errorData = await response.json().catch(() => null); + console.error( + "Error get admin contact:", + errorData?.message || "Unknown error" + ); + + return null; + } + + const result = await response.json(); + return result; + } catch (error) { + console.error("Error get admin contact:", error); + throw error; // Re-throw the error to handle it in the calling function + } }; diff --git a/src/app_modules/home/view_home_new.tsx b/src/app_modules/home/view_home_new.tsx index 71ece24a..fad2762c 100644 --- a/src/app_modules/home/view_home_new.tsx +++ b/src/app_modules/home/view_home_new.tsx @@ -1,5 +1,4 @@ "use client"; -import { API_RouteNotifikasi } from "@/lib/api_user_router/route_api_notifikasi"; import { gs_count_ntf, gs_user_ntf } from "@/lib/global_state"; import global_limit from "@/lib/limit"; import { RouterProfile } from "@/lib/router_hipmi/router_katalog"; @@ -18,7 +17,7 @@ import UIGlobal_LayoutTamplate from "../_global/ui/ui_layout_tamplate"; import { gs_notifikasi_kategori_app } from "../notifikasi/lib"; import BodyHome from "./component/body_home"; import FooterHome from "./component/footer_home"; -import { apiGetDataHome } from "./fun/get/api_home"; +import { apiGetDataHome, apiGetNotifikasiHome } from "./fun/get/api_home"; export default function HomeViewNew() { const [countNtf, setCountNtf] = useAtom(gs_count_ntf); @@ -53,9 +52,10 @@ export default function HomeViewNew() { async function onLoadNotifikasi() { try { - const loadNotif = await fetch(API_RouteNotifikasi.get_count_by_id()); - const data = await loadNotif.json().then((res) => res.data); - setCountNtf(data); + const response = await apiGetNotifikasiHome(); + if (response && response.success) { + setCountNtf(response.data); + } } catch (error) { clientLogger.error("Error load notifikasi", error); } @@ -66,11 +66,14 @@ export default function HomeViewNew() { const response = await apiGetDataHome({ path: "?cat=cek_profile", }); - if (response) { + if (response && response.success) { setDataUser(response.data); + } else { + setDataUser(null); } } catch (error) { clientLogger.error("Error get data home", error); + setDataUser(null); } } @@ -83,7 +86,7 @@ export default function HomeViewNew() { customButtonLeft={ !dataUser || !countNtf ? ( - + ) : dataUser?.profile === undefined ? ( - + ) : dataUser?.profile === undefined ? (