diff --git a/blocks/commerce-order-data-initializer/commerce-order-data-initializer.css b/blocks/commerce-order-data-initializer/commerce-order-data-initializer.css deleted file mode 100644 index f105bad6fa..0000000000 --- a/blocks/commerce-order-data-initializer/commerce-order-data-initializer.css +++ /dev/null @@ -1,3 +0,0 @@ -.commerce-order-data-initializer-wrapper { - display: none; -} diff --git a/blocks/commerce-order-data-initializer/commerce-order-data-initializer.js b/blocks/commerce-order-data-initializer/commerce-order-data-initializer.js deleted file mode 100644 index 2e2ba23993..0000000000 --- a/blocks/commerce-order-data-initializer/commerce-order-data-initializer.js +++ /dev/null @@ -1,72 +0,0 @@ -/* eslint-disable import/no-unresolved */ -/* eslint-disable import/no-extraneous-dependencies */ -import { initializers } from '@dropins/tools/initializer.js'; -import * as orderApi from '@dropins/storefront-order/api.js'; -import { events } from '@dropins/tools/event-bus.js'; -import { getCookie } from '../../scripts/configs.js'; -import { readBlockConfig } from '../../scripts/aem.js'; - -export default async function decorate(block) { - const { - 'allow-guest-access': allowGuestAccess = 'true', - } = readBlockConfig(block); - - const isAuthenticated = !!getCookie('auth_dropin_user_token'); - - const currentUrl = new URL(window.location.href); - const orderRef = currentUrl.searchParams.get('orderRef'); - const orderToken = orderRef && orderRef.length > 20 ? orderRef : null; - const orderNumber = orderRef && orderRef.length < 20 ? orderRef : null; - - if (!allowGuestAccess && !isAuthenticated) { - if (orderToken) { - window.location.href = `/order-details?orderRef=${orderToken}`; - } else if (orderNumber) { - window.location.href = `/order-status?orderRef=${orderNumber}`; - } else { - window.location.href = '/order-status'; - } - - return; - } - - events.on('order/error', () => { - const isAuthenticatedOnErrorEvent = !!getCookie('auth_dropin_user_token'); - - if (isAuthenticatedOnErrorEvent) { - window.location.href = '/customer/orders'; - } else { - window.location.href = '/order-status'; - } - }); - - events.on('order/data', (orderData) => { - if (!orderData) { - const isAuthenticatedOnDataEvent = !!getCookie('auth_dropin_user_token'); - - if (isAuthenticatedOnDataEvent) { - window.location.href = '/customer/orders'; - } else { - window.location.href = '/order-status'; - } - } - }); - - if (orderNumber && !isAuthenticated) { - window.location.href = `/order-status?orderRef=${orderNumber}`; - } else if (orderNumber && isAuthenticated) { - if (!window.location.href.includes(`/customer/order-details?orderRef=${orderNumber}`)) { - window.location.href = `/customer/order-details?orderRef=${orderNumber}`; - } else { - initializers.register(orderApi.initialize, { - orderNumber, - }); - } - } - - if (orderToken) { - initializers.register(orderApi.initialize, { - orderToken, - }); - } -} diff --git a/blocks/commerce-search-order/commerce-search-order.css b/blocks/commerce-search-order/commerce-search-order.css index d0fc79f68e..3d2b4d30a1 100644 --- a/blocks/commerce-search-order/commerce-search-order.css +++ b/blocks/commerce-search-order/commerce-search-order.css @@ -1,4 +1,3 @@ .commerce-search-order-container { - max-width: 700px; margin: auto; } diff --git a/scripts/dropins.js b/scripts/dropins.js index 43300afcf9..d866b433f4 100644 --- a/scripts/dropins.js +++ b/scripts/dropins.js @@ -12,12 +12,14 @@ import { initializers, Initializer } from '@dropins/tools/initializer.js'; // Drop-ins import * as authApi from '@dropins/storefront-auth/api.js'; import * as cartApi from '@dropins/storefront-cart/api.js'; +import * as orderApi from '@dropins/storefront-order/api.js'; // Recaptcha import * as recaptcha from '@dropins/tools/recaptcha.js'; // Libs import { getConfigValue, getCookie } from './configs.js'; +import { getMetadata } from './aem.js'; export const getUserTokenCookie = () => getCookie('auth_dropin_user_token'); @@ -62,6 +64,69 @@ export default async function initializeDropins() { initializers.register(authApi.initialize, {}); initializers.register(cartApi.initialize, {}); + // Get current page template metadata + const templateMeta = getMetadata('template'); + const isOrderPage = templateMeta.includes('Order'); + + if (isOrderPage) { + const isAuthenticated = !!getCookie('auth_dropin_user_token'); + + const currentUrl = new URL(window.location.href); + const orderRef = currentUrl.searchParams.get('orderRef'); + const orderToken = orderRef && orderRef.length > 20 ? orderRef : null; + const orderNumber = orderRef && orderRef.length < 20 ? orderRef : null; + + if (!isAuthenticated) { + if (orderToken) { + window.location.href = `/order-details?orderRef=${orderToken}`; + } else if (orderNumber) { + window.location.href = `/order-status?orderRef=${orderNumber}`; + } else { + window.location.href = '/order-status'; + } + } else { + events.on('order/error', () => { + const isAuthenticatedOnErrorEvent = !!getCookie('auth_dropin_user_token'); + + if (isAuthenticatedOnErrorEvent) { + window.location.href = '/customer/orders'; + } else { + window.location.href = '/order-status'; + } + }); + + events.on('order/data', (orderData) => { + if (!orderData) { + const isAuthenticatedOnDataEvent = !!getCookie('auth_dropin_user_token'); + + if (isAuthenticatedOnDataEvent) { + window.location.href = '/customer/orders'; + } else { + window.location.href = '/order-status'; + } + } + }); + + if (orderNumber && !isAuthenticated) { + window.location.href = `/order-status?orderRef=${orderNumber}`; + } else if (orderNumber && isAuthenticated) { + if (!window.location.href.includes(`/customer/order-details?orderRef=${orderNumber}`)) { + window.location.href = `/customer/order-details?orderRef=${orderNumber}`; + } else { + initializers.register(orderApi.initialize, { + orderRef: orderNumber, + }); + } + } + + if (orderToken) { + initializers.register(orderApi.initialize, { + orderRef: orderToken, + }); + } + } + } + const mount = async () => { // Event Bus Logger events.enableLogger(true);