fix button user search & notifikasi

This commit is contained in:
2025-02-27 11:02:27 +08:00
parent 18f9cce963
commit 74792a7636
2 changed files with 80 additions and 21 deletions

View File

@@ -1,17 +1,73 @@
export const apiGetDataHome = async ({ path }: { path?: string }) => { export const apiGetDataHome = async ({ path }: { path?: string }) => {
const { token } = await fetch("/api/get-cookie").then((res) => res.json()); // Fetch token from cookie
if (!token) return await token.json().catch(() => null); 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 : ""}`, { const response = await fetch(`/api/new/home${path ? path : ""}`, {
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
Accept: "application/json", Accept: "application/json",
"Access-Control-Allow-Origin": "*", Authorization: `Bearer ${token}`,
Authorization: `Bearer ${token}`, },
}, });
});
if (!response.ok) return null; // if (!response.ok) return null;
const data: Record<string, any> = await response.json(); // const data: Record<string, any> = await response.json();
return data; // 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
}
}; };

View File

@@ -1,5 +1,4 @@
"use client"; "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 { gs_count_ntf, gs_user_ntf } from "@/lib/global_state";
import global_limit from "@/lib/limit"; import global_limit from "@/lib/limit";
import { RouterProfile } from "@/lib/router_hipmi/router_katalog"; 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 { gs_notifikasi_kategori_app } from "../notifikasi/lib";
import BodyHome from "./component/body_home"; import BodyHome from "./component/body_home";
import FooterHome from "./component/footer_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() { export default function HomeViewNew() {
const [countNtf, setCountNtf] = useAtom(gs_count_ntf); const [countNtf, setCountNtf] = useAtom(gs_count_ntf);
@@ -53,9 +52,10 @@ export default function HomeViewNew() {
async function onLoadNotifikasi() { async function onLoadNotifikasi() {
try { try {
const loadNotif = await fetch(API_RouteNotifikasi.get_count_by_id()); const response = await apiGetNotifikasiHome();
const data = await loadNotif.json().then((res) => res.data); if (response && response.success) {
setCountNtf(data); setCountNtf(response.data);
}
} catch (error) { } catch (error) {
clientLogger.error("Error load notifikasi", error); clientLogger.error("Error load notifikasi", error);
} }
@@ -66,11 +66,14 @@ export default function HomeViewNew() {
const response = await apiGetDataHome({ const response = await apiGetDataHome({
path: "?cat=cek_profile", path: "?cat=cek_profile",
}); });
if (response) { if (response && response.success) {
setDataUser(response.data); setDataUser(response.data);
} else {
setDataUser(null);
} }
} catch (error) { } catch (error) {
clientLogger.error("Error get data home", error); clientLogger.error("Error get data home", error);
setDataUser(null);
} }
} }
@@ -83,7 +86,7 @@ export default function HomeViewNew() {
customButtonLeft={ customButtonLeft={
!dataUser || !countNtf ? ( !dataUser || !countNtf ? (
<ActionIcon radius={"xl"} variant={"transparent"}> <ActionIcon radius={"xl"} variant={"transparent"}>
<IconUserSearch color={MainColor.white} /> <IconUserSearch color={"gray"} />
</ActionIcon> </ActionIcon>
) : dataUser?.profile === undefined ? ( ) : dataUser?.profile === undefined ? (
<ActionIcon <ActionIcon
@@ -110,7 +113,7 @@ export default function HomeViewNew() {
customButtonRight={ customButtonRight={
!dataUser || !countNtf ? ( !dataUser || !countNtf ? (
<ActionIcon radius={"xl"} variant={"transparent"}> <ActionIcon radius={"xl"} variant={"transparent"}>
<IconBell color={MainColor.white} /> <IconBell color={"gray"} />
</ActionIcon> </ActionIcon>
) : dataUser?.profile === undefined ? ( ) : dataUser?.profile === undefined ? (
<ActionIcon <ActionIcon