PluginProbe
HubSpot All-In-One Marketing – Forms, Popups, Live Chat / 11.0.7
HubSpot All-In-One Marketing – Forms, Popups, Live Chat v11.0.7
11.3.75 11.3.73 11.3.71 11.3.70 11.3.69 11.3.64 11.3.65 11.3.62 11.3.61 11.3.56 11.3.58 11.0.31 11.0.52 11.0.54 11.0.56 11.0.58 11.0.7 11.1.10 11.1.11 11.1.13 11.1.14 11.1.15 11.1.2 11.1.20 11.1.21 All 73 releases
leadin / scripts / iframe / messageMiddleware.ts

messageMiddleware.ts in HubSpot All-In-One Marketing – Forms, Popups, Live Chat 11.0.7, at scripts/iframe/messageMiddleware.ts

125 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { MessageType, PluginMessages } from '../iframe/integratedMessages';
2 import {
3 fetchDisableInternalTracking,
4 trackConsent,
5 disableInternalTracking,
6 getBusinessUnitId,
7 setBusinessUnitId,
8 skipReview,
9 } from '../api/wordpressApiClient';
10 import { removeQueryParamFromLocation } from '../utils/queryParams';
11
12 export type Message = { key: MessageType; payload?: any };
13
14 const messageMapper: Map<MessageType, Function> = new Map([
15 [
16 PluginMessages.TrackConsent,
17 (message: Message) => {
18 trackConsent(message.payload);
19 },
20 ],
21 [
22 PluginMessages.InternalTrackingChangeRequest,
23 (message: Message, embedder: any) => {
24 disableInternalTracking(message.payload)
25 .then(() => {
26 embedder.postMessage({
27 key: PluginMessages.InternalTrackingFetchResponse,
28 payload: message.payload,
29 });
30 })
31 .catch(payload => {
32 embedder.postMessage({
33 key: PluginMessages.InternalTrackingChangeError,
34 payload,
35 });
36 });
37 },
38 ],
39 [
40 PluginMessages.InternalTrackingFetchRequest,
41 (__message: Message, embedder: any) => {
42 fetchDisableInternalTracking()
43 .then(({ message: payload }) => {
44 embedder.postMessage({
45 key: PluginMessages.InternalTrackingFetchResponse,
46 payload,
47 });
48 })
49 .catch(payload => {
50 embedder.postMessage({
51 key: PluginMessages.InternalTrackingFetchError,
52 payload,
53 });
54 });
55 },
56 ],
57 [
58 PluginMessages.BusinessUnitFetchRequest,
59 (__message: Message, embedder: any) => {
60 getBusinessUnitId()
61 .then(payload => {
62 embedder.postMessage({
63 key: PluginMessages.BusinessUnitFetchResponse,
64 payload,
65 });
66 })
67 .catch(payload => {
68 embedder.postMessage({
69 key: PluginMessages.BusinessUnitFetchError,
70 payload,
71 });
72 });
73 },
74 ],
75 [
76 PluginMessages.BusinessUnitChangeRequest,
77 (message: Message, embedder: any) => {
78 setBusinessUnitId(message.payload)
79 .then(payload => {
80 embedder.postMessage({
81 key: PluginMessages.BusinessUnitFetchResponse,
82 payload,
83 });
84 })
85 .catch(payload => {
86 embedder.postMessage({
87 key: PluginMessages.BusinessUnitChangeError,
88 payload,
89 });
90 });
91 },
92 ],
93 [
94 PluginMessages.SkipReviewRequest,
95 (__message: Message, embedder: any) => {
96 skipReview()
97 .then(payload => {
98 embedder.postMessage({
99 key: PluginMessages.SkipReviewResponse,
100 payload,
101 });
102 })
103 .catch(payload => {
104 embedder.postMessage({
105 key: PluginMessages.SkipReviewError,
106 payload,
107 });
108 });
109 },
110 ],
111 [
112 PluginMessages.RemoveParentQueryParam,
113 (message: Message) => {
114 removeQueryParamFromLocation(message.payload);
115 },
116 ],
117 ]);
118
119 export const messageMiddleware = (embedder: any) => (message: Message) => {
120 const next = messageMapper.get(message.key);
121 if (next) {
122 next(message, embedder);
123 }
124 };
125