diff --git a/client/src/collections/strapiPoleElementsLoader.mjs b/client/src/collections/strapiPoleElementsLoader.mjs index 538a9472..44fac7e5 100644 --- a/client/src/collections/strapiPoleElementsLoader.mjs +++ b/client/src/collections/strapiPoleElementsLoader.mjs @@ -19,26 +19,52 @@ const strapiPoleElementsLoader = defineCollection({ }, }); - // Fetch articles from Strapi - const poleElementsData = await fetch(url.href); - const { data }= await poleElementsData.json(); - - // Transform the API response into the desired data structure - return data.map((item) => ({ - id: item.id.toString(), - name: item.name, - title: item.name, - description: item.description, - createdAt: item.createdAt, - updatedAt: item.updatedAt, - publishedAt: item.publishedAt, - mainImage: { - id: Number(item.mainImage.id), - documentId: item.mainImage.documentId, - url: item.mainImage.url, - alternativeText: item.mainImage.alternativeText, + try { + // Fetch articles from Strapi + const poleElementsData = await fetch(url.href); + + if (!poleElementsData.ok) { + throw new Error(`Failed to fetch data from Strapi: ${poleElementsData.status} ${poleElementsData.statusText}`); } - })); + + const response = await poleElementsData.json(); + const { data } = response; + + // Check if data is null or undefined + if (!data) { + throw new Error("No data received from Strapi API - the response was null or undefined"); + } + + // Ensure data is an array + const dataArray = Array.isArray(data) ? data : [data]; + + // Transform the API response into the desired data structure + return dataArray + .filter(item => item !== null && item !== undefined) // Filter out null/undefined items + .map((item) => ({ + id: item.id?.toString() || "", + name: item.name || "", + title: item.name || "", + description: item.description || "", + createdAt: item.createdAt || "", + updatedAt: item.updatedAt || "", + publishedAt: item.publishedAt || "", + mainImage: item.mainImage ? { + id: Number(item.mainImage.id) || 0, + documentId: item.mainImage.documentId || "", + url: item.mainImage.url || "", + alternativeText: item.mainImage.alternativeText || "", + } : { + id: 0, + documentId: "", + url: "", + alternativeText: "", + } + })); + } catch (error) { + console.error("Error loading pole elements from Strapi:", error); + return []; + } }, // Define the schema for type validation using Zod schema: z.object({ diff --git a/client/src/views/ElementView.astro b/client/src/views/ElementView.astro new file mode 100644 index 00000000..773c1d17 --- /dev/null +++ b/client/src/views/ElementView.astro @@ -0,0 +1,36 @@ +--- +import MardownContent from '../components/MardownContent.astro'; + +interface Props { + entry: { + data: { + title: string; + description: string; + }; + }; +} + +const { entry } = Astro.props; +--- + +
+

{entry.data.title}

+ +
+ + \ No newline at end of file