From 38d315784d89e7c7e49d11b7717eec6fe455f581 Mon Sep 17 00:00:00 2001 From: Andrei Bogdan Date: Thu, 22 Feb 2024 12:12:33 +0200 Subject: [PATCH 1/4] Add price, oldPrice --- solutions/blocks/hero/hero.js | 111 +++++++++++++++++++++++++- solutions/blocks/products/products.js | 5 +- 2 files changed, 111 insertions(+), 5 deletions(-) diff --git a/solutions/blocks/hero/hero.js b/solutions/blocks/hero/hero.js index 4ec77d913..0b55aa71a 100644 --- a/solutions/blocks/hero/hero.js +++ b/solutions/blocks/hero/hero.js @@ -5,7 +5,8 @@ import { renderNanoBlocks, fetchProduct, } from '../../scripts/utils/utils.js'; - +import { trackProduct } from '../../scripts/scripts.js'; +import { ProductCard } from '../products/products.js'; /** * Builds hero block and prepends to main in a new section. * @param {Element} element The container element @@ -60,6 +61,79 @@ createNanoBlock('discount', (code, variant) => { return root; }); +/** + * Nanoblock representing the new product price + * @param mv The modelview holding the state of the view + * @param text The text located before the price + * @param monthly Show the monthly price if equal to 'monthly' + * @returns Root node of the nanoblock + */ +function renderPrice(mv, text = '', monthly = '') { + // TODO simplify CSS + const root = createTag( + 'div', + { + class: 'price', + }, + `${mv.model.basePrice}`, + ); + + const priceElt = root.querySelector('strong'); + + mv.subscribe(() => { + if (monthly.toLowerCase() === 'monthly') { + if (mv.model.discountedPrice) { + priceElt.innerHTML = `${text} ${mv.model.discountedMonthlyPrice} ${mv.model.currency} /mo`; + } else { + priceElt.innerHTML = `${text} ${mv.model.monthlyBasePrice} ${mv.model.currency} /mo`; + } + } else if (mv.model.discountedPrice) { + priceElt.innerHTML = `${text} ${mv.model.discountedPrice} ${mv.model.currency}`; + } else { + priceElt.innerHTML = `${text} ${mv.model.basePrice} ${mv.model.currency}`; + } + + trackProduct(mv.model); + }); + + return root; +} + +/** + * Nanoblock representing the old product price + * @param mv The modelview holding the state of the view + * @param text The text located before the price + * @param monthly Show the monthly price if equal to 'monthly' + * @returns Root node of the nanoblock + */ +function renderOldPrice(mv, text = '', monthly = '') { + // TODO simplify CSS + const root = createTag( + 'div', + { + class: 'price', + }, + `${text} ${mv.model.basePrice ?? ''} ${mv.model.currency ?? ''}`, + ); + + const oldPriceElt = root.querySelector('span'); + + mv.subscribe(() => { + if (mv.model.discountedPrice) { + oldPriceElt.innerHTML = monthly.toLowerCase() === 'monthly' + ? `${text} ${mv.model.monthlyBasePrice} ${mv.model.currency}/mo` + : `${text} ${mv.model.basePrice} ${mv.model.currency}`; + oldPriceElt.style.visibility = 'visible'; + } else { + oldPriceElt.style.visibility = 'hidden'; + } + }); + + return root; +} +createNanoBlock('price', renderPrice); +createNanoBlock('oldPrice', renderOldPrice); + /** * decorates hero block * @param {Element} block The hero block element @@ -79,6 +153,41 @@ export default async function decorate(block) { // Apply a CSS class to each selected