forked from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add product teaser block * Update styling * Add commerce events * Update product teaser block in Commerce picker * Fix product images
- Loading branch information
Showing
11 changed files
with
312 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
.product-teaser-wrapper { | ||
padding: 20px 0; | ||
} | ||
|
||
.product-teaser { | ||
display: flex; | ||
gap: 40px; | ||
flex-direction: column; | ||
justify-content: center; | ||
} | ||
|
||
.product-teaser .image { | ||
display: flex; | ||
justify-content: center; | ||
} | ||
|
||
.product-teaser .image .placeholder { | ||
width: 250px; | ||
height: 250px; | ||
background-color: var(--color-neutral-200); | ||
} | ||
|
||
.product-teaser .image picture>img { | ||
display: block; | ||
width: 250px; | ||
height: 250px; | ||
object-fit: contain; | ||
} | ||
|
||
.product-teaser .details { | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
|
||
.product-teaser .actions { | ||
display: flex; | ||
gap: 20px; | ||
margin-top: auto; | ||
justify-content: center; | ||
} | ||
|
||
.product-teaser h1 { | ||
min-height: 32px; | ||
font: var(--type-headline-1-font); | ||
letter-spacing: var(--type-headline-1-letter-spacing); | ||
margin: 0 0 16px; | ||
} | ||
|
||
.product-teaser .price { | ||
font: var(--type-headline-2-strong-font); | ||
letter-spacing: var(--type-headline-2-strong-letter-spacing); | ||
} | ||
|
||
.product-teaser .price .price-regular { | ||
font: var(--type-headline-2-default-font); | ||
letter-spacing: var(--type-headline-2-default-letter-spacing); | ||
color: var(--color-neutral-600); | ||
text-decoration: line-through; | ||
} | ||
|
||
.product-teaser .price .price-range { | ||
display: flex; | ||
gap: 20px; | ||
align-items: baseline; | ||
} | ||
|
||
.product-teaser .price .price-range .price-from { | ||
font: var(--type-headline-2-strong-font); | ||
letter-spacing: var(--type-headline-2-strong-letter-spacing); | ||
} | ||
|
||
.product-teaser .price .price-final { | ||
font: var(--type-headline-2-strong-font); | ||
letter-spacing: var(--type-headline-2-strong-letter-spacing); | ||
} | ||
|
||
@media (width >= 600px) { | ||
.product-teaser { | ||
flex-direction: row; | ||
} | ||
|
||
.product-teaser .details { | ||
flex: 1; | ||
} | ||
|
||
.product-teaser .actions { | ||
justify-content: flex-start; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import { readBlockConfig } from '../../scripts/aem.js'; | ||
import { performCatalogServiceQuery, renderPrice } from '../../scripts/commerce.js'; | ||
|
||
const productTeaserQuery = `query productTeaser($sku: String!) { | ||
products(skus: [$sku]) { | ||
sku | ||
urlKey | ||
name | ||
addToCartAllowed | ||
__typename | ||
images(roles: ["small_image"]) { | ||
label | ||
url | ||
} | ||
... on SimpleProductView { | ||
price { | ||
...priceFields | ||
} | ||
} | ||
... on ComplexProductView { | ||
priceRange { | ||
minimum { | ||
...priceFields | ||
} | ||
maximum { | ||
...priceFields | ||
} | ||
} | ||
} | ||
} | ||
} | ||
fragment priceFields on ProductViewPrice { | ||
regular { | ||
amount { | ||
currency | ||
value | ||
} | ||
} | ||
final { | ||
amount { | ||
currency | ||
value | ||
} | ||
} | ||
}`; | ||
|
||
function renderPlaceholder(config, block) { | ||
block.textContent = ''; | ||
block.appendChild(document.createRange().createContextualFragment(` | ||
<div class="image"> | ||
<div class="placeholder"></div> | ||
</div> | ||
<div class="details"> | ||
<h1></h1> | ||
<div class="price"></div> | ||
<div class="actions"> | ||
${config['details-button'] ? '<a href="#" class="button primary disabled">Details</a>' : ''} | ||
${config['cart-button'] ? '<button class="secondary" disabled>Add to Cart</button>' : ''} | ||
</div> | ||
</div> | ||
`)); | ||
} | ||
|
||
function renderImage(image, size = 250) { | ||
const { url: imageUrl, label } = image; | ||
const createUrlForWidth = (url, w, useWebply = true) => { | ||
const newUrl = new URL(url, window.location); | ||
if (useWebply) { | ||
newUrl.searchParams.set('format', 'webply'); | ||
newUrl.searchParams.set('optimize', 'medium'); | ||
} else { | ||
newUrl.searchParams.delete('format'); | ||
} | ||
newUrl.searchParams.set('width', w); | ||
newUrl.searchParams.delete('quality'); | ||
newUrl.searchParams.delete('dpr'); | ||
newUrl.searchParams.delete('bg-color'); | ||
return newUrl.toString(); | ||
}; | ||
|
||
const createUrlForDpi = (url, w, useWebply = true) => `${createUrlForWidth(url, w, useWebply)} 1x, ${createUrlForWidth(url, w * 2, useWebply)} 2x, ${createUrlForWidth(url, w * 3, useWebply)} 3x`; | ||
|
||
const webpUrl = createUrlForDpi(imageUrl, size, true); | ||
const jpgUrl = createUrlForDpi(imageUrl, size, false); | ||
|
||
return document.createRange().createContextualFragment(`<picture> | ||
<source srcset="${webpUrl}" /> | ||
<source srcset="${jpgUrl}" /> | ||
<img height="${size}" width="${size}" src="${createUrlForWidth(imageUrl, size, false)}" loading="eager" alt="${label}" /> | ||
</picture> | ||
`); | ||
} | ||
|
||
function renderProduct(product, config, block) { | ||
const { | ||
name, urlKey, sku, price, priceRange, addToCartAllowed, __typename, | ||
} = product; | ||
|
||
const currency = price?.final?.amount?.currency || priceRange?.minimum?.final?.amount?.currency; | ||
const priceFormatter = new Intl.NumberFormat('en-US', { | ||
style: 'currency', | ||
currency, | ||
}); | ||
|
||
block.textContent = ''; | ||
const fragment = document.createRange().createContextualFragment(` | ||
<div class="image"> | ||
</div> | ||
<div class="details"> | ||
<h1>${name}</h1> | ||
<div class="price">${renderPrice(product, priceFormatter.format)}</div> | ||
<div class="actions"> | ||
${config['details-button'] ? `<a href="/products/${urlKey}/${sku}" class="button primary">Details</a>` : ''} | ||
${config['cart-button'] && addToCartAllowed && __typename === 'SimpleProductView' ? '<button class="add-to-cart secondary">Add to Cart</button>' : ''} | ||
</div> | ||
</div> | ||
`); | ||
|
||
fragment.querySelector('.image').appendChild(renderImage(product.images[0], 250)); | ||
|
||
const addToCartButton = fragment.querySelector('.add-to-cart'); | ||
if (addToCartButton) { | ||
addToCartButton.addEventListener('click', async () => { | ||
const { cartApi } = await import('../../scripts/minicart/api.js'); | ||
// TODO: productId not exposed by catalog service as number | ||
window.adobeDataLayer.push({ productContext: { productId: 0, ...product } }); | ||
cartApi.addToCart(product.sku, [], 1, 'product-teaser'); | ||
}); | ||
} | ||
|
||
block.appendChild(fragment); | ||
} | ||
|
||
export default async function decorate(block) { | ||
const config = readBlockConfig(block); | ||
config['details-button'] = !!(config['details-button'] || config['details-button'] === 'true'); | ||
config['cart-button'] = !!(config['cart-button'] || config['cart-button'] === 'true'); | ||
|
||
renderPlaceholder(config, block); | ||
|
||
const { products } = await performCatalogServiceQuery(productTeaserQuery, { | ||
sku: config.sku, | ||
}); | ||
if (!products || !products.length > 0 || !products[0].sku) { | ||
return; | ||
} | ||
const [product] = products; | ||
product.images = product.images.map((image) => ({ ...image, url: image.url.replace(/^https?:/, '') })); | ||
|
||
renderProduct(product, config, block); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.