Hello! I am building a site with a shop.
The shop has a content collection, containing products, with data properties containing the title, product id (to create a payment) and price.
The body of the collection entry is the description of the product, that will be displayed in a modal (svelte component) when clicking on the product card.
My current code:
- Shop component (
src/components/Shop.svelte):
<script lang="ts">
import type { CollectionEntry } from "astro:content";
import Modal from "./Modal.svelte";
import type { AstroComponentFactory } from "astro/dist/runtime/server";
export let products: CollectionEntry<"shop">[];
let shownModal: Promise<AstroComponentFactory> | undefined = undefined
</script>
<ol>
{#each products as product}
<!-- svelte-ignore a11y-click-events-have-key-events -->
<li on:click={() => {shownModal = (product.render().then(({ Content }) => Content))}}>
<h2>{product.data.title}</h2>
<h3 class="align-end">{product.data.price}</h3>
</li>
{/each}
</ol>
<Modal showModal={shownModal !== undefined}>
{#if shownModal !== undefined}
{#await (shownModal) then M}
<M />
{/await}
{/if}
</Modal>
- Shop page (
src/pages/shop.astro):
---
import Shop from '@/components/Shop.svelte'
import { CollectionEntry, getCollection } from 'astro:content'
const products: CollectionEntry<"shop">[] = await getCollection('shop');
---
<Shop client:load {products} />