feature/IO-3499-React-19 - Phone Number Formatter / Chat Open Button / Chat Affix container
This commit is contained in:
@@ -12,11 +12,16 @@ export function ChatAffixContainer({ bodyshop, chatVisible, currentUser }) {
|
||||
const client = useApolloClient();
|
||||
const { socket } = useSocket();
|
||||
|
||||
// 1) FCM subscription (independent of socket handler registration)
|
||||
useEffect(() => {
|
||||
if (!bodyshop?.messagingservicesid) return;
|
||||
const messagingServicesId = bodyshop?.messagingservicesid;
|
||||
const bodyshopId = bodyshop?.id;
|
||||
const imexshopid = bodyshop?.imexshopid;
|
||||
|
||||
async function subscribeToTopicForFCMNotification() {
|
||||
const messagingEnabled = Boolean(messagingServicesId);
|
||||
|
||||
useEffect(() => {
|
||||
if (!messagingEnabled) return;
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
await requestForToken();
|
||||
await axios.post("/notifications/subscribe", {
|
||||
@@ -24,23 +29,19 @@ export function ChatAffixContainer({ bodyshop, chatVisible, currentUser }) {
|
||||
vapidKey: import.meta.env.VITE_APP_FIREBASE_PUBLIC_VAPID_KEY
|
||||
}),
|
||||
type: "messaging",
|
||||
imexshopid: bodyshop.imexshopid
|
||||
imexshopid
|
||||
});
|
||||
} catch (error) {
|
||||
console.log("Error attempting to subscribe to messaging topic: ", error);
|
||||
}
|
||||
}
|
||||
})();
|
||||
}, [messagingEnabled, imexshopid]);
|
||||
|
||||
subscribeToTopicForFCMNotification();
|
||||
}, [bodyshop?.messagingservicesid, bodyshop?.imexshopid]);
|
||||
|
||||
// 2) Register socket handlers as soon as socket is connected (regardless of chatVisible)
|
||||
useEffect(() => {
|
||||
if (!socket) return;
|
||||
if (!bodyshop?.messagingservicesid) return;
|
||||
if (!bodyshop?.id) return;
|
||||
if (!messagingEnabled) return;
|
||||
if (!bodyshopId) return;
|
||||
|
||||
// If socket isn't connected yet, ensure no stale handlers remain.
|
||||
if (!socket.connected) {
|
||||
unregisterMessagingHandlers({ socket });
|
||||
return;
|
||||
@@ -56,16 +57,14 @@ export function ChatAffixContainer({ bodyshop, chatVisible, currentUser }) {
|
||||
bodyshop
|
||||
});
|
||||
|
||||
return () => {
|
||||
unregisterMessagingHandlers({ socket });
|
||||
};
|
||||
}, [socket, socket?.connected, bodyshop?.id, bodyshop?.messagingservicesid, client, currentUser?.email]);
|
||||
return () => unregisterMessagingHandlers({ socket });
|
||||
}, [socket, messagingEnabled, bodyshopId, client, currentUser?.email, bodyshop]);
|
||||
|
||||
if (!bodyshop?.messagingservicesid) return <></>;
|
||||
if (!messagingEnabled) return null;
|
||||
|
||||
return (
|
||||
<div className={`chat-affix ${chatVisible ? "chat-affix-open" : ""}`}>
|
||||
{bodyshop?.messagingservicesid ? <ChatPopupComponent /> : null}
|
||||
{messagingEnabled ? <ChatPopupComponent /> : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user