From 7df048adb059ab9c6a67db1d9a24809adeebc1f5 Mon Sep 17 00:00:00 2001 From: Oier Bravo Urtasun Date: Sun, 22 Jun 2025 00:22:26 +0200 Subject: [PATCH] PoleElementsList component --- client/src/components/PoleElementsList.astro | 38 +++++++++++++++ client/src/config/strapi.ts | 9 ++++ client/src/pages/index.astro | 50 +++++--------------- 3 files changed, 58 insertions(+), 39 deletions(-) create mode 100644 client/src/components/PoleElementsList.astro create mode 100644 client/src/config/strapi.ts diff --git a/client/src/components/PoleElementsList.astro b/client/src/components/PoleElementsList.astro new file mode 100644 index 00000000..12b923b6 --- /dev/null +++ b/client/src/components/PoleElementsList.astro @@ -0,0 +1,38 @@ +--- +// Import necessary components and utilities +import MarkdownComponent from "./MardownContent.astro"; +import { getStrapiMedia } from "../utils/strapi"; +import { getStrapiBaseUrl } from "../config/strapi"; +import type { HTMLAttributes } from "astro/types"; + +// Define the props interface +export interface Props extends HTMLAttributes<'div'> { + elements: any[]; +} + +// Destructure props and get BASE_URL from global config +const { elements, ...otherProps } = Astro.props; +const BASE_URL = getStrapiBaseUrl(); +--- + +
+ { + elements.map((poleElement) => ( +
+ {poleElement.data.mainImage.alternativeText} +
+

+ {poleElement.data.name} +

+
+
+ )) + } +
\ No newline at end of file diff --git a/client/src/config/strapi.ts b/client/src/config/strapi.ts new file mode 100644 index 00000000..84880375 --- /dev/null +++ b/client/src/config/strapi.ts @@ -0,0 +1,9 @@ +// Global Strapi configuration +export const STRAPI_CONFIG = { + BASE_URL: (import.meta.env.STRAPI_URL as string) || "http://localhost:1337", +} as const; + +// Helper function to get the base URL +export function getStrapiBaseUrl(): string { + return STRAPI_CONFIG.BASE_URL; +} \ No newline at end of file diff --git a/client/src/pages/index.astro b/client/src/pages/index.astro index 1680c9c0..55d40d73 100644 --- a/client/src/pages/index.astro +++ b/client/src/pages/index.astro @@ -2,13 +2,13 @@ // Import necessary components and utilities import Layout from "../layouts/Layout.astro"; import { getCollection } from "astro:content"; -import MarkdownComponent from "../components/MardownContent.astro"; -import { getStrapiMedia } from "../utils/strapi"; +import PoleElementsList from "../components/PoleElementsList.astro"; +import { getStrapiBaseUrl } from "../config/strapi"; // Fetch all posts from Strapi using Astro's content collection const strapiPoleElements = await getCollection("strapiPoleElementsLoader"); -// Get Strapi URL from environment variables with fallback to localhost -const BASE_URL = (await import.meta.env.STRAPI_URL) || "http://localhost:1337"; +// Get Strapi URL from global config +const BASE_URL = getStrapiBaseUrl(); --- @@ -17,40 +17,12 @@ const BASE_URL = (await import.meta.env.STRAPI_URL) || "http://localhost:1337";

Hello Strapi 5 and Astro 5 World

- -
- {/* Map through posts and create article cards */} - { - strapiPoleElements.map((poleElement) => ( -
- {/* Post cover image */} - {poleElement.data.mainImage.alternativeText} - {/* Post content container */} -
-

- {poleElement.data.name} -

- -
- Published:{" "} - {new Date( - poleElement.data.publishedAt, - ).toLocaleDateString()} -
-
-
- )) - } -
+ +