Skip to content

Commit

Permalink
274 fix hero url (#281)
Browse files Browse the repository at this point in the history
* Fix hero thing closing #274

* Prettier
  • Loading branch information
EliasKnudsen authored and GitHub committed Apr 13, 2024
1 parent 48fd958 commit 8fad13a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
6 changes: 1 addition & 5 deletions frontend/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,7 @@ export default async function Home() {
</div>
</ColoredSection>

<div className="flex flex-col items-center px-8 py-12 text-center">
<div className="prose prose-invert">
<HeroWrapper></HeroWrapper>
</div>
</div>
<HeroWrapper></HeroWrapper>
</>
);
}
51 changes: 37 additions & 14 deletions frontend/src/components/HeroWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,52 @@ query Query($publicationState: PublicationState) {
}
}
}`);

export default async function HeroWrapper() {
const graphqlData = await getClient().query({
query: GET_HERO_DATA,
});

if (graphqlData.data === null || graphqlData.data === undefined) {
return <div>There are no projects to show.</div>;
// Check if data is available
if (
!graphqlData.data ||
!graphqlData.data.hero ||
!graphqlData.data.hero.data
) {
return <></>;
}

const heroData = graphqlData.data.hero.data;
const heroAttributes = heroData.attributes;

if (!heroAttributes || !heroAttributes.image?.data) {
return <></>;
}

const imageUrl = STRAPI_URL! + heroAttributes.image?.data?.attributes?.url;
if (!isValidUrl(imageUrl)) {
return <div>Invalid image URL.</div>;
}

return (
<>
<Hero
title={"Hero"}
description={
graphqlData.data.hero?.data?.attributes?.text as string
}
imageUrl={
(STRAPI_URL! +
graphqlData.data.hero?.data?.attributes?.image?.data
?.attributes?.url) as string
}
></Hero>
<div className="flex flex-col items-center px-8 py-12 text-center">
<div className="prose prose-invert">
<Hero
title="Hero"
description={heroAttributes.text || ""}
imageUrl={imageUrl}
/>
</div>
</div>
</>
);
}

function isValidUrl(urlString: string): boolean {
try {
new URL(urlString);
return true;
} catch (e) {
return false;
}
}

0 comments on commit 8fad13a

Please sign in to comment.