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 }) => {
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<string, any> = await response.json();
return data;
// if (!response.ok) return null;
// const data: Record<string, any> = 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
}
};

View File

@@ -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 ? (
<ActionIcon radius={"xl"} variant={"transparent"}>
<IconUserSearch color={MainColor.white} />
<IconUserSearch color={"gray"} />
</ActionIcon>
) : dataUser?.profile === undefined ? (
<ActionIcon
@@ -110,7 +113,7 @@ export default function HomeViewNew() {
customButtonRight={
!dataUser || !countNtf ? (
<ActionIcon radius={"xl"} variant={"transparent"}>
<IconBell color={MainColor.white} />
<IconBell color={"gray"} />
</ActionIcon>
) : dataUser?.profile === undefined ? (
<ActionIcon