diff --git a/src/components/Card/TagRow.astro b/src/components/Card/TagRow.astro index af56aec..28ec731 100644 --- a/src/components/Card/TagRow.astro +++ b/src/components/Card/TagRow.astro @@ -2,21 +2,11 @@ import client from "../../data/pocketbase" interface Props { - tagIds: string[] + tags: string[] } -const { tagIds } = Astro.props +const { tags } = Astro.props -const tags = tagIds && tagIds.length > 0 - ? await Promise.all(tagIds.map(async (tagId: string) => { - try { - const tagData = await client.collection("tags").getOne(tagId) - return { name: tagData.name, id: tagId } - } catch (error) { - return null - } - })) - : [] ---
diff --git a/src/components/Detail/ImageCarousel.astro b/src/components/Detail/ImageCarousel.astro index bbdc8c6..3e5f90d 100644 --- a/src/components/Detail/ImageCarousel.astro +++ b/src/components/Detail/ImageCarousel.astro @@ -1,17 +1,7 @@ --- import client from "@/data/pocketbase"; const { class: className, recipe } = Astro.props - -async function getLink(img: string) { - const record = await client.collection("images").getOne(img) - const link = await client.files.getRelativeURL(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)) -) +const links = await client.getRecipeImages(recipe as string) --- diff --git a/src/components/Detail/InfoView.astro b/src/components/Detail/InfoView.astro index 2516136..4bde49e 100644 --- a/src/components/Detail/InfoView.astro +++ b/src/components/Detail/InfoView.astro @@ -26,4 +26,4 @@ const waitTime = formatTime(re.waittime) {waitTime && (

{waitTime} Wait

)} {re.rating !== 0 && (

{re.rating}

/10

)}
- \ No newline at end of file + \ No newline at end of file diff --git a/src/components/Detail/StepIngredientSideView.astro b/src/components/Detail/StepIngredientSideView.astro index b4cdc49..325890d 100644 --- a/src/components/Detail/StepIngredientSideView.astro +++ b/src/components/Detail/StepIngredientSideView.astro @@ -3,13 +3,10 @@ import client from "@/data/pocketbase"; const { ingredients, class: className } = Astro.props; -const ings = await Promise.all( - ingredients.map(async i => await client.collection("ingredients").getOne(i)) -) ---
- {ings.map(i => ( + {ingredients.map(i => (

• {i.quantity} {i.unit || " "} {i.name}

diff --git a/src/components/Detail/StepView.astro b/src/components/Detail/StepView.astro index 1860721..ef41ea5 100644 --- a/src/components/Detail/StepView.astro +++ b/src/components/Detail/StepView.astro @@ -16,7 +16,7 @@ const { steps, class: className } = Astro.props

{s.instruction}

{s.ingredients && s.ingredients.length > 0 && (
- +
)}
diff --git a/src/data/pocketbase.ts b/src/data/pocketbase.ts index 7fe09de..d2b63ab 100644 --- a/src/data/pocketbase.ts +++ b/src/data/pocketbase.ts @@ -35,8 +35,26 @@ class APIClient { } async getRecipe(id: string) { - return await this.client.collection(Collection.RECIPES).getOne(id, { expand: 'ingredients,tags,steps' }) + return await this.client.collection(Collection.RECIPES).getOne(id, { expand: 'ingredients,tags,steps,images,steps.ingredients' }) } + + // IMAGE + async getImageURL(imgID: string, relative: boolean = true) { + const record = await this.client.collection("images").getOne(imgID) + const res = this.client.files.getURL(record, record.image) + return relative ? res.substring(21) : res + } + + async getRecipeImages(recipeID: string) { + const re = await this.getRecipe(recipeID) + const imgIDs = re.images ?? [] + + const urls = Promise.all( + imgIDs.map(img => this.getImageURL(img)) + ) + return urls + } + } const client = new APIClient() export default client; \ No newline at end of file diff --git a/src/data/schema.ts b/src/data/schema.ts index efaab1c..e2b49f3 100644 --- a/src/data/schema.ts +++ b/src/data/schema.ts @@ -25,6 +25,8 @@ export interface Tag extends BaseRecord { // it is here in case it is (a good idea) export interface Image extends BaseRecord { id: string + filename: string + url?: string } export interface Recipe extends BaseRecord { diff --git a/src/pages/recipe/[recipeid].astro b/src/pages/recipe/[recipeid].astro index ffe0c54..1b938d1 100644 --- a/src/pages/recipe/[recipeid].astro +++ b/src/pages/recipe/[recipeid].astro @@ -15,7 +15,7 @@ const re = await client.getRecipe(recipeid as string)
- +

{re.name}