Skip to content
This repository was archived by the owner on Jun 28, 2025. It is now read-only.

Commit 8f3cb0b

Browse files
author
Manuel Proß
committed
refactor(web): rename variables to make fetching of news per news section easier to understand
1 parent 66579b0 commit 8f3cb0b

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

web/src/app/api/[homepage]/route.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getNews } from "@/helpers/homepage";
2-
import { SingleType } from "@/types/strapi";
2+
import { NewsSection, SingleType } from "@/types/strapi";
33

44
export async function GET() {
55
const res = await fetch(`${process.env.CMS_API}/home?populate=deep`, {
@@ -15,21 +15,20 @@ export async function GET() {
1515

1616
const homePageData = (await res.json()) as SingleType;
1717

18-
if (!homePageData.data.attributes) {
19-
return Response.json("Homepage contains no Data");
18+
if (!homePageData.data.attributes.body) {
19+
return Response.json("Homepage contains no data");
2020
}
21-
const news = homePageData.data.attributes.body
22-
.map((el, i) => {
23-
if ("newsCount" in el) return { index: i, headline: el.headline, newsCount: el.newsCount };
24-
})
25-
.filter(Boolean);
26-
27-
for (const newsEntry of news) {
28-
if (!newsEntry) break;
29-
const fetchedNews = await getNews(newsEntry?.newsCount);
30-
fetchedNews.headline = newsEntry.headline;
31-
32-
homePageData.data.attributes.body[newsEntry.index] = fetchedNews;
21+
22+
const newsSections = homePageData.data.attributes.body.map((contentEntry, i) => {
23+
if ("newsCount" in contentEntry) return { index: i, news: { headline: contentEntry.headline, newsCount: contentEntry.newsCount } as NewsSection };
24+
});
25+
26+
for (const newsSectionEntry of newsSections) {
27+
if (!newsSectionEntry) break;
28+
const fetchedNews = await getNews(newsSectionEntry?.news.newsCount);
29+
fetchedNews.headline = newsSectionEntry.news.headline;
30+
31+
homePageData.data.attributes.body[newsSectionEntry.index] = fetchedNews;
3332
}
3433

3534
return Response.json(homePageData.data.attributes);

0 commit comments

Comments
 (0)