Skip to content

Commit

Permalink
eds-400 updated template and fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
KostiantynFandeliuk committed Sep 18, 2024
1 parent e32c62f commit cc9870c
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 140 deletions.
90 changes: 15 additions & 75 deletions blocks/commerce-search-order/commerce-search-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,26 @@ import { render as orderRenderer } from '@dropins/storefront-order/render.js';
import { getCookie } from '../../scripts/configs.js';

const renderSignIn = async (element, email, orderNumber) => authRenderer.render(SignIn, {
initialEmailValue: email,
renderSignUpLink: false,
labels: {
formTitleText: email
? 'Enter your password to view order details'
: 'Sign in to view order details',
primaryButtonText: 'View order',
},
routeForgotPassword: () => 'reset-password.html',
routeRedirectOnSignIn: () => `/customer/order-details?orderRef=${orderNumber}`,
})(element);
initialEmailValue: email,
renderSignUpLink: false,
labels: {
formTitleText: email
? 'Enter your password to view order details'
: 'Sign in to view order details',
primaryButtonText: 'View order',
},
routeForgotPassword: () => 'reset-password.html',
routeRedirectOnSignIn: () => `/customer/order-details?orderRef=${orderNumber}`,
})(element);

export default async function decorate(block) {
const isAuthenticated = !!getCookie('auth_dropin_user_token') || false;

await orderRenderer.render(OrderSearch, {
isAuth: isAuthenticated,
renderSignIn: async ({ render, formValues }) => {
console.log('render', render);
if (render) {
const { email, number } = formValues;

renderSignIn(block, email, number);
renderSignIn(block, formValues?.email ?? '', formValues?.number ?? '');

return false;
}
Expand All @@ -38,65 +35,8 @@ export default async function decorate(block) {
},
routeCustomerOrderDetails: () => '/customer/order-details',
routeOrderDetails: () => '/order-details',
onError: async (errorInformation) => {},
onError: async (errorInformation) => {
console.info('errorInformation', errorInformation);
},
})(block);

// const checkIsOrderBelongsToCustomer = async (orderEmail) => {
// try {
// const customerData = await authApi.getCustomerData();
// return customerData.email === orderEmail;
// } catch {
// return false;
// }
// };

// events.on('order/data', async (orderData) => {
// const isAuthenticatedOnDataEvent = !!getCookie('auth_dropin_user_token');

// if (isAuthenticatedOnDataEvent) {
// const isOrderBelongsToCustomer = await checkIsOrderBelongsToCustomer(orderData.email);

// if (isOrderBelongsToCustomer) {
// window.location.href = `/customer/order-details?orderRef=${orderData.number}`;
// } else if (!orderToken) {
// window.location.href = `/order-details?orderRef=${orderData.token}`;
// }
// } else if (!orderToken) {
// window.location.href = `/order-details?orderRef=${orderData.token}`;
// }
// });

// if (orderToken) {
// window.location.href = `/order-details?orderRef=${orderToken}`;
// } else if (orderNumber) {
// if (isAuthenticated) {
// window.location.href = `/customer/order-details?orderRef=${orderNumber}`;
// } else {
// await renderSignIn(block, '', orderNumber);
// }
// } else {
// await orderRenderer.render(OrderSearch, {
// onError: async (errorInformation) => {
// const { error, formValues } = errorInformation;

// if (error === 'Please login to view the order.') {
// const { email, number } = formValues;

// const isOrderBelongsToCustomer = await checkIsOrderBelongsToCustomer(email);

// if (isOrderBelongsToCustomer) {
// window.location.href = `/customer/order-details?orderRef=${number}`;
// } else {
// // TODO add url param without reload orderRef - number
// await renderSignIn(block, email, number);
// }

// // Hide error message
// return false;
// }

// return true;
// },
// })(block);
// }
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@ export interface inLineAlertProps {
text: string;
type: 'success' | 'warning' | 'error';
}
type RouteSignInProps = {
render: boolean;
formValues?: {
number: number | string;
email?: string;
};
};
export interface OrderSearchProps {
className?: string;
onError?: (errorInformation: errorInformationProps) => boolean | Promise<boolean> | undefined;
isAuth: boolean;
routeSignIn: () => string;
renderSignIn: ({ render, formValues }: RouteSignInProps) => boolean;
routeCustomerOrderDetails: () => string;
routeOrderDetails: () => string;
}
export interface useOrderSearchProps {
onError?: (errorInformation: errorInformationProps) => boolean | Promise<boolean> | undefined;
isAuth?: boolean;
routeSignIn?: () => string;
renderSignIn: ({ render, formValues }: RouteSignInProps) => boolean;
routeCustomerOrderDetails?: () => string;
routeOrderDetails?: () => string;
}
Expand Down
137 changes: 75 additions & 62 deletions scripts/dropins.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,80 @@ import { getMetadata } from './aem.js';

export const getUserTokenCookie = () => getCookie('auth_dropin_user_token');

const helderInitializers = (orderRef) => {
initializers.register(orderApi.initialize, {
orderRef,
});
};

const redirectTo = (path) => {
window.location.href = path;
};

const handleUserOrdersRedirects = () => {
// Get current page template metadata
const currentUrl = new URL(window.location.href);
const templateMeta = getMetadata('template');
const isOrderPage = templateMeta.includes('Order');
const isAccountPage = currentUrl.pathname.includes('/customer');
const isAuthenticated = !!getCookie('auth_dropin_user_token') ?? false;

const orderRef = currentUrl.searchParams.get('orderRef');
const isToken = orderRef && orderRef.length > 20 ? orderRef : null;

const ORDER_STATUS_PATH = '/order-status';
const ORDER_DETAILS_PATH = '/order-details';
const CUSTOMER_ORDER_DETAILS_PATH = '/customer/order-details';
const CUSTOMER_ORDERS_PATH = '/customer/orders';
const ORDER_REF_URL_QUERY = `?orderRef=${orderRef}`;

if (isOrderPage) {
let targetPath = null;
if (currentUrl.pathname.includes(CUSTOMER_ORDERS_PATH)) {
return;
}

events.on('order/error', ({ error }) => {
const defaultErrorMessage =

Check failure on line 60 in scripts/dropins.js

View workflow job for this annotation

GitHub Actions / build

There should be no line break before or after '='
"We couldn't locate an order with the information provided.";

if (error.includes(defaultErrorMessage)) {
window.location.href = `${ORDER_STATUS_PATH}${ORDER_REF_URL_QUERY}`;
} else if (isAuthenticated) {
window.location.href = `${CUSTOMER_ORDERS_PATH}`;
} else {
window.location.href = `${ORDER_STATUS_PATH}`;
}
});

if (isAuthenticated) {
if (!orderRef) {
targetPath = CUSTOMER_ORDERS_PATH;
} else if (isAccountPage) {
if (isToken) {
targetPath = `${ORDER_DETAILS_PATH}${ORDER_REF_URL_QUERY}`;
} else {
helderInitializers(orderRef);
}
} else if (isToken) {
helderInitializers(orderRef);
} else {
targetPath = `${CUSTOMER_ORDER_DETAILS_PATH}${ORDER_REF_URL_QUERY}`;
}
} else if (!orderRef) {
targetPath = ORDER_STATUS_PATH;
} else if (isToken) {
helderInitializers(orderRef);
} else {
targetPath = `${ORDER_STATUS_PATH}${ORDER_REF_URL_QUERY}`;
}

if (targetPath) {
redirectTo(targetPath);
}
}
};

// Update auth headers
const setAuthHeaders = (state) => {
if (state) {
Expand Down Expand Up @@ -64,68 +138,7 @@ 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,
});
}
}
}
handleUserOrdersRedirects();

const mount = async () => {
// Event Bus Logger
Expand Down

0 comments on commit cc9870c

Please sign in to comment.