decent nice carousel working

This commit is contained in:
June 2025-08-13 08:32:43 +12:00
parent 4451c95bf7
commit 66567b9b68
Signed by: breadone
GPG Key ID: FDC19FE143200483
3 changed files with 54 additions and 3 deletions

View File

@ -0,0 +1,47 @@
---
import client from "@/data/pocketbase";
const { recipe } = Astro.props
async function getLink(img: string) {
const record = await client.collection("images").getOne(img)
const link = await client.files.getURL(record, record.image)
return link
}
// Use Promise.all to wait for all async operations to complete
const links = await Promise.all(
recipe.images.map((img: string) => getLink(img))
)
---
<script>
let pos = 0;
const dataElement = document.getElementById('carousel-data');
const links = dataElement ? JSON.parse(dataElement.textContent || '[]') : [];
const cap = links.length - 1;
const img = document.getElementById('carousel-img') as HTMLImageElement;
function inc() {
pos = pos === cap ? 0 : pos + 1;
if (img) img.src = links[pos];
}
function dec() {
pos = pos === 0 ? cap : pos - 1;
if (img) img.src = links[pos];
}
// make functions globally accessible
(window as any).inc = inc;
(window as any).dec = dec;
</script>
<div>
<!-- Hidden element to pass server data to client -->
<div class="hidden" id="carousel-data">{JSON.stringify(links)}</div>
<img id="carousel-img" src={links[0]} />
<button onclick="inc()">NEXT</button>
<button onclick="dec()">PREV</button>
</div>

View File

@ -1,8 +1,8 @@
<div class="flex w-full items-center bg-yellow-100 p-5"> <div class="flex w-full items-center bg-yellow-100 p-5">
<a class="flex" href="/"> <a class="flex" href="/">
<p class=" text-3xl">Reci</p> <p class=" text-3xl">Recipie</p>
<p class=" text-3xl text-amber-500">pie</p> <!-- <p class=" text-3xl text-amber-500">pie</p> -->
🥧 🥧
</a> </a>

View File

@ -1,6 +1,8 @@
--- ---
import client from "@/data/pocketbase"; import client from "@/data/pocketbase";
import SiteLayout from "@/layouts/base"; import SiteLayout from "@/layouts/base";
import ImageCarousel from "@/components/Detail/ImageCarousel";
const { slug } = Astro.params; const { slug } = Astro.params;
const re = await client.collection("recipes").getOne(slug ?? "0"); const re = await client.collection("recipes").getOne(slug ?? "0");
@ -8,5 +10,7 @@ const re = await client.collection("recipes").getOne(slug ?? "0");
--- ---
<SiteLayout> <SiteLayout>
{re.name} <div class="flex">
<ImageCarousel recipe={re} />
</div>
</SiteLayout> </SiteLayout>