Skip to content

Commit

Permalink
Merge pull request #43 from Blazity/recommended
Browse files Browse the repository at this point in the history
feat: add recommended articles
  • Loading branch information
Pierniki authored Sep 25, 2023
2 parents 26e8491 + abed8a0 commit 983278a
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 18 deletions.
40 changes: 23 additions & 17 deletions src/app/[lang]/article/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Image from "next/image"
import { notFound } from "next/navigation"
import { Metadata } from "next/types"
import { RecommendedArticles } from "@/components/RecommendedArticles/RecommendedArticles"
import { RichText } from "@/components/RichText/RichText"
import { Locale } from "@/i18n/i18n"
import { getArticleBySlug } from "@/lib/client"
Expand Down Expand Up @@ -44,23 +45,28 @@ export default async function Web({ params: { slug, lang } }: ArticlePageProps)

if (!article) return notFound()
return (
<article className="w-full px-4 pb-16 pt-8">
{article.coverImage && (
<Image
src={article.coverImage.url}
alt={""}
width={1200}
height={630}
quality={100}
className="max-h-[630px] rounded-sm object-cover"
/>
<>
<article className="w-full px-4 pb-16 pt-8">
{article.coverImage && (
<Image
src={article.coverImage.url}
alt={""}
width={1200}
height={630}
quality={100}
className="max-h-[630px] rounded-sm object-cover"
/>
)}
<h1 className="mb-8 text-2xl font-semibold">{article.title}</h1>
{article.content && (
<section className="flex w-full flex-col gap-4">
<RichText raw={article.content.raw} />
</section>
)}
</article>
{article.recommendedArticles.length > 0 && (
<RecommendedArticles recommendedArticles={article.recommendedArticles} lang={lang} />
)}
<h1 className="mb-8 text-2xl font-semibold">{article.title}</h1>
{article.content && (
<section className="flex w-full flex-col gap-4">
<RichText raw={article.content.raw} />
</section>
)}
</article>
</>
)
}
2 changes: 2 additions & 0 deletions src/app/[lang]/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Footer } from "@/components/Footer/Footer"
import { DynamicLangSelect } from "@/components/LangSelect/DynamicLangSelect"
import { Navigation } from "@/components/Navigation/Navigation"
import { DynamicSearchDialog } from "@/components/Search/DynamicSearchDialog"
Expand Down Expand Up @@ -44,6 +45,7 @@ export default function Layout({ children, params }: { children: React.ReactNode
<DynamicLangSelect />
</nav>
{children}
<Footer lang={params.lang} />
</main>
</body>
</Providers>
Expand Down
28 changes: 28 additions & 0 deletions src/components/Footer/Footer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Link from "next/link"
import { Locale } from "@/i18n/i18n"
import { getFooter } from "@/lib/client"

type FooterProps = {
lang: Locale
}

export async function Footer({ lang }: FooterProps) {
const footer = await getFooter(lang)

return (
<footer className="flex w-[100%] items-center justify-between p-4">
<p>Footer</p>
<nav>
<ul className="flex gap-5">
{footer.pages.map((footerElement) => (
<li key={footerElement?.slug}>
<Link href={`/${lang}/${footerElement?.slug}`} prefetch={false}>
{footerElement?.title}
</Link>
</li>
))}
</ul>
</nav>
</footer>
)
}
40 changes: 40 additions & 0 deletions src/components/RecommendedArticles/RecommendedArticles.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import Image from "next/image"
import Link from "next/link"
import { Locale } from "@/i18n/i18n"

type RecommendedArticle = {
title: string
slug: string
id: string
coverImage?: { url: string } | null | undefined
}

type RecommendedArticlesProps = { recommendedArticles: RecommendedArticle[]; lang: Locale }

export async function RecommendedArticles({ recommendedArticles, lang }: RecommendedArticlesProps) {
return (
<section className="w-full px-4">
<h2 className="mb-4 text-2xl font-bold">Recommended articles</h2>
<div className="grid grid-cols-2 gap-8 md:grid-cols-4">
{recommendedArticles?.map((article) => (
<Link href={`/${lang}/article/${article.slug}`} prefetch={false} passHref key={`recommended-${article.id}`}>
<article className="flex flex-col gap-2">
<div className="h-[157px] max-w-[300px] rounded-sm bg-slate-100">
{article?.coverImage?.url && (
<Image
src={article.coverImage.url}
alt={article.title}
width={300}
height={157}
className="h-[157px] w-[300px] rounded-sm object-cover"
/>
)}
</div>
<div className="font-semibold">{article?.title}</div>
</article>
</Link>
))}
</div>
</section>
)
}
12 changes: 11 additions & 1 deletion src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
listArticlesBySlugQuery,
listArticlesForSitemapQuery,
} from "./queries/articles"
import { getHomepageQuery, getNavigationQuery } from "./queries/components"
import { getFooterQuery, getHomepageQuery, getNavigationQuery } from "./queries/components"
import { getPageBySlugQuery, listPagesForSitemapQuery } from "./queries/pages"
import { Tag } from "./tags"

Expand Down Expand Up @@ -56,6 +56,16 @@ export async function graphqlFetch<TQuery, TVariables>({
return parsed.data
}

export async function getFooter(locale: Locale) {
const { footers } = await graphqlFetch({
cache: "force-cache",
document: getFooterQuery,
tags: ["PAGES"],
variables: { locale },
})
return footers[0] ?? null
}

export async function getHomepage(locale: Locale) {
const { homepages } = await graphqlFetch({
cache: "force-cache",
Expand Down
8 changes: 8 additions & 0 deletions src/lib/queries/articles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ export const getArticleBySlugQuery = graphql(`
content {
raw
}
recommendedArticles {
title
slug
id
coverImage {
url
}
}
}
}
`)
Expand Down
11 changes: 11 additions & 0 deletions src/lib/queries/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,14 @@ export const getHomepageQuery = graphql(`
}
}
`)

export const getFooterQuery = graphql(`
query getFooter($locales: [Locale!]!) {
footers(locales: $locales, first: 1) {
pages {
slug
title
}
}
}
`)

0 comments on commit 983278a

Please sign in to comment.