Skip to content

Commit

Permalink
Update pushDataLayer to check logged in state of user (#16561)
Browse files Browse the repository at this point in the history
* chore: update pushDataLayer to check logged in state of user

* Update packages/core/src/Stores/gtm-store.js

Co-authored-by: Ali(Ako) Hosseini <[email protected]>

* chore: callbacks to async/await

* chore: add optional chaining

---------

Co-authored-by: Ali(Ako) Hosseini <[email protected]>
  • Loading branch information
shahzaib-deriv and ali-hosseini-deriv authored Oct 16, 2024
1 parent 2990e29 commit b3c2b8e
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ const IdvFailed = ({

React.useEffect(() => {
const initializeFormValues = async (required_fields: string[]) => {
await WS.wait('get_settings');
await WS?.wait('get_settings');
const form_data = filterObjProperties(account_settings, required_fields);
if (form_data.date_of_birth) {
form_data.date_of_birth = toMoment(form_data.date_of_birth).format('YYYY-MM-DD');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ const PoiConfirmWithExampleFormContainer = ({
const side_note_image = <DerivLightNameDobPoiIcon height='195px' width='285px' />;

React.useEffect(() => {
const initializeFormValues = () => {
WS.wait('get_settings').then(() => {
const initializeFormValues = async () => {
try {
await WS?.wait('get_settings');
const visible_settings = ['first_name', 'last_name', 'date_of_birth'];
const form_initial_values = filterObjProperties(account_settings, visible_settings);
if (form_initial_values.date_of_birth) {
Expand All @@ -69,7 +70,10 @@ const PoiConfirmWithExampleFormContainer = ({
form_initial_values,
});
setIsLoading(false);
});
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
}
};

initializeFormValues();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,9 @@ const FinancialAssessment = observer(() => {
setIsLoading(false);
history.push(routes.personal_details);
} else {
WS.authorized.storage.getFinancialAssessment().then((data: GetFinancialAssessmentResponse) => {
WS.wait('get_account_status').then(() => {
WS.authorized.storage.getFinancialAssessment().then(async (data: GetFinancialAssessmentResponse) => {
try {
await WS.wait('get_account_status');
setHasTradingExperience(
(is_financial_account || is_trading_experience_incomplete) && !is_svg && !is_mf
);
Expand All @@ -255,7 +256,10 @@ const FinancialAssessment = observer(() => {
}
if (data?.get_financial_assessment) setInitialFormValues(data.get_financial_assessment);
setIsLoading(false);
});
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const ProofOfAddressForm = observer(
const { localize } = useTranslations();

React.useEffect(() => {
fetchResidenceList?.().then(() => {
fetchResidenceList?.().then(async () => {
Promise.all([fetchStatesList(), WS.wait('get_settings')]).then(() => {
setFormValues({
address_line_1: account_settings.address_line_1 ?? '',
Expand Down
12 changes: 9 additions & 3 deletions packages/cashier/src/pages/account-transfer/account-transfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,15 @@ const AccountTransfer = observer(({ onClickDeposit, onClickNotes, onClose }: TAc

React.useEffect(() => {
onMount();
WS.wait('authorize', 'website_status', 'get_settings', 'paymentagent_list').then(() => {
setIsLoadingStatus(false);
});
(async () => {
try {
await WS.wait('authorize', 'website_status', 'get_settings', 'paymentagent_list');
setIsLoadingStatus(false);
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
}
})();

return () => {
setAccountTransferAmount('');
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Services/socket-general.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ const BinarySocketGeneral = (() => {
};

const setBalanceActiveAccount = flow(function* (obj_balance) {
yield BinarySocket.wait('website_status');
yield BinarySocket?.wait('website_status');
client_store.setBalanceActiveAccount(obj_balance);
});

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/Stores/client-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -1828,7 +1828,7 @@ export default class ClientStore extends BaseStore {
}

broadcastAccountChangeAfterAuthorize() {
return BinarySocket.wait('authorize').then(() => {
return BinarySocket?.wait('authorize')?.then(() => {
this.broadcastAccountChange();
});
}
Expand Down Expand Up @@ -1879,7 +1879,7 @@ export default class ClientStore extends BaseStore {

if (should_switch_socket_connection) {
BinarySocket.closeAndOpenNewConnection();
await BinarySocket.wait('authorize');
await BinarySocket?.wait('authorize');
} else {
await WS.forgetAll('balance');
await BinarySocket.authorize(this.getToken());
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Stores/common-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export default class CommonStore extends BaseStore {
if (key === 'EN') {
window.localStorage.setItem('i18n_language', key);
}
await WS.wait('authorize');
await WS?.wait('authorize');
return new Promise((resolve, reject) => {
WS.setSettings({
set_settings: 1,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Stores/contract-replay-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default class ContractReplayStore extends BaseStore {
};

subscribeProposalOpenContract = () => {
WS.wait('authorize').then(() => {
WS?.wait('authorize')?.then(() => {
this.handleSubscribeProposalOpenContract(this.contract_id, this.populateConfig);
});
};
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/Stores/gtm-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export default class GTMStore extends BaseStore {
* @param {object} data
*/
async pushDataLayer(data) {
if (this.is_gtm_applicable) {
BinarySocket.wait('authorize').then(() => {
if (this.is_gtm_applicable && this.root_store?.client?.is_logged_in) {
BinarySocket?.wait('authorize')?.then(() => {
const gtm_object = { ...this.common_variables, ...data };
if (!gtm_object.event) return;

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/_common/base/socket_base.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ const proxyForAuthorize = obj =>
if (target[field] && typeof target[field] !== 'function') {
return proxyForAuthorize(target[field]);
}
return (...args) => BinarySocketBase?.wait('authorize').then(() => target[field](...args));
return (...args) => BinarySocketBase?.wait('authorize')?.then(() => target[field](...args));
},
});

Expand Down

0 comments on commit b3c2b8e

Please sign in to comment.