| @@ -2,10 +2,13 @@ | ||
| 2 | 2 | deviceId, |
| 3 | 3 | hubspotBaseUrl, |
| 4 | 4 | locale, |
| 5 | 5 | portalId, |
| 6 | + leadinPluginVersion, | |
| 6 | 7 | } from '../constants/leadinConfig'; |
| 7 | 8 | import { initApp } from './appUtils'; |
| 9 | +import { fetchAccessToken } from '../api/wordpressApiClient'; | |
| 10 | +import { isEmbedderReady } from './embedderReady'; | |
| 8 | 11 | |
| 9 | 12 | type CallbackFn = (...args: any[]) => void; |
| 10 | 13 | |
| 11 | 14 | export function initBackgroundApp(initFn: CallbackFn | CallbackFn[]) { |
| @@ -18,17 +21,27 @@ | ||
| 18 | 21 | } |
| 19 | 22 | initApp(main); |
| 20 | 23 | } |
| 21 | 24 | |
| 22 | -export const getOrCreateBackgroundApp = (refreshToken: string) => { | |
| 25 | +const getLeadinConfig = () => { | |
| 26 | + return { | |
| 27 | + leadinPluginVersion, | |
| 28 | + }; | |
| 29 | +}; | |
| 30 | + | |
| 31 | +export const getOrCreateBackgroundApp = (accessToken = '', expiresIn = 0) => { | |
| 23 | 32 | if ((window as any).LeadinBackgroundApp) { |
| 24 | 33 | return (window as any).LeadinBackgroundApp; |
| 25 | 34 | } |
| 35 | + if (!isEmbedderReady()) { | |
| 36 | + return null; | |
| 37 | + } | |
| 26 | 38 | const { IntegratedAppEmbedder, IntegratedAppOptions }: any = window; |
| 27 | 39 | const options = new IntegratedAppOptions() |
| 28 | 40 | .setLocale(locale) |
| 29 | 41 | .setDeviceId(deviceId) |
| 30 | - .setRefreshToken(refreshToken); | |
| 42 | + .setLeadinConfig(getLeadinConfig()) | |
| 43 | + .setAccessToken(accessToken, expiresIn); | |
| 31 | 44 | |
| 32 | 45 | const embedder = new IntegratedAppEmbedder( |
| 33 | 46 | 'integrated-plugin-proxy', |
| 34 | 47 | portalId, |
| @@ -36,9 +49,10 @@ | ||
| 36 | 49 | () => {} |
| 37 | 50 | ).setOptions(options); |
| 38 | 51 | |
| 39 | 52 | embedder.attachTo(document.body, false); |
| 40 | - embedder.postStartAppMessage(); // lets the app know all all data has been passed to it | |
| 53 | + embedder.setTokenRenewalCallback(fetchAccessToken); | |
| 54 | + embedder.postStartAppMessage(); // lets the app know all data has been passed to it | |
| 41 | 55 | |
| 42 | 56 | (window as any).LeadinBackgroundApp = embedder; |
| 43 | 57 | return (window as any).LeadinBackgroundApp; |
| 44 | 58 | }; |