Skip to content

Commit cb747a9

Browse files
authored
Scheduling initialization of the UI SDK (#8)
1 parent 5ac9cfb commit cb747a9

File tree

1 file changed

+27
-61
lines changed

1 file changed

+27
-61
lines changed

cartridges/int_reachfive_sfra/cartridge/templates/default/reachfiveinitglobal.isml

Lines changed: 27 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,28 @@
3939
</isif>
4040

4141
<isif condition="${pdict.loadUISDK && pdict.reachFiveUiSdkUrl}">
42-
<script src="${pdict.reachFiveUiSdkUrl}"></script>
43-
4442
<script>
43+
function loadScript(url) {
44+
return new Promise(function(resolve, reject) {
45+
var script = document.createElement("script");
46+
script.onload = resolve;
47+
script.onerror = reject;
48+
script.async = true;
49+
script.src = url;
50+
document.getElementsByTagName("head")[0].appendChild(script);
51+
});
52+
}
53+
54+
function loadSDKUI() {
55+
if (window.sdkUiClient) {
56+
// already loaded and ready to go
57+
return Promise.resolve();
58+
} else {
59+
return loadScript('${pdict.reachFiveUiSdkUrl}');
60+
}
61+
}
62+
63+
loadSDKUI().then(function() {
4564
window.sdkUiClient = reach5Widgets.createClient({
4665
// Required parameters
4766
domain: '${pdict.reachFiveDomain}',
@@ -50,9 +69,7 @@
5069
language: '${pdict.reachFiveLanguageCode}' || 'en',
5170
locale: '${pdict.reachFivelocaleCode}'
5271
});
53-
</script>
5472

55-
<script>
5673
let allowForgotPassword = ${!pdict.isTransitionActive};
5774
let redirectUri = '${pdict.callbackUrl}';
5875
let origin ='${pdict.siteID}';
@@ -88,39 +105,13 @@
88105
if (document.getElementById("reset-password")) {
89106
<isprint value="${dw.system.Site.getCurrent().getCustomPreferenceValue('reset-password')}" encoding="off">
90107
}
91-
</script>
92108

93-
<isif condition="${!empty(pdict.reachFiveAccess_token)}">
94-
<script>
109+
<isif condition="${!empty(pdict.reachFiveAccess_token)}">
95110
if(document.getElementById("password-editor")) {
96111
console.log('password-editor');
97112
<isprint value="${dw.system.Site.getCurrent().getCustomPreferenceValue('password-editor')}" encoding="off">
98113
}
99114

100-
// if(document.getElementById("profile-editor")) {
101-
// sdkUiClient.showProfileEditor({
102-
// container: 'profile-editor',
103-
// accessToken: '${pdict.reachFiveAccess_token}',
104-
// fields: [
105-
// 'given_name',
106-
// 'family_name',
107-
// 'birthdate',
108-
// 'consents.newsletter'
109-
// ],
110-
// onSuccess: function() {
111-
// var el = document.getElementsByTagName('body');
112-
// var event = new CustomEvent('reachfive-profile-update', {
113-
// bubbles: true,
114-
// detail: {
115-
// source: 'profileEditor',
116-
// updateUrl: decodeURIComponent('${dw.crypto.Encoding.toURI(pdict.updateProfileUrl)}')
117-
// }
118-
// });
119-
// el[0].dispatchEvent(event);
120-
// }
121-
// });
122-
// }
123-
124115
if(document.getElementById("email-editor")) {
125116
<isprint value="${dw.system.Site.getCurrent().getCustomPreferenceValue('email-editor')}" encoding="off">
126117
}
@@ -133,35 +124,10 @@
133124
if (socialAccountsContainer) {
134125
<isprint value="${dw.system.Site.getCurrent().getCustomPreferenceValue('social-accounts-container')}" encoding="off">
135126
}
136-
137-
</script>
138-
139-
<iscomment>
140-
TODO: This functionality related to "imlicid flow"
141-
and it is normally disabled becasue of security issues
142-
<script type="text/javascript">
143-
var reAuthContainer = document.getElementById("re-auth-container");
144-
var socialAccountsContainer = document.getElementById("social-accounts-container");
145-
146-
if(reAuthContainer && socialAccountsContainer) {
147-
var idToken = null;
148-
149-
try{
150-
idToken = '${pdict.CurrentSession.privacy.id_token}';
151-
} catch(err) {
152-
console.log(err)
153-
}
154-
155-
console.log('social-accounts-container');
156-
<isprint value="${dw.system.Site.getCurrent().getCustomPreferenceValue('re-auth-container')}" encoding="off">
157-
158-
sdkCoreClient.on('authenticated', function(authResult) {
159-
let accessToken = authResult.accessToken;
160-
<isprint value="${dw.system.Site.getCurrent().getCustomPreferenceValue('authenticated')}" encoding="off">
161-
});
162-
}
163-
</script>
164-
</iscomment>
165-
</isif>
127+
</isif>
128+
}, function(e) {
129+
throw new Error('Unable to load ReachFive SDK Core Client');
130+
});
131+
</script>
166132
</isif>
167133
</isif>

0 commit comments

Comments
 (0)