Skip to content

Commit

Permalink
Move order initializer logic to dropins.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferri0 committed Sep 17, 2024
1 parent ca86a23 commit c9541fa
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 76 deletions.

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion blocks/commerce-search-order/commerce-search-order.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.commerce-search-order-container {
max-width: 700px;
margin: auto;
}
65 changes: 65 additions & 0 deletions scripts/dropins.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit c9541fa

Please sign in to comment.