[PIE-8] Add recipe detail view #6
47
src/components/Detail/ImageCarousel.astro
Normal file
47
src/components/Detail/ImageCarousel.astro
Normal 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>
|
@ -1,8 +1,8 @@
|
||||
|
||||
<div class="flex w-full items-center bg-yellow-100 p-5">
|
||||
<a class="flex" href="/">
|
||||
<p class=" text-3xl">Reci</p>
|
||||
<p class=" text-3xl text-amber-500">pie</p>
|
||||
<p class=" text-3xl">Recipie</p>
|
||||
<!-- <p class=" text-3xl text-amber-500">pie</p> -->
|
||||
🥧
|
||||
</a>
|
||||
|
||||
|
@ -1,6 +1,8 @@
|
||||
---
|
||||
import client from "@/data/pocketbase";
|
||||
import SiteLayout from "@/layouts/base";
|
||||
import ImageCarousel from "@/components/Detail/ImageCarousel";
|
||||
|
||||
const { slug } = Astro.params;
|
||||
|
||||
const re = await client.collection("recipes").getOne(slug ?? "0");
|
||||
@ -8,5 +10,7 @@ const re = await client.collection("recipes").getOne(slug ?? "0");
|
||||
---
|
||||
|
||||
<SiteLayout>
|
||||
{re.name}
|
||||
<div class="flex">
|
||||
<ImageCarousel recipe={re} />
|
||||
</div>
|
||||
</SiteLayout>
|
Loading…
x
Reference in New Issue
Block a user