Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions src/cmp/Cmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,25 @@ import tagManagerModule from './tagManager';
export default class Cmp extends ConsentString {
constructor(result = null) {
super(result.iabCookie);
this.clientId = result.clientId;
}

// will be usefull when all data are retrieved async
async _initialize(){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why this will be useful?
What is difference between the constructor and the initialise function?

Copy link
Author

@juji juji Aug 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, all data may be retrieved asynchronously. Just setting things up.

this.setCmpId(52);
this.setCmpVersion(1);
this.setConsentLanguage('en');
this.setConsentScreen(1);
this.setGlobalVendorList(iabVendorList);
this.clientId = result.clientId;
this.cmpLoaded = false;
this.customVendorsAllowed = getCustomVendorsAllowed();
}

readyCmpAPI() {
this.cmpLoaded = true;
console.log(`[INFO][Module-CMP]: CMP loaded status is => ${this.cmpLoaded}`);
Promise.resolve();
// doesn't even return anything
// Promise.resolve();
}

ping(empty = null, callback = () => {}) {
Expand All @@ -45,21 +50,19 @@ export default class Cmp extends ConsentString {
callback(result, true);
}

showConsentTool() {
async showConsentTool() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain the changes here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the async is not really important, i just forgot to delete this change.
The real changes are the ones below this line.

console.log('[INFO][CMP-Module] showConsentTool() has been called.');
return new Promise((resolve, reject) => {
return import(
/* webpackMode: "lazy",
webpackPrefetch: true,
webpackChunkName: "ui" */
'../ui/main')
.then(appModule => appModule.default(this.clientId))
.then(userConsentObject => this.updateCmpAndWriteCookie(userConsentObject))
.then(() => tagManagerModule())
.then(() => cookies.requestHttpCookies('euconsent', this.getConsentString()))
.then(result => Promise.resolve(result))
.catch(err => console.error(err));
});
return import(
/* webpackMode: "lazy",
webpackPrefetch: true,
webpackChunkName: "ui" */
'../ui/main')
.then(appModule => appModule.default(this.clientId))
.then(userConsentObject => this.updateCmpAndWriteCookie(userConsentObject))
.then(() => tagManagerModule(this.getPurposesAllowed()))
.then(() => cookies.requestHttpCookies('euconsent', this.getConsentString()))
.then(result => result)
.catch(err => console.error(err));
}

setCustomVendorsAllowed(customVendorArray) {
Expand Down
15 changes: 8 additions & 7 deletions src/cmp/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import Cmp from './Cmp';

export default function initCmp(loaderData) {
export default async function initCmp(loaderData) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again please can you explain the benefits of the suggested approach?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better readability. Assuming we know how async/await works

console.log('[INFO][Module-Loader]: ', loaderData);
return new Promise((resolve, reject) => {
const cmp = new Cmp(loaderData);
if (!cmp) reject();
console.log('[INFO][Module-CMP]: ', cmp);
resolve(cmp);
});
const cmp = new Cmp(loaderData);
if (!cmp) throw 'cmp is NULL';

console.log('[INFO][Module-CMP]: ', cmp);
await cmp._initialize();
return cmp;
}
11 changes: 8 additions & 3 deletions src/cmp/isShowUi.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
export default function (cookie) {
const isCookiePresent = (typeof cookie === 'string');
console.log('[INFO][Module-isShowUi]: ', !isCookiePresent);
return Promise.resolve(!isCookiePresent);
// make it sync
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we make it sync, it cannot be used in a promise chain right?

Copy link
Author

@juji juji Aug 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes.. but by nature, it is not async. so we dont have to make it return promise.

edit: actually, we can use it in promise chain. but not on the start of chain.

// wont make a difference to be async
return !(typeof cookie === 'string')

// const isCookiePresent = (typeof cookie === 'string');
// console.log('[INFO][Module-isShowUi]: ', !isCookiePresent);

// !isCookiePresent;
}
3 changes: 1 addition & 2 deletions src/cmp/tagManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ function updateConsentCookies(purposeArray) {
}
}

export default function tagManagerModule() {
export default function tagManagerModule(purposeArray) {
return new Promise((resolve, reject) => {
const purposeArray = cmp.getPurposesAllowed();
if (purposeArray === undefined || purposeArray.length == 0) {
console.log('[INFO][Module-TMS] => No user consented purposes');
resolve(false);
Expand Down
14 changes: 9 additions & 5 deletions src/loader/utils/getClientId.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
export default function getClientID() {
return new Promise((resolve, reject) => {
const scriptElement = document.getElementById('pluto-cmp-js-src');
const clientId = (scriptElement) ? scriptElement.getAttribute('client-id') : 0;
resolve(parseInt(clientId));
});

const scriptElement = document.getElementById('pluto-cmp-js-src');
return (scriptElement) ? scriptElement.getAttribute('client-id') : 0;

// return new Promise((resolve, reject) => {
// const scriptElement = document.getElementById('pluto-cmp-js-src');
// const clientId = (scriptElement) ? scriptElement.getAttribute('client-id') : 0;
// resolve(parseInt(clientId));
// });
}
8 changes: 3 additions & 5 deletions src/loader/utils/isDataLayer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// renameBug..
export default function isDataLayer() {
return new Promise((resolve, reject) => {
const result = (typeof dataLayer !== 'undefined') ? true : false;
resolve(result);
});
// use async if this will be promise in the future
export default async function isDataLayer() {
return typeof dataLayer !== 'undefined'
}
31 changes: 20 additions & 11 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,26 @@ import isShowUi from './cmp/isShowUi';
import tagManagerModule from './cmp/tagManager';

async function init() {

// prevent this script from running without window object
// e.g: SSR
if(typeof window === 'undefined') return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we need to try init() again?

Copy link
Author

@juji juji Aug 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, in the browser.

For certain library (anything with node.js + SSR), Javascript are run in the server. So if this is run in the server, there will be no ERROR saying: cannot find {property} of undefined, or something like that



// in async, use try catch instead
// or catch on execution

const loaderData = await initLoader();
const cmp = await initCmp(loaderData);
window.cmp = cmp; // TODO: remove this it should not be set globally
initApi(cmp)
.then(() => isShowUi(loaderData.iabCookie))
.then((bool) => {
if (bool) cmp.showConsentTool();
Promise.resolve(true);
})
.then(result => cmp.readyCmpAPI(result))
.then(() => tagManagerModule())
.catch(err => console.error(err));

await initApi(cmp);
const result = isShowUi(loaderData.iabCookie) ? await cmp.showConsentTool() : true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels less readable, can you explain the benefit?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i just like one-liner.


// why do we need window.__cpm ?
cmp.readyCmpAPI(result);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree window is not good to attach to, but it is to stop having to import the cmp object in all files, maybe it should be created and exported elsewhere?


tagManagerModule(cmp.getPurposesAllowed());

}
init();

init().catch(e => console.log(e));
24 changes: 10 additions & 14 deletions src/ui/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,16 @@ const vm = new Vue(App).$mount('#cmp-app');

// this function is called to load the UI, it accepts the clientId
function renderVueApp(clientId) {
return new Promise((resolve, reject) => {
if (vm) {
vm.$store.commit('setClientId', parseInt(clientId, 10));
vm.$store.dispatch('setClientId', parseInt(clientId, 10));
vm.$store.commit('changeShowState', true);
EventBus.$on('save-selection', (value) => {
console.log('[INFO][CMP-UI] Resolving Promise (save-selection):', value);
resolve(value);
});
} else {
console.error('CMP-UI :: No App Present');
reject();
}
});
if(!vm) return Promise.reject(new Error('CMP-UI :: No App Present'));

vm.$store.commit('setClientId', parseInt(clientId, 10));
vm.$store.dispatch('setClientId', parseInt(clientId, 10));
vm.$store.commit('changeShowState', true);

return new Promise((resolve, reject) => EventBus.$on('save-selection', (value) => {
console.log('[INFO][CMP-UI] Resolving Promise (save-selection):', value);
resolve(value);
}));
}

export default renderVueApp;
Loading