-
Notifications
You must be signed in to change notification settings - Fork 0
tri review #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: staging
Are you sure you want to change the base?
tri review #68
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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(){ | ||
| 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 = () => {}) { | ||
|
|
@@ -45,21 +50,19 @@ export default class Cmp extends ConsentString { | |
| callback(result, true); | ||
| } | ||
|
|
||
| showConsentTool() { | ||
| async showConsentTool() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please explain the changes here?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the async is not really important, i just forgot to delete this |
||
| 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) { | ||
|
|
||
| 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) { | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again please can you explain the benefits of the suggested approach?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
| 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
| 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)); | ||
| // }); | ||
| } |
| 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' | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But we need to try init() again?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: |
||
|
|
||
|
|
||
| // 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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels less readable, can you explain the benefit?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree |
||
|
|
||
| tagManagerModule(cmp.getPurposesAllowed()); | ||
|
|
||
| } | ||
| init(); | ||
|
|
||
| init().catch(e => console.log(e)); | ||
There was a problem hiding this comment.
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?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.