chore: fix notif
This commit is contained in:
@@ -10,15 +10,6 @@ import React, {
|
||||
import { getClientNotifications, markNotificationsRead } from "../api/api";
|
||||
import type { ClientNotification } from "../api/api";
|
||||
import { getToken } from "../auth/tokenStorage";
|
||||
import {
|
||||
registerForPushNotificationsAsync,
|
||||
sendPushTokenToBackend,
|
||||
setupNotificationChannel,
|
||||
addNotificationReceivedListener,
|
||||
addNotificationResponseListener,
|
||||
setBadgeCount,
|
||||
getLastNotificationResponse,
|
||||
} from "../services/pushNotifications";
|
||||
|
||||
interface ToastData {
|
||||
message: string;
|
||||
@@ -32,7 +23,6 @@ interface NotificationContextType {
|
||||
refreshNotifications: () => Promise<void>;
|
||||
toast: ToastData | null;
|
||||
clearToast: () => void;
|
||||
pushToken: string | null;
|
||||
navigateToOrder: number | null;
|
||||
clearNavigateToOrder: () => void;
|
||||
}
|
||||
@@ -44,7 +34,6 @@ const NotificationContext = createContext<NotificationContextType>({
|
||||
refreshNotifications: async () => {},
|
||||
toast: null,
|
||||
clearToast: () => {},
|
||||
pushToken: null,
|
||||
navigateToOrder: null,
|
||||
clearNavigateToOrder: () => {},
|
||||
});
|
||||
@@ -57,35 +46,30 @@ function getToastType(
|
||||
notifType: string,
|
||||
): "success" | "error" | "warning" | "info" {
|
||||
switch (notifType) {
|
||||
case "order_confirmed":
|
||||
case "order_approved":
|
||||
case "assigned":
|
||||
return "success";
|
||||
case "order_en_route":
|
||||
case "order_assigned":
|
||||
case "en_route":
|
||||
case "support":
|
||||
return "info";
|
||||
case "order_delivered":
|
||||
return "warning";
|
||||
case "livre":
|
||||
return "success";
|
||||
case "failed":
|
||||
return "error";
|
||||
default:
|
||||
return "info";
|
||||
}
|
||||
}
|
||||
|
||||
export function NotificationProvider({ children }: { children: ReactNode }) {
|
||||
const [notifications, setNotifications] = useState<ClientNotification[]>(
|
||||
[],
|
||||
);
|
||||
const [notifications, setNotifications] = useState<ClientNotification[]>([]);
|
||||
const [unreadCount, setUnreadCount] = useState(0);
|
||||
const [toast, setToast] = useState<ToastData | null>(null);
|
||||
const [pushToken, setPushToken] = useState<string | null>(null);
|
||||
const [navigateToOrder, setNavigateToOrder] = useState<number | null>(null);
|
||||
const seenIdsRef = useRef<Set<string>>(new Set());
|
||||
const isFirstLoadRef = useRef(true);
|
||||
|
||||
const clearToast = useCallback(() => setToast(null), []);
|
||||
const clearNavigateToOrder = useCallback(
|
||||
() => setNavigateToOrder(null),
|
||||
[],
|
||||
);
|
||||
const clearNavigateToOrder = useCallback(() => setNavigateToOrder(null), []);
|
||||
|
||||
const showToast = useCallback(
|
||||
(message: string, type: ToastData["type"]) => {
|
||||
@@ -95,68 +79,6 @@ export function NotificationProvider({ children }: { children: ReactNode }) {
|
||||
[],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
let isMounted = true;
|
||||
|
||||
const initPush = async () => {
|
||||
const token = await getToken();
|
||||
if (!token) return;
|
||||
|
||||
await setupNotificationChannel();
|
||||
|
||||
const expoPushToken = await registerForPushNotificationsAsync();
|
||||
if (expoPushToken && isMounted) {
|
||||
setPushToken(expoPushToken);
|
||||
await sendPushTokenToBackend(expoPushToken);
|
||||
}
|
||||
|
||||
const lastResponse = await getLastNotificationResponse();
|
||||
if (lastResponse && isMounted) {
|
||||
const data = lastResponse.notification.request.content.data;
|
||||
if (data?.command_id) {
|
||||
setNavigateToOrder(data.command_id as number);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
initPush();
|
||||
|
||||
return () => {
|
||||
isMounted = false;
|
||||
};
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const receivedSub = addNotificationReceivedListener((notification) => {
|
||||
const { title, body } = notification.request.content;
|
||||
const data = notification.request.content.data;
|
||||
|
||||
// Afficher un toast pour la notification push en foreground
|
||||
if (body) {
|
||||
const notifType = (data?.type as string) || "";
|
||||
showToast(body, getToastType(notifType));
|
||||
}
|
||||
|
||||
// Rafraîchir la liste des notifications
|
||||
fetchNotifications();
|
||||
});
|
||||
|
||||
// L'utilisateur tap sur une notification
|
||||
const responseSub = addNotificationResponseListener((response) => {
|
||||
const data = response.notification.request.content.data;
|
||||
if (data?.command_id) {
|
||||
setNavigateToOrder(data.command_id as number);
|
||||
}
|
||||
// Rafraîchir les notifications
|
||||
fetchNotifications();
|
||||
});
|
||||
|
||||
return () => {
|
||||
receivedSub.remove();
|
||||
responseSub.remove();
|
||||
};
|
||||
}, [showToast]);
|
||||
|
||||
const fetchNotifications = useCallback(async () => {
|
||||
const token = await getToken();
|
||||
if (!token) return;
|
||||
@@ -168,11 +90,7 @@ export function NotificationProvider({ children }: { children: ReactNode }) {
|
||||
setNotifications(newNotifs);
|
||||
setUnreadCount(response.unread_count);
|
||||
|
||||
// Mettre à jour le badge de l'app
|
||||
await setBadgeCount(response.unread_count);
|
||||
|
||||
if (!isFirstLoadRef.current) {
|
||||
// Détecter les NOUVELLES notifications non lues
|
||||
for (const notif of newNotifs) {
|
||||
if (notif.read) continue;
|
||||
const key = `${notif.command_id}-${notif.type}-${notif.created_at}`;
|
||||
@@ -182,7 +100,6 @@ export function NotificationProvider({ children }: { children: ReactNode }) {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Premier chargement : enregistrer tous les IDs sans toast
|
||||
for (const notif of newNotifs) {
|
||||
const key = `${notif.command_id}-${notif.type}-${notif.created_at}`;
|
||||
seenIdsRef.current.add(key);
|
||||
@@ -191,7 +108,7 @@ export function NotificationProvider({ children }: { children: ReactNode }) {
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Silencieux en cas d'erreur
|
||||
// Silencieux
|
||||
}
|
||||
}, [showToast]);
|
||||
|
||||
@@ -206,8 +123,6 @@ export function NotificationProvider({ children }: { children: ReactNode }) {
|
||||
setNotifications((prev) =>
|
||||
prev.map((n) => ({ ...n, read: true })),
|
||||
);
|
||||
// Remettre le badge à 0
|
||||
await setBadgeCount(0);
|
||||
}
|
||||
} catch {
|
||||
// Silencieux
|
||||
@@ -230,7 +145,6 @@ export function NotificationProvider({ children }: { children: ReactNode }) {
|
||||
refreshNotifications: fetchNotifications,
|
||||
toast,
|
||||
clearToast,
|
||||
pushToken,
|
||||
navigateToOrder,
|
||||
clearNavigateToOrder,
|
||||
}}
|
||||
|
||||
Reference in New Issue
Block a user