Skip to content

Commit 6f35e17

Browse files
✨ carregando os dados da pagina
1 parent 97cf7ed commit 6f35e17

File tree

4 files changed

+77
-24
lines changed

4 files changed

+77
-24
lines changed

src/app/artigos/ArticlesRepository.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ import { Article } from './Article'
22

33
export interface ArticlesRepository {
44
findMany(): Promise<Article[]>
5+
findBySlug(slug: string): Promise<Article | null>
56
}

src/app/artigos/SanityArticlesRepository.ts

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@ import { Article } from './Article'
44
import { ArticlesRepository } from './ArticlesRepository'
55

66
export class SanityArticlesRepository implements ArticlesRepository {
7-
async findMany(): Promise<Article[]> {
8-
const articleResponseSchema = z.array(
9-
z.object({
10-
title: z.string(),
11-
description: z.string(),
12-
author: z.object({
13-
name: z.string(),
14-
}),
15-
mainImage: z.object({
16-
asset: z.object({
17-
path: z.string(),
18-
}),
19-
}),
20-
slug: z.object({
21-
current: z.string(),
22-
}),
23-
publishedAt: z.string(),
7+
private readonly articleSchema = z.object({
8+
title: z.string(),
9+
description: z.string(),
10+
author: z.object({
11+
name: z.string(),
12+
}),
13+
mainImage: z.object({
14+
asset: z.object({
15+
path: z.string(),
2416
}),
25-
)
17+
}),
18+
slug: z.object({
19+
current: z.string(),
20+
}),
21+
publishedAt: z.string(),
22+
})
23+
24+
async findMany(): Promise<Article[]> {
25+
const articleResponseSchema = z.array(this.articleSchema)
2626
const CONTENT_QUERY = `*[_type == "post"] {
2727
...,
2828
author->,
@@ -52,4 +52,42 @@ export class SanityArticlesRepository implements ArticlesRepository {
5252
} as Article
5353
})
5454
}
55+
56+
async findBySlug(slug: string) {
57+
const query = `*[_type == "post" && slug.current == $slug][0] {
58+
...,
59+
author->,
60+
mainImage {
61+
...,
62+
asset->
63+
},
64+
categories[]->,
65+
body
66+
}`
67+
68+
const params = { slug }
69+
70+
try {
71+
const contentResponse = await client.fetch(query, params)
72+
console.log('Artigo:', contentResponse)
73+
const article = this.articleSchema.parse(contentResponse)
74+
return {
75+
id: article.slug.current,
76+
title: article.title,
77+
description:
78+
article.description.length > 120
79+
? article.description.substring(0, 120).concat('...')
80+
: article.description,
81+
author: article.author.name,
82+
createdAt: article.publishedAt,
83+
imageUrl: 'https://cdn.sanity.io/'.concat(article.mainImage.asset.path),
84+
slug: article.slug.current,
85+
} as Article
86+
} catch (error) {
87+
console.error('Erro ao buscar artigo:', error)
88+
return null
89+
}
90+
}
5591
}
92+
93+
export const articlesRepository = new SanityArticlesRepository()

src/app/artigos/[slug]/page.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
1+
import Image from 'next/image'
2+
import { articlesRepository } from '../SanityArticlesRepository'
3+
14
interface ArticlePageProps {
25
params: {
36
slug: string
47
}
58
}
69

7-
export default function ArticlePage(props: ArticlePageProps) {
10+
export default async function ArticlePage(props: ArticlePageProps) {
11+
const article = await articlesRepository.findBySlug(props.params.slug)
12+
13+
if (!article) return <div>Not found</div>
14+
815
return (
9-
<div>
10-
<h1>{props.params.slug}</h1>
16+
<div className="max-w-4xl mx-auto mt-8 max-md:mx-4">
17+
<div className="grid items-center gap-4">
18+
<Image
19+
width={2200}
20+
height={120}
21+
src={article.imageUrl}
22+
alt=""
23+
className="rounded-3xl"
24+
/>
25+
<h1 className="font-bold text-3xl">{article.title}</h1>
26+
</div>
1127
</div>
1228
)
1329
}

src/app/artigos/page.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ import { useEffect, useState } from 'react'
44
import { Article } from './Article'
55
import { ArticleList } from './ArticleList'
66
import { EmptyState } from './EmptyState'
7-
import { SanityArticlesRepository } from './SanityArticlesRepository'
7+
import { articlesRepository } from './SanityArticlesRepository'
88
import { SeachInputField, SearchForm } from './SeachInputField'
99

10-
const articlesRepository = new SanityArticlesRepository()
11-
1210
export default function ArticlePage() {
1311
const [allArticles, setAllArticles] = useState<Article[]>([])
1412
const [articles, setArticles] = useState(allArticles)

0 commit comments

Comments
 (0)