Fitur notifikasi dan foreground

Add:
- types/type-notification-category.ts

Fix:
- app/(application)/(user)/notifications/index.tsx
- app/(application)/(user)/test-notifications.tsx
- app/(application)/admin/notification/index.tsx
- components/Notification/NotificationInitializer.tsx
- hooks/use-notification-store.tsx
- service/api-notifications.ts
- utils/formatChatTime.ts

### No Issue
This commit is contained in:
2025-12-24 15:29:58 +08:00
parent 54611ef812
commit 7743a2467c
8 changed files with 260 additions and 89 deletions

View File

@@ -1,4 +1,8 @@
// hooks/useNotificationStore.ts
import {
apiNotificationMarkAsRead,
apiNotificationUnreadCount,
} from "@/service/api-notifications";
import {
createContext,
ReactNode,
@@ -7,10 +11,6 @@ import {
useState,
} from "react";
import { useAuth } from "./use-auth";
import {
apiGetNotificationsById,
apiNotificationUnreadCount,
} from "@/service/api-notifications";
type AppNotification = {
id: string;
@@ -19,7 +19,7 @@ type AppNotification = {
data?: Record<string, string>;
isRead: boolean;
timestamp: number;
type: "notification" | "trigger";
type: "announcement" | "trigger";
// untuk id dari setiap kategori app
appId?: string;
kategoriApp?:
@@ -70,7 +70,7 @@ export const NotificationProvider = ({ children }: { children: ReactNode }) => {
try {
const count = await apiNotificationUnreadCount({
id: user?.id as any,
role: user?.masterUserRoleId as any
role: user?.masterUserRoleId as any,
}); // ← harus return number
const result = count.data;
console.log("📖 Unread count:", result);
@@ -96,10 +96,22 @@ export const NotificationProvider = ({ children }: { children: ReactNode }) => {
setUnreadCount((prev) => prev + 1);
};
const markAsRead = (id: string) => {
setNotifications((prev) =>
prev.map((n) => (n.id === id ? { ...n, isRead: true } : n))
);
const markAsRead = async (id: string) => {
try {
const response = await apiNotificationMarkAsRead({ id });
console.log("🚀 Response Mark As Read:", response);
if (response.success) {
const cloneNotifications = [...notifications];
const index = cloneNotifications.findIndex((n) => n?.data?.id === id);
if (index !== -1) {
cloneNotifications[index].isRead = true;
setNotifications(cloneNotifications);
}
}
} catch (error) {
console.error("Gagal mark as read:", error);
}
};
const syncUnreadCount = async () => {