Skip to content
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

Minor fix for sku reading from URL #651

Merged
merged 3 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions blocks/product-resources/product-resources.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable import/no-unresolved */
import { getMetadata, loadScript } from '../../scripts/lib-franklin.js';
import { getCookie, isOTEnabled, getProductResponse } from '../../scripts/scripts.js';
import { loadScript } from '../../scripts/lib-franklin.js';
import {
getCookie, isOTEnabled, getProductResponse, getSKU,
} from '../../scripts/scripts.js';

const productResources = `
<atomic-search-interface class="resource-search"
Expand Down Expand Up @@ -156,7 +158,7 @@ const productResources = `
`;

export default async function decorate(block) {
const sku = getMetadata('sku');
const sku = getSKU();
const host = (window.location.host === 'lifesciences.danaher.com') ? window.location.host : 'stage.lifesciences.danaher.com';
const response = getProductResponse();
if (response?.length > 0 && response[0]?.raw?.objecttype === 'Family' && response[0]?.raw?.numresources > 0) {
Expand Down
9 changes: 9 additions & 0 deletions scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,15 @@ export function getProductResponse() {
return JSON.parse(localStorage.getItem('product-details'));
}

/**
*
* @returns Product SKU from requested URL
*/
export function getSKU() {
const sku = window.location.pathname.replace(/^\/content\/danaher\/ls\/us\/en\/products\//, '').replace(/\.html$/, '').split('/');
return sku.pop();
}

/**
* Set the content of a cookie
* @param {string} cname The cookie name (or property)
Expand Down
9 changes: 4 additions & 5 deletions templates/productDetail/productDetail.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { getMetadata } from '../../scripts/lib-franklin.js';
import { getProductResponse, makeCoveoApiRequest } from '../../scripts/scripts.js';
import { getProductResponse, getSKU, makeCoveoApiRequest } from '../../scripts/scripts.js';

function getCoveoApiPayload(qParam) {
const sku = getMetadata('sku');
const sku = getSKU();
const payload = {
context: {
host: 'stage.lifesciences.danaher.com',
Expand All @@ -15,10 +14,10 @@ function getCoveoApiPayload(qParam) {
}

export default async function buildAutoBlocks() {
const sku = getMetadata('sku');
const sku = getSKU();
let response = getProductResponse();
try {
if (response && response.at(0)?.raw.sku !== sku) {
if (!response || response.at(0)?.raw.sku !== sku) {
response = await makeCoveoApiRequest('/rest/search/v2', 'productKey', getCoveoApiPayload('productid'));
if (response.results.length > 0) {
localStorage.setItem('product-details', JSON.stringify(response.results));
Expand Down