feature/IO-3182-Phone-Number-Consent - Checkpoint

This commit is contained in:
Dave Richer
2025-05-20 16:04:36 -04:00
parent 9d81c68a4d
commit 83860152a9
12 changed files with 540 additions and 43 deletions

View File

@@ -8,6 +8,7 @@ import ChatPopupComponent from "../chat-popup/chat-popup.component";
import "./chat-affix.styles.scss";
import { registerMessagingHandlers, unregisterMessagingHandlers } from "./registerMessagingSocketHandlers";
import { useSocket } from "../../contexts/SocketIO/useSocket.js";
import { GET_PHONE_NUMBER_CONSENT } from "../../graphql/consent.queries.js";
export function ChatAffixContainer({ bodyshop, chatVisible }) {
const { t } = useTranslation();
@@ -34,16 +35,59 @@ export function ChatAffixContainer({ bodyshop, chatVisible }) {
SubscribeToTopicForFCMNotification();
//Register WS handlers
// Register WebSocket handlers
if (socket && socket.connected) {
registerMessagingHandlers({ socket, client });
}
return () => {
if (socket && socket.connected) {
// Handle consent-changed events
const handleConsentChanged = ({ bodyshopId, phone_number, consent_status }) => {
try {
client.cache.writeQuery(
{
query: GET_PHONE_NUMBER_CONSENT,
variables: { bodyshopid: bodyshopId, phone_number }
},
(data) => {
if (!data?.phone_number_consent?.[0]) {
return {
phone_number_consent: [
{
__typename: "phone_number_consent",
id: null,
bodyshopid: bodyshopId,
phone_number,
consent_status,
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
consent_updated_at: new Date().toISOString(),
history: []
}
]
};
}
return {
phone_number_consent: [
{
...data.phone_number_consent[0],
consent_status,
consent_updated_at: new Date().toISOString()
}
]
};
}
);
} catch (error) {
console.error("Error updating consent cache:", error);
}
};
socket.on("consent-changed", handleConsentChanged);
return () => {
socket.off("consent-changed", handleConsentChanged);
unregisterMessagingHandlers({ socket });
}
};
};
}
}, [bodyshop, socket, t, client]);
if (!bodyshop || !bodyshop.messagingservicesid) return <></>;