442 lines
17 KiB
Plaintext
442 lines
17 KiB
Plaintext
---
|
||
export const prerender = false;
|
||
|
||
import Layout from "../../layouts/Layout.astro";
|
||
import { listProducts, getDefaultRegion } from "../../lib/medusa";
|
||
import { formatEUR, formatGrundpreis } from "../../lib/format";
|
||
import { CLAIM } from "../../lib/sections";
|
||
import {
|
||
findVariety,
|
||
varietyName,
|
||
COLLECTION_LABELS,
|
||
COLLECTION_NOTES,
|
||
COLLECTION_ORDER,
|
||
} from "../../lib/varieties";
|
||
|
||
let products: Awaited<ReturnType<typeof listProducts>> = [];
|
||
let loadError = false;
|
||
let regionId = "";
|
||
|
||
try {
|
||
const region = await getDefaultRegion();
|
||
regionId = region.id;
|
||
products = await listProducts(region.id);
|
||
} catch {
|
||
loadError = true;
|
||
}
|
||
|
||
function minPrice(product: (typeof products)[number]) {
|
||
const prices = product.variants
|
||
.map((v) => v.calculated_price?.calculated_amount)
|
||
.filter((amount): amount is number => amount != null);
|
||
return prices.length ? Math.min(...prices) : null;
|
||
}
|
||
|
||
function priceLabel(product: (typeof products)[number]) {
|
||
const prices = product.variants
|
||
.map((v) => v.calculated_price?.calculated_amount)
|
||
.filter((amount): amount is number => amount != null);
|
||
if (prices.length === 0) return null;
|
||
const min = Math.min(...prices);
|
||
return prices.length > 1 && new Set(prices).size > 1 ? `ab ${formatEUR(min)}` : formatEUR(min);
|
||
}
|
||
|
||
function grundpreisLabel(product: (typeof products)[number]) {
|
||
const variant = product.variants.find((v) => v.metadata?.net_weight_grams);
|
||
if (!variant?.calculated_price || !variant.metadata?.net_weight_grams) return null;
|
||
return formatGrundpreis(variant.calculated_price.calculated_amount, variant.metadata.net_weight_grams);
|
||
}
|
||
|
||
/**
|
||
* Ein Satz unter der Überschrift, wo die Kategorie allein nicht klar macht,
|
||
* worum es geht. Wo nichts steht, steht nichts.
|
||
*/
|
||
const CATEGORY_NOTES: Record<string, string | undefined> = {
|
||
"Abo": "Honig, der von allein kommt.",
|
||
"Bienenpatenschaften": "Ein eigenes Volk, betreut von uns.",
|
||
"Kurse": "Einen Tag lang selbst an den Bienen.",
|
||
};
|
||
|
||
const categoriesByName = new Map<string, number>();
|
||
for (const product of products) {
|
||
for (const category of product.categories ?? []) {
|
||
categoriesByName.set(category.name, category.rank);
|
||
}
|
||
}
|
||
const categories = [...categoriesByName.entries()].sort((a, b) => a[1] - b[1]).map(([name]) => name);
|
||
|
||
// Das Briefing führt die Honigkollektion in zwei Linien — Urban und Country.
|
||
// Der Shop bildet das ab, statt alles in ein Raster zu kippen. Was keine
|
||
// Honigsorte ist (Wachs, Geschenke), hängt hinten dran. Die Reihenfolge
|
||
// innerhalb einer Gruppe bleibt die aus Medusa.
|
||
// Was keine Honigsorte ist, bekommt seinen eigenen Abschnitt statt eines
|
||
// Sammelbeckens: Abo, Patenschaft und Kurs sind eigene Angebote und sollen
|
||
// nicht zwischen Bonbons und Bienenwachs verschwinden. Die Reihenfolge ist
|
||
// die der Kategorien in Medusa, dort lässt sie sich ändern.
|
||
const uebrige = products.filter((product) => !findVariety(product.title));
|
||
const kategoriegruppen = categories
|
||
.map((name) => ({
|
||
key: `kategorie-${name}`,
|
||
title: name,
|
||
note: CATEGORY_NOTES[name],
|
||
products: uebrige.filter((product) => product.categories?.[0]?.name === name),
|
||
}))
|
||
.filter((group) => group.products.length > 0);
|
||
|
||
const groups = [
|
||
...COLLECTION_ORDER.map((collection) => ({
|
||
key: collection as string,
|
||
title: COLLECTION_LABELS[collection],
|
||
note: COLLECTION_NOTES[collection] as string | undefined,
|
||
products: products.filter((product) => findVariety(product.title)?.collection === collection),
|
||
})),
|
||
...kategoriegruppen,
|
||
{
|
||
key: "weitere",
|
||
title: "Weitere Produkte",
|
||
note: undefined as string | undefined,
|
||
// Nur, was gar keiner Kategorie zugeordnet ist.
|
||
products: uebrige.filter(
|
||
(product) => !categories.includes(product.categories?.[0]?.name ?? "")
|
||
),
|
||
},
|
||
].filter((group) => group.products.length > 0);
|
||
---
|
||
|
||
<Layout
|
||
title="Shop"
|
||
description="Regionaler Honig, Bienenwachsprodukte und Geschenkideen von place4bees direkt aus Hamburg bestellen."
|
||
>
|
||
{/* Der Kopf steht hell und mittig: im Shop soll das Glas die Hauptrolle
|
||
spielen, nicht ein Banner darüber. */}
|
||
<section class="honeycomb-bg">
|
||
<div class="mx-auto max-w-[48rem] px-[var(--gutter)] pt-24 pb-12 text-center sm:pt-28 sm:pb-14">
|
||
<span class="section-label justify-center">{CLAIM}</span>
|
||
<h1 class="mt-6">Der Shop</h1>
|
||
<p class="lede mx-auto mt-6 max-w-[46ch] text-brand-text">
|
||
Regionaler Honig, Bienenprodukte und Geschenkideen direkt aus Hamburg —
|
||
von Hand abgefüllt, in kleinen Mengen.
|
||
</p>
|
||
</div>
|
||
</section>
|
||
|
||
<section class="honeycomb-bg pb-[var(--section-y)]">
|
||
<div class="mx-auto max-w-[84rem] px-[var(--gutter)]">
|
||
{loadError && (
|
||
<p role="alert" class="mt-10 rounded-[var(--radius-inner)] border border-[#e3b7b1] bg-[#fdf2f0] px-5 py-4 text-sm text-[#8f3128]">
|
||
Die Produkte konnten nicht geladen werden. Bitte laden Sie die Seite neu oder versuchen Sie es später erneut.
|
||
</p>
|
||
)}
|
||
|
||
{/* Klebt unterhalb der Shop-Leiste, nicht unter dem Hauptheader —
|
||
sonst schöbe sich die Leiste über die Filter. */}
|
||
<div class="sticky top-[calc(var(--header-h)+3.25rem)] z-20 -mx-[var(--gutter)] border-b border-brand-border/70 bg-brand-surface/85 px-[var(--gutter)] py-4 backdrop-blur-md">
|
||
{categories.length > 1 && (
|
||
<div
|
||
id="category-filters"
|
||
role="group"
|
||
aria-label="Nach Kategorie filtern"
|
||
class="-mx-1 flex gap-1.5 overflow-x-auto px-1 pb-1 [scrollbar-width:none] sm:flex-wrap sm:justify-center sm:overflow-visible"
|
||
>
|
||
<button type="button" data-category="" class="category-pill is-active" aria-pressed="true">Alle</button>
|
||
{categories.map((category) => (
|
||
<button type="button" data-category={category} class="category-pill" aria-pressed="false">
|
||
{category}
|
||
</button>
|
||
))}
|
||
</div>
|
||
)}
|
||
|
||
<div class="mt-3.5 flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-center">
|
||
<div class="relative w-full sm:max-w-[26rem]">
|
||
<svg
|
||
class="pointer-events-none absolute left-4 top-1/2 h-4 w-4 -translate-y-1/2 text-brand-text-muted"
|
||
viewBox="0 0 24 24"
|
||
fill="none"
|
||
stroke="currentColor"
|
||
stroke-width="1.75"
|
||
aria-hidden="true"
|
||
>
|
||
<circle cx="11" cy="11" r="7" />
|
||
<path stroke-linecap="round" d="m20 20-3.5-3.5" />
|
||
</svg>
|
||
<label class="sr-only" for="product-search">Produkte durchsuchen</label>
|
||
<input id="product-search" type="search" placeholder="Produkte durchsuchen" class="field pl-11" />
|
||
</div>
|
||
|
||
<div class="flex items-center gap-3">
|
||
<p id="result-count" aria-live="polite" class="whitespace-nowrap text-sm tabular-nums text-brand-text-muted">
|
||
{products.length} Produkte
|
||
</p>
|
||
<label class="sr-only" for="sort-select">Sortierung</label>
|
||
<select id="sort-select" class="field w-auto pr-8">
|
||
<option value="default">Empfehlung</option>
|
||
<option value="price-asc">Preis aufsteigend</option>
|
||
<option value="price-desc">Preis absteigend</option>
|
||
<option value="name-asc">Name A–Z</option>
|
||
</select>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div id="shop-products" data-region-id={regionId} class="mt-16 flex flex-col gap-28">
|
||
{groups.map((group) => (
|
||
<section class="product-group" data-group={group.key}>
|
||
{groups.length > 1 && (
|
||
<header class="mb-14 text-center">
|
||
<h2 class="shop-gruppe-titel">{group.title}</h2>
|
||
{group.note && (
|
||
<p class="mx-auto mt-2.5 max-w-[42ch] text-brand-text-muted">{group.note}</p>
|
||
)}
|
||
</header>
|
||
)}
|
||
|
||
{/* Höchstens drei Spalten, mit viel Luft dazwischen — die Kacheln
|
||
haben keinen Rahmen, der Abstand trennt sie. Vier Spalten haben
|
||
die Fotos so klein gemacht, dass die Seite gedrängt wirkte. */}
|
||
<div class="product-grid grid grid-cols-2 gap-x-6 gap-y-14 sm:gap-x-10 sm:gap-y-20 lg:grid-cols-3">
|
||
{group.products.map((product) => {
|
||
const price = priceLabel(product);
|
||
const grundpreis = grundpreisLabel(product);
|
||
const categoryName = product.categories?.[0]?.name;
|
||
const singleVariant = product.variants.length === 1 ? product.variants[0] : null;
|
||
const variety = findVariety(product.title);
|
||
return (
|
||
<article
|
||
class="shop-karte product-card group relative flex flex-col"
|
||
data-category={categoryName ?? ""}
|
||
data-name={product.title.toLowerCase()}
|
||
data-price={minPrice(product) ?? ""}
|
||
>
|
||
<a href={`/shop/${product.handle}`} class="absolute inset-0 z-10">
|
||
<span class="sr-only">{product.title} ansehen</span>
|
||
</a>
|
||
|
||
{/* Der Steg vom Etikett: die Sortenfarbe über dem Bild. */}
|
||
{variety && <span class="shop-karte-steg" style={`--sorte: ${variety.color}`} aria-hidden="true"></span>}
|
||
|
||
<div class="shop-karte-bild">
|
||
{product.thumbnail ? (
|
||
<img
|
||
src={product.thumbnail}
|
||
alt={product.title}
|
||
loading="lazy"
|
||
transition:name={`p-${product.handle}`}
|
||
/>
|
||
) : (
|
||
<div class="honeycomb-bg flex h-full w-full items-center justify-center">
|
||
<img src="/images/bee-single.png" alt="" class="h-12 w-auto opacity-30" />
|
||
</div>
|
||
)}
|
||
|
||
{singleVariant && (
|
||
<button type="button" class="quick-add shop-schnellkauf" data-variant-id={singleVariant.id}>
|
||
In den Warenkorb
|
||
</button>
|
||
)}
|
||
</div>
|
||
|
||
{/* Der kurze Sortenname aus dem Briefing statt des vollen
|
||
Katalogtitels — der beschreibende Teil steht darunter. */}
|
||
<h3 class="shop-karte-titel">{varietyName(product.title)}</h3>
|
||
{variety?.subline && <p class="shop-karte-unterzeile">{variety.subline}</p>}
|
||
{price && <p class="shop-karte-preis">{price}</p>}
|
||
{grundpreis && <p class="shop-karte-grundpreis">{grundpreis}</p>}
|
||
</article>
|
||
);
|
||
})}
|
||
</div>
|
||
</section>
|
||
))}
|
||
</div>
|
||
|
||
<!-- Empty state: composed rather than a bare line of grey text. -->
|
||
<div id="empty-state" class="hidden py-20 text-center">
|
||
<img src="/images/bee-single.png" alt="" class="mx-auto h-10 w-auto opacity-40" />
|
||
<p class="mt-6 text-2xl font-semibold -tracking-[0.015em] text-brand-ink">Nichts gefunden</p>
|
||
<p class="mx-auto mt-3 max-w-[38ch] text-brand-text">
|
||
Für diese Suche oder Kategorie gibt es keine Produkte. Setzen Sie die
|
||
Filter zurück, um wieder alles zu sehen.
|
||
</p>
|
||
<button type="button" id="reset-filters" class="btn btn-outline mt-7">Filter zurücksetzen</button>
|
||
</div>
|
||
|
||
{!loadError && products.length === 0 && (
|
||
<p class="py-20 text-center text-brand-text-muted">Aktuell sind keine Produkte verfügbar.</p>
|
||
)}
|
||
</div>
|
||
</section>
|
||
</Layout>
|
||
|
||
<style>
|
||
.category-pill {
|
||
flex: none;
|
||
border-radius: 9999px;
|
||
border: 1px solid var(--color-brand-border);
|
||
background: var(--color-brand-surface-strong);
|
||
padding: 0.45rem 1rem;
|
||
font-size: 0.875rem;
|
||
color: var(--color-brand-text);
|
||
transition:
|
||
background-color 0.25s var(--ease-spring),
|
||
color 0.25s var(--ease-spring),
|
||
border-color 0.25s var(--ease-spring);
|
||
}
|
||
|
||
.category-pill:hover {
|
||
border-color: var(--color-brand-ink);
|
||
color: var(--color-brand-ink);
|
||
}
|
||
|
||
.category-pill.is-active {
|
||
background: var(--color-brand-ink);
|
||
border-color: var(--color-brand-ink);
|
||
color: #fff;
|
||
}
|
||
</style>
|
||
|
||
<script define:vars={{ regionId }}>
|
||
const pills = document.querySelectorAll(".category-pill");
|
||
const cards = Array.from(document.querySelectorAll(".product-card"));
|
||
const searchInput = document.getElementById("product-search");
|
||
const sortSelect = document.getElementById("sort-select");
|
||
const resultCount = document.getElementById("result-count");
|
||
const emptyState = document.getElementById("empty-state");
|
||
const resetButton = document.getElementById("reset-filters");
|
||
const groupSections = Array.from(document.querySelectorAll(".product-group"));
|
||
const grids = Array.from(document.querySelectorAll(".product-grid"));
|
||
|
||
let activeCategory = "";
|
||
|
||
// `animate` nur bei Kategorie- und Reset-Klicks. Beim Tippen in der Suche
|
||
// liefe die Animation bei jedem Anschlag erneut — laut Playbook gehört auf
|
||
// hochfrequente Aktionen keine Bewegung.
|
||
function applyFilters(animate = false) {
|
||
const query = (searchInput?.value ?? "").trim().toLowerCase();
|
||
let visible = 0;
|
||
|
||
cards.forEach((card) => {
|
||
const matchesCategory = !activeCategory || card.dataset.category === activeCategory;
|
||
const matchesQuery = !query || card.dataset.name?.includes(query);
|
||
const show = matchesCategory && matchesQuery;
|
||
const wasHidden = card.classList.contains("hidden");
|
||
card.classList.toggle("hidden", !show);
|
||
if (show) {
|
||
visible++;
|
||
if (animate && wasHidden) {
|
||
card.classList.remove("swap-in");
|
||
void card.offsetWidth;
|
||
card.classList.add("swap-in");
|
||
card.addEventListener(
|
||
"animationend",
|
||
() => card.classList.remove("swap-in"),
|
||
{ once: true }
|
||
);
|
||
}
|
||
}
|
||
});
|
||
|
||
// Eine Kollektion ohne sichtbares Produkt verschwindet mitsamt
|
||
// Überschrift — sonst steht "Country Collection" über einem leeren Raster.
|
||
groupSections.forEach((section) => {
|
||
section.classList.toggle("hidden", section.querySelector(".product-card:not(.hidden)") === null);
|
||
});
|
||
|
||
if (resultCount) resultCount.textContent = `${visible} Produkt${visible === 1 ? "" : "e"}`;
|
||
// Ohne geladene Produkte greift bereits die Fehler- bzw. "keine
|
||
// Produkte verfügbar"-Meldung; dann nicht zusätzlich "Nichts gefunden".
|
||
emptyState?.classList.toggle("hidden", visible !== 0 || cards.length === 0);
|
||
}
|
||
|
||
function selectCategory(category) {
|
||
activeCategory = category;
|
||
pills.forEach((pill) => {
|
||
const active = (pill.dataset.category ?? "") === category;
|
||
pill.classList.toggle("is-active", active);
|
||
pill.setAttribute("aria-pressed", String(active));
|
||
});
|
||
applyFilters(true);
|
||
}
|
||
|
||
pills.forEach((pill) => {
|
||
pill.addEventListener("click", () => selectCategory(pill.dataset.category ?? ""));
|
||
});
|
||
|
||
searchInput?.addEventListener("input", applyFilters);
|
||
|
||
resetButton?.addEventListener("click", () => {
|
||
if (searchInput) searchInput.value = "";
|
||
selectCategory("");
|
||
});
|
||
|
||
// Sortiert wird innerhalb der Kollektion. Die Gliederung aus dem Briefing
|
||
// bleibt stehen, egal wie sortiert wird.
|
||
sortSelect?.addEventListener("change", () => {
|
||
const mode = sortSelect.value;
|
||
const compare = (a, b) => {
|
||
if (mode === "price-asc" || mode === "price-desc") {
|
||
const pa = Number(a.dataset.price) || 0;
|
||
const pb = Number(b.dataset.price) || 0;
|
||
return mode === "price-asc" ? pa - pb : pb - pa;
|
||
}
|
||
if (mode === "name-asc") {
|
||
return (a.dataset.name ?? "").localeCompare(b.dataset.name ?? "");
|
||
}
|
||
return 0;
|
||
};
|
||
grids.forEach((grid) => {
|
||
Array.from(grid.querySelectorAll(".product-card"))
|
||
.sort(compare)
|
||
.forEach((card) => grid.appendChild(card));
|
||
});
|
||
});
|
||
|
||
applyFilters();
|
||
</script>
|
||
|
||
<script>
|
||
import { getCartIdClient, setCartIdClient } from "../../lib/cartCookie";
|
||
import { createCart, addLineItem } from "../../lib/cart";
|
||
|
||
// astro:page-load feuert beim ersten Laden und nach jedem
|
||
// View-Transition-Wechsel.
|
||
document.addEventListener("astro:page-load", () => {
|
||
|
||
document.querySelectorAll<HTMLButtonElement>(".quick-add").forEach((button) => {
|
||
button.addEventListener("click", async (event) => {
|
||
event.preventDefault();
|
||
event.stopPropagation();
|
||
|
||
const regionId = document.getElementById("shop-products")?.dataset.regionId;
|
||
const variantId = button.dataset.variantId;
|
||
if (!variantId) return;
|
||
|
||
const originalLabel = button.textContent;
|
||
button.disabled = true;
|
||
try {
|
||
let cartId = getCartIdClient();
|
||
if (!cartId) {
|
||
if (!regionId) throw new Error("no region");
|
||
const cart = await createCart(regionId);
|
||
cartId = cart.id;
|
||
setCartIdClient(cartId);
|
||
}
|
||
await addLineItem(cartId, variantId, 1);
|
||
window.dispatchEvent(new CustomEvent("cart:updated"));
|
||
button.textContent = "Hinzugefügt";
|
||
setTimeout(() => {
|
||
button.textContent = originalLabel;
|
||
button.disabled = false;
|
||
}, 1500);
|
||
} catch {
|
||
button.textContent = "Nicht geklappt";
|
||
setTimeout(() => {
|
||
button.textContent = originalLabel;
|
||
button.disabled = false;
|
||
}, 1500);
|
||
}
|
||
});
|
||
});
|
||
});
|
||
</script>
|