| 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 |
import { startActivation, startInstall } from '../utils/contentEmbedInstaller'; |
| 12 |
|
| 13 |
export type Message = { key: MessageType; payload?: any }; |
| 14 |
|
| 15 |
const messageMapper: Map<MessageType, Function> = new Map([ |
| 16 |
[ |
| 17 |
PluginMessages.TrackConsent, |
| 18 |
(message: Message) => { |
| 19 |
trackConsent(message.payload); |
| 20 |
}, |
| 21 |
], |
| 22 |
[ |
| 23 |
PluginMessages.InternalTrackingChangeRequest, |
| 24 |
(message: Message, embedder: any) => { |
| 25 |
disableInternalTracking(message.payload) |
| 26 |
.then(() => { |
| 27 |
embedder.postMessage({ |
| 28 |
key: PluginMessages.InternalTrackingFetchResponse, |
| 29 |
payload: message.payload, |
| 30 |
}); |
| 31 |
}) |
| 32 |
.catch(payload => { |
| 33 |
embedder.postMessage({ |
| 34 |
key: PluginMessages.InternalTrackingChangeError, |
| 35 |
payload, |
| 36 |
}); |
| 37 |
}); |
| 38 |
}, |
| 39 |
], |
| 40 |
[ |
| 41 |
PluginMessages.InternalTrackingFetchRequest, |
| 42 |
(__message: Message, embedder: any) => { |
| 43 |
fetchDisableInternalTracking() |
| 44 |
.then(({ message: payload }) => { |
| 45 |
embedder.postMessage({ |
| 46 |
key: PluginMessages.InternalTrackingFetchResponse, |
| 47 |
payload, |
| 48 |
}); |
| 49 |
}) |
| 50 |
.catch(payload => { |
| 51 |
embedder.postMessage({ |
| 52 |
key: PluginMessages.InternalTrackingFetchError, |
| 53 |
payload, |
| 54 |
}); |
| 55 |
}); |
| 56 |
}, |
| 57 |
], |
| 58 |
[ |
| 59 |
PluginMessages.BusinessUnitFetchRequest, |
| 60 |
(__message: Message, embedder: any) => { |
| 61 |
getBusinessUnitId() |
| 62 |
.then(payload => { |
| 63 |
embedder.postMessage({ |
| 64 |
key: PluginMessages.BusinessUnitFetchResponse, |
| 65 |
payload, |
| 66 |
}); |
| 67 |
}) |
| 68 |
.catch(payload => { |
| 69 |
embedder.postMessage({ |
| 70 |
key: PluginMessages.BusinessUnitFetchError, |
| 71 |
payload, |
| 72 |
}); |
| 73 |
}); |
| 74 |
}, |
| 75 |
], |
| 76 |
[ |
| 77 |
PluginMessages.BusinessUnitChangeRequest, |
| 78 |
(message: Message, embedder: any) => { |
| 79 |
setBusinessUnitId(message.payload) |
| 80 |
.then(payload => { |
| 81 |
embedder.postMessage({ |
| 82 |
key: PluginMessages.BusinessUnitFetchResponse, |
| 83 |
payload, |
| 84 |
}); |
| 85 |
}) |
| 86 |
.catch(payload => { |
| 87 |
embedder.postMessage({ |
| 88 |
key: PluginMessages.BusinessUnitChangeError, |
| 89 |
payload, |
| 90 |
}); |
| 91 |
}); |
| 92 |
}, |
| 93 |
], |
| 94 |
[ |
| 95 |
PluginMessages.SkipReviewRequest, |
| 96 |
(__message: Message, embedder: any) => { |
| 97 |
skipReview() |
| 98 |
.then(payload => { |
| 99 |
embedder.postMessage({ |
| 100 |
key: PluginMessages.SkipReviewResponse, |
| 101 |
payload, |
| 102 |
}); |
| 103 |
}) |
| 104 |
.catch(payload => { |
| 105 |
embedder.postMessage({ |
| 106 |
key: PluginMessages.SkipReviewError, |
| 107 |
payload, |
| 108 |
}); |
| 109 |
}); |
| 110 |
}, |
| 111 |
], |
| 112 |
[ |
| 113 |
PluginMessages.RemoveParentQueryParam, |
| 114 |
(message: Message) => { |
| 115 |
removeQueryParamFromLocation(message.payload); |
| 116 |
}, |
| 117 |
], |
| 118 |
[ |
| 119 |
PluginMessages.ContentEmbedInstallRequest, |
| 120 |
(message: Message, embedder: any) => { |
| 121 |
startInstall(message.payload.nonce) |
| 122 |
.then(payload => { |
| 123 |
embedder.postMessage({ |
| 124 |
key: PluginMessages.ContentEmbedInstallResponse, |
| 125 |
payload: payload, |
| 126 |
}); |
| 127 |
}) |
| 128 |
.catch(payload => { |
| 129 |
embedder.postMessage({ |
| 130 |
key: PluginMessages.ContentEmbedInstallError, |
| 131 |
payload, |
| 132 |
}); |
| 133 |
}); |
| 134 |
}, |
| 135 |
], |
| 136 |
[ |
| 137 |
PluginMessages.ContentEmbedActivationRequest, |
| 138 |
(message: Message, embedder: any) => { |
| 139 |
startActivation(message.payload.activateAjaxUrl) |
| 140 |
.then(payload => { |
| 141 |
embedder.postMessage({ |
| 142 |
key: PluginMessages.ContentEmbedActivationResponse, |
| 143 |
payload: payload, |
| 144 |
}); |
| 145 |
}) |
| 146 |
.catch(payload => { |
| 147 |
embedder.postMessage({ |
| 148 |
key: PluginMessages.ContentEmbedActivationError, |
| 149 |
payload, |
| 150 |
}); |
| 151 |
}); |
| 152 |
}, |
| 153 |
], |
| 154 |
]); |
| 155 |
|
| 156 |
export const messageMiddleware = (embedder: any) => (message: Message) => { |
| 157 |
const next = messageMapper.get(message.key); |
| 158 |
if (next) { |
| 159 |
next(message, embedder); |
| 160 |
} |
| 161 |
}; |
| 162 |
|