Compare commits

...

1 Commits

Author SHA1 Message Date
34680a4c38 test 2025-12-13 17:31:48 +08:00
5 changed files with 166 additions and 22 deletions

View File

@@ -3,6 +3,7 @@
import { StackCustom, ViewWrapper } from "@/components"; import { StackCustom, ViewWrapper } from "@/components";
import { MainColor } from "@/constants/color-palet"; import { MainColor } from "@/constants/color-palet";
import { useAuth } from "@/hooks/use-auth"; import { useAuth } from "@/hooks/use-auth";
import { useNotificationStore } from "@/hooks/use-notification-store";
import Home_BottomFeatureSection from "@/screens/Home/bottomFeatureSection"; import Home_BottomFeatureSection from "@/screens/Home/bottomFeatureSection";
import Home_ImageSection from "@/screens/Home/imageSection"; import Home_ImageSection from "@/screens/Home/imageSection";
import TabSection from "@/screens/Home/tabSection"; import TabSection from "@/screens/Home/tabSection";
@@ -13,13 +14,18 @@ import { apiVersion } from "@/service/api-config";
import { Ionicons } from "@expo/vector-icons"; import { Ionicons } from "@expo/vector-icons";
import { Redirect, router, Stack, useFocusEffect } from "expo-router"; import { Redirect, router, Stack, useFocusEffect } from "expo-router";
import { useCallback, useEffect, useState } from "react"; import { useCallback, useEffect, useState } from "react";
import { RefreshControl } from "react-native"; import { RefreshControl, Text, View } from "react-native";
export default function Application() { export default function Application() {
const { token, user, userData } = useAuth(); const { token, user, userData } = useAuth();
const [data, setData] = useState<any>(); const [data, setData] = useState<any>();
const [refreshing, setRefreshing] = useState(false); const [refreshing, setRefreshing] = useState(false);
console.log("[User] >>", JSON.stringify(user?.id, null, 2)); // console.log("[User] >>", JSON.stringify(user?.id, null, 2));
const { notifications } = useNotificationStore();
const unreadCount = notifications.filter((n) => !n.read).length;
console.log("UNREAD", unreadCount)
// ‼️ Untuk cek apakah: 1. user ada, 2. user punya profile, 3. accept temrs of forum nya ada atau tidak // ‼️ Untuk cek apakah: 1. user ada, 2. user punya profile, 3. accept temrs of forum nya ada atau tidak
useFocusEffect( useFocusEffect(
@@ -82,17 +88,46 @@ export default function Application() {
}} }}
/> />
), ),
headerRight: () => ( headerRight: () => {
<Ionicons return (
disabled={true} <View style={{ position: "relative" }}>
name="notifications" <Ionicons
size={20} name="notifications"
color={MainColor.placeholder} size={20}
onPress={() => { color={MainColor.yellow}
router.push("/notifications"); onPress={() => {
}} router.push("/notifications");
/> }}
), />
{unreadCount > 0 && (
<View
style={{
position: "absolute",
top: -4,
right: -4,
backgroundColor: "red",
borderRadius: 8,
minWidth: 16,
height: 16,
justifyContent: "center",
alignItems: "center",
paddingHorizontal: 2,
}}
>
<Text
style={{
color: "white",
fontSize: 10,
fontWeight: "bold",
}}
>
{unreadCount > 9 ? "9+" : unreadCount}
</Text>
</View>
)}
</View>
);
},
}} }}
/> />
<ViewWrapper <ViewWrapper

View File

@@ -4,7 +4,14 @@ import { useEffect } from "react";
import "react-native-gesture-handler"; import "react-native-gesture-handler";
import { SafeAreaProvider } from "react-native-safe-area-context"; import { SafeAreaProvider } from "react-native-safe-area-context";
import Toast from "react-native-toast-message"; import Toast from "react-native-toast-message";
import messaging from "@react-native-firebase/messaging"; import messaging, {
FirebaseMessagingTypes,
} from "@react-native-firebase/messaging";
import { useForegroundNotifications } from "@/hooks/use-foreground-notifications";
import {
NotificationProvider,
useNotificationStore,
} from "@/hooks/use-notification-store";
export default function RootLayout() { export default function RootLayout() {
useEffect(() => { useEffect(() => {
@@ -27,14 +34,41 @@ export default function RootLayout() {
testFCM(); testFCM();
}, []); }, []);
const { addNotification } = useNotificationStore();
const handleForegroundNotification = (
message: FirebaseMessagingTypes.RemoteMessage
) => {
const title = message.notification?.title || "Notifikasi";
const body = message.notification?.body || "";
const rawData = message.data || {};
const safeData: Record<string, string> = {};
for (const key in rawData) {
if (typeof rawData[key] === "string") {
safeData[key] = rawData[key] as string;
} else {
// Jika object/array/number → ubah ke JSON string
safeData[key] = JSON.stringify(rawData[key]);
}
}
// ✅ Simpan ke state → akan trigger update UI (termasuk icon bell)
addNotification({ body, title, data: safeData });
};
useForegroundNotifications(handleForegroundNotification);
return ( return (
<> <>
<SafeAreaProvider> <NotificationProvider>
<AuthProvider> <SafeAreaProvider>
<AppRoot /> <AuthProvider>
</AuthProvider> <AppRoot />
</SafeAreaProvider> </AuthProvider>
<Toast /> </SafeAreaProvider>
<Toast />
</NotificationProvider>
</> </>
); );
} }

View File

@@ -0,0 +1,23 @@
import { useEffect } from "react";
import messaging, {
FirebaseMessagingTypes,
} from "@react-native-firebase/messaging";
// Gunakan tipe resmi dari library
type RemoteMessage = FirebaseMessagingTypes.RemoteMessage;
export function useForegroundNotifications(
onMessageReceived: (message: RemoteMessage) => void
) {
useEffect(() => {
const unsubscribe = messaging().onMessage((remoteMessage) => {
console.log(
"🔔 Notifikasi diterima saat app aktif:",
JSON.stringify(remoteMessage, null, 2)
);
onMessageReceived(remoteMessage);
});
return unsubscribe;
}, [onMessageReceived]);
}

View File

@@ -0,0 +1,52 @@
// hooks/useNotificationStore.ts
import { createContext, useContext, useState, ReactNode } from 'react';
import type { FirebaseMessagingTypes } from '@react-native-firebase/messaging';
type AppNotification = {
id: string;
title: string;
body: string;
data?: Record<string, string>;
read: boolean;
timestamp: number;
};
const NotificationContext = createContext<{
notifications: AppNotification[];
addNotification: (notif: Omit<AppNotification, 'id' | 'read' | 'timestamp'>) => void;
markAsRead: (id: string) => void;
}>({
notifications: [],
addNotification: () => {},
markAsRead: () => {},
});
export const NotificationProvider = ({ children }: { children: ReactNode }) => {
const [notifications, setNotifications] = useState<AppNotification[]>([]);
const addNotification = (notif: Omit<AppNotification, 'id' | 'read' | 'timestamp'>) => {
setNotifications(prev => [
{
...notif,
id: Date.now().toString(),
read: false,
timestamp: Date.now(),
},
...prev,
]);
};
const markAsRead = (id: string) => {
setNotifications(prev =>
prev.map(n => (n.id === id ? { ...n, read: true } : n))
);
};
return (
<NotificationContext.Provider value={{ notifications, addNotification, markAsRead }}>
{children}
</NotificationContext.Provider>
);
};
export const useNotificationStore = () => useContext(NotificationContext);

View File

@@ -134,10 +134,10 @@ export default function LoginView() {
if (token && token !== "" && isAdmin) { if (token && token !== "" && isAdmin) {
// Akan di aktifkan jika sudah losos review // Akan di aktifkan jika sudah losos review
// return <Redirect href={"/(application)/admin/dashboard"} />; return <Redirect href={"/(application)/admin/dashboard"} />;
// Sementara gunakan ini // Sementara gunakan ini
return <Redirect href={"/(application)/(user)/home"} />; // return <Redirect href={"/(application)/(user)/home"} />;
} }
return ( return (