|
| 1 | +/* eslint-disable indent */ |
| 2 | +import { addQueryParams } from './utils'; |
| 3 | +import { RECOM_TOKEN_PARAM, NUM_OF_PRODUCTS } from './constants'; |
| 4 | + |
| 5 | +function renderPrice(node, themeSettings) { |
| 6 | + const { price, retailPrice } = node.prices || { price: {} }; |
| 7 | + return ` |
| 8 | + <div class="price-section price-section--withoutTax rrp-price--withoutTax"${!retailPrice ? ' style="display: none;"' : ''}> |
| 9 | + ${themeSettings['pdp-retail-price-label']} |
| 10 | + <span data-product-rrp-price-without-tax class="price price--rrp"> |
| 11 | + ${retailPrice ? `${retailPrice.value} ${retailPrice.currencyCode}` : ''} |
| 12 | + </span> |
| 13 | + </div> |
| 14 | + <div class="price-section price-section--withoutTax"> |
| 15 | + <span class="price-label"> |
| 16 | + ${themeSettings['pdp-price-label']} |
| 17 | + </span> |
| 18 | + <span data-product-price-without-tax class="price price--withoutTax">${price.value} ${price.currencyCode}</span> |
| 19 | + </div> |
| 20 | + `; |
| 21 | +} |
| 22 | + |
| 23 | +function renderRestrictToLogin() { |
| 24 | + return '<p translate>Log in for pricing</p>'; |
| 25 | +} |
| 26 | + |
| 27 | +function renderCard(node, options) { |
| 28 | + const { themeSettings, attributionToken } = options; |
| 29 | + const categories = node.categories.edges.map(({ node: cNode }) => cNode.name).join(','); |
| 30 | + const productUrl = addQueryParams(node.path, { [RECOM_TOKEN_PARAM]: attributionToken }); |
| 31 | + const addToCartUrl = addQueryParams(node.addToCartUrl, { [RECOM_TOKEN_PARAM]: attributionToken }); |
| 32 | + |
| 33 | + return `<div class="productCarousel-slide"> |
| 34 | + <article |
| 35 | + class="card" |
| 36 | + data-entity-id="${node.entityId}" |
| 37 | + data-event-type="" |
| 38 | + data-name="${node.name}" |
| 39 | + data-product-brand="${node.brand && node.brand.name ? node.brand.name : ''}" |
| 40 | + data-product-price="${node.prices && node.prices.price.value}" |
| 41 | + data-product-category="${categories}" |
| 42 | + data-position="" |
| 43 | + > |
| 44 | + <figure class="card-figure"> |
| 45 | + <a href="${productUrl}" data-event-type="product-click"> |
| 46 | + <div class="card-img-container"> |
| 47 | + ${node.defaultImage ? |
| 48 | + `<img |
| 49 | + src="${node.defaultImage.urlOriginal}" |
| 50 | + alt="${node.name}" |
| 51 | + title="${node.name}" |
| 52 | + data-sizes="auto" |
| 53 | + srcset-bak="" |
| 54 | + class="card-image${themeSettings.lazyload_mode ? ' lazyload' : ''}" |
| 55 | + />` : '' |
| 56 | + } |
| 57 | + |
| 58 | + </div> |
| 59 | + </a> |
| 60 | + <figcaption class="card-figcaption"> |
| 61 | + <div class="card-figcaption-body"> |
| 62 | + ${themeSettings.show_product_quick_view |
| 63 | + ? `<a class="button button--small card-figcaption-button quickview" |
| 64 | + data-product-id="${node.entityId} |
| 65 | + data-event-type="product-click" |
| 66 | + >Quick view</a>` |
| 67 | + : ''} |
| 68 | + <a href="${addToCartUrl}" data-event-type="product-click" class="button button--small card-figcaption-button">Add to Cart</a> |
| 69 | + </div> |
| 70 | + </figcaption> |
| 71 | + </figure> |
| 72 | + <div class="card-body"> |
| 73 | + ${node.brand && node.brand.name ? `<p class="card-text" data-test-info-type="brandName">${node.brand.name}</p>` : ''} |
| 74 | + <h4 class="card-title"> |
| 75 | + <a href="${productUrl}" data-event-type="product-click">${node.name}</a> |
| 76 | + </h4> |
| 77 | + <div class="card-text" data-test-info-type="price"> |
| 78 | + ${themeSettings.restrict_to_login ? renderRestrictToLogin() : renderPrice(node, themeSettings)} |
| 79 | + </div> |
| 80 | + </div> |
| 81 | + </article> |
| 82 | + </div>`; |
| 83 | +} |
| 84 | + |
| 85 | +function createFallbackContainer(carousel) { |
| 86 | + const container = $('[itemscope] > .tabs-contents'); |
| 87 | + const tabs = $('[itemscope] > .tabs'); |
| 88 | + tabs.html(` |
| 89 | + <li class="tab is-active" role="presentational"> |
| 90 | + <a class="tab-title" href="#tab-related" role="tab" tabindex="0" aria-selected="true" controls="tab-related">Related products</a> |
| 91 | + </li> |
| 92 | + `); |
| 93 | + container.html(` |
| 94 | + <div role="tabpanel" aria-hidden="false" class="tab-content has-jsContent is-active recommendations" id="tab-related"> |
| 95 | + ${carousel} |
| 96 | + </div> |
| 97 | + `); |
| 98 | +} |
| 99 | + |
| 100 | +export default function injectRecommendations(products, el, options) { |
| 101 | + const cards = products |
| 102 | + .slice(0, NUM_OF_PRODUCTS) |
| 103 | + .map((product) => renderCard(product, options)) |
| 104 | + .join(''); |
| 105 | + |
| 106 | + const carousel = ` |
| 107 | + <section |
| 108 | + class="productCarousel" |
| 109 | + data-list-name="Recommended Products" |
| 110 | + data-slick='{ |
| 111 | + "dots": true, |
| 112 | + "infinite": false, |
| 113 | + "mobileFirst": true, |
| 114 | + "slidesToShow": 2, |
| 115 | + "slidesToScroll": 2, |
| 116 | + "responsive": [ |
| 117 | + { |
| 118 | + "breakpoint": 800, |
| 119 | + "settings": { |
| 120 | + "slidesToShow": ${NUM_OF_PRODUCTS}, |
| 121 | + "slidesToScroll": 3 |
| 122 | + } |
| 123 | + }, |
| 124 | + { |
| 125 | + "breakpoint": 550, |
| 126 | + "settings": { |
| 127 | + "slidesToShow": 3, |
| 128 | + "slidesToScroll": 3 |
| 129 | + } |
| 130 | + } |
| 131 | + ] |
| 132 | + }' |
| 133 | + > |
| 134 | + ${cards} |
| 135 | + </section>`; |
| 136 | + // eslint-disable-next-line no-param-reassign |
| 137 | + if (!el.get(0)) { |
| 138 | + createFallbackContainer(carousel); |
| 139 | + } else { |
| 140 | + el.html(carousel); |
| 141 | + } |
| 142 | +} |
0 commit comments