|
1 | 1 | 'use client' |
2 | 2 |
|
| 3 | +import { client } from 'programou/utils/sanity/client' |
3 | 4 | import { useState } from 'react' |
| 5 | +import { z } from 'zod' |
| 6 | +import { Article } from './Article' |
4 | 7 | import { ArticleList } from './ArticleList' |
5 | 8 | import { EmptyState } from './EmptyState' |
6 | 9 | import { SeachInputField, SearchForm } from './SeachInputField' |
7 | 10 |
|
| 11 | +interface ArticlesRepository { |
| 12 | + findMany(): Promise<Article[]> |
| 13 | +} |
| 14 | + |
| 15 | +class SanityArticlesRepository implements ArticlesRepository { |
| 16 | + async findMany(): Promise<Article[]> { |
| 17 | + const articleResponseSchema = z.array( |
| 18 | + z.object({ |
| 19 | + title: z.string(), |
| 20 | + author: z.object({ |
| 21 | + name: z.string(), |
| 22 | + }), |
| 23 | + mainImage: z.object({ |
| 24 | + asset: z.object({ |
| 25 | + path: z.string(), |
| 26 | + }), |
| 27 | + }), |
| 28 | + slug: z.object({ |
| 29 | + current: z.string(), |
| 30 | + }), |
| 31 | + publishedAt: z.string(), |
| 32 | + }), |
| 33 | + ) |
| 34 | + const CONTENT_QUERY = `*[_type == "post"] { |
| 35 | + ..., |
| 36 | + author->, |
| 37 | + mainImage { |
| 38 | + ..., |
| 39 | + asset-> |
| 40 | + }, |
| 41 | + categories[]->, |
| 42 | + body |
| 43 | + } |
| 44 | + ` |
| 45 | + const contentResponse = await client.fetch(CONTENT_QUERY) |
| 46 | + const articlesResponse = articleResponseSchema.parse(contentResponse) |
| 47 | + return articlesResponse.map((article) => { |
| 48 | + return { |
| 49 | + id: article.slug.current, |
| 50 | + title: article.title, |
| 51 | + excerpt: article.title.substring(0, 40).concat('...'), |
| 52 | + author: article.author.name, |
| 53 | + createdAt: article.publishedAt, |
| 54 | + imageUrl: 'https://cdn.sanity.io/'.concat(article.mainImage.asset.path), |
| 55 | + slug: article.slug.current, |
| 56 | + } as Article |
| 57 | + }) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +const articlesRepository = new SanityArticlesRepository() |
| 62 | + |
8 | 63 | export default function ArticlePage() { |
9 | | - const allArticles = [ |
10 | | - { |
11 | | - id: '1', |
12 | | - imageUrl: |
13 | | - 'https://images.unsplash.com/photo-1457369804613-52c61a468e7d?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D', |
14 | | - title: 'How to create a complete component using view code for Swift', |
15 | | - slug: 'article-1', |
16 | | - excerpt: |
17 | | - 'Lorem ipsum, dolor sit amet consectetur adipisicing elit. Ex incidunt, excepturi porro vel repellat deserunt culpa veritatis tempora, fugit consectetur expedita fuga hic odio! Molestias necessitatibus hic non maxime itaque...', |
18 | | - author: 'Paolo Prodossimo Lopes', |
19 | | - createdAt: '10 Apr 2024', |
20 | | - }, |
21 | | - { |
22 | | - id: '2', |
23 | | - imageUrl: |
24 | | - 'https://images.unsplash.com/photo-1457369804613-52c61a468e7d?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D', |
25 | | - title: 'How to create a complete component using view code for Swift', |
26 | | - slug: 'article-2', |
27 | | - excerpt: |
28 | | - 'Lorem ipsum, dolor sit amet consectetur adipisicing elit. Ex incidunt, excepturi porro vel repellat deserunt culpa veritatis tempora, fugit consectetur expedita fuga hic odio! Molestias necessitatibus hic non maxime itaque...', |
29 | | - author: 'Paolo Prodossimo Lopes', |
30 | | - createdAt: '10 Apr 2024', |
31 | | - }, |
32 | | - { |
33 | | - id: '3', |
34 | | - imageUrl: |
35 | | - 'https://images.unsplash.com/photo-1457369804613-52c61a468e7d?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D', |
36 | | - title: 'How to create a complete component using view code for Swift', |
37 | | - slug: 'article-3', |
38 | | - excerpt: |
39 | | - 'Lorem ipsum, dolor sit amet consectetur adipisicing elit. Ex incidunt, excepturi porro vel repellat deserunt culpa veritatis tempora, fugit consectetur expedita fuga hic odio! Molestias necessitatibus hic non maxime itaque...', |
40 | | - author: 'Paolo Prodossimo Lopes', |
41 | | - createdAt: '10 Apr 2024', |
42 | | - }, |
43 | | - { |
44 | | - id: '4', |
45 | | - imageUrl: |
46 | | - 'https://images.unsplash.com/photo-1457369804613-52c61a468e7d?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D', |
47 | | - title: 'How to create a complete component using view code for Swift', |
48 | | - slug: 'article-1', |
49 | | - excerpt: |
50 | | - 'Lorem ipsum, dolor sit amet consectetur adipisicing elit. Ex incidunt, excepturi porro vel repellat deserunt culpa veritatis tempora, fugit consectetur expedita fuga hic odio! Molestias necessitatibus hic non maxime itaque...', |
51 | | - author: 'Paolo Prodossimo Lopes', |
52 | | - createdAt: '10 Apr 2024', |
53 | | - }, |
54 | | - { |
55 | | - id: '5', |
56 | | - imageUrl: |
57 | | - 'https://images.unsplash.com/photo-1457369804613-52c61a468e7d?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D', |
58 | | - title: 'How to create a complete component using view code for Swift', |
59 | | - slug: 'article-2', |
60 | | - excerpt: |
61 | | - 'Lorem ipsum, dolor sit amet consectetur adipisicing elit. Ex incidunt, excepturi porro vel repellat deserunt culpa veritatis tempora, fugit consectetur expedita fuga hic odio! Molestias necessitatibus hic non maxime itaque...', |
62 | | - author: 'Paolo Prodossimo Lopes', |
63 | | - createdAt: '10 Apr 2024', |
64 | | - }, |
65 | | - { |
66 | | - id: '6', |
67 | | - imageUrl: |
68 | | - 'https://images.unsplash.com/photo-1457369804613-52c61a468e7d?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D', |
69 | | - title: 'How to create a complete component using view code for Swift 2', |
70 | | - slug: 'article-3', |
71 | | - excerpt: |
72 | | - 'Lorem ipsum, dolor sit amet consectetur adipisicing elit. Ex incidunt, excepturi porro vel repellat deserunt culpa veritatis tempora, fugit consectetur expedita fuga hic odio! Molestias necessitatibus hic non maxime itaque...', |
73 | | - author: 'Paolo Prodossimo Lopes', |
74 | | - createdAt: '10 Apr 2024', |
75 | | - }, |
76 | | - { |
77 | | - id: '7', |
78 | | - imageUrl: |
79 | | - 'https://images.unsplash.com/photo-1457369804613-52c61a468e7d?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D', |
80 | | - title: 'How to create a complete component using view code for Swift 1', |
81 | | - slug: 'article-1', |
82 | | - excerpt: |
83 | | - 'Lorem ipsum, dolor sit amet consectetur adipisicing elit. Ex incidunt, excepturi porro vel repellat deserunt culpa veritatis tempora, fugit consectetur expedita fuga hic odio! Molestias necessitatibus hic non maxime itaque...', |
84 | | - author: 'Paolo Prodossimo Lopes', |
85 | | - createdAt: '10 Apr 2024', |
86 | | - }, |
87 | | - { |
88 | | - id: '8', |
89 | | - imageUrl: |
90 | | - 'https://images.unsplash.com/photo-1457369804613-52c61a468e7d?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D', |
91 | | - title: 'How to create a complete component using view code for Swift 3', |
92 | | - slug: 'article-2', |
93 | | - excerpt: |
94 | | - 'Lorem ipsum, dolor sit amet consectetur adipisicing elit. Ex incidunt, excepturi porro vel repellat deserunt culpa veritatis tempora, fugit consectetur expedita fuga hic odio! Molestias necessitatibus hic non maxime itaque...', |
95 | | - author: 'Paolo Prodossimo Lopes', |
96 | | - createdAt: '10 Apr 2024', |
97 | | - }, |
98 | | - { |
99 | | - id: '9', |
100 | | - imageUrl: |
101 | | - 'https://images.unsplash.com/photo-1457369804613-52c61a468e7d?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D', |
102 | | - title: 'How to create a complete component using view code for Swift', |
103 | | - slug: 'article-3', |
104 | | - excerpt: |
105 | | - 'Lorem ipsum, dolor sit amet consectetur adipisicing elit. Ex incidunt, excepturi porro vel repellat deserunt culpa veritatis tempora, fugit consectetur expedita fuga hic odio! Molestias necessitatibus hic non maxime itaque...', |
106 | | - author: 'Paolo Prodossimo Lopes', |
107 | | - createdAt: '10 Apr 2024', |
108 | | - }, |
109 | | - { |
110 | | - id: '10', |
111 | | - imageUrl: |
112 | | - 'https://images.unsplash.com/photo-1457369804613-52c61a468e7d?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D', |
113 | | - title: 'How to create a complete component using view code for Swift', |
114 | | - slug: 'article-1', |
115 | | - excerpt: |
116 | | - 'Lorem ipsum, dolor sit amet consectetur adipisicing elit. Ex incidunt, excepturi porro vel repellat deserunt culpa veritatis tempora, fugit consectetur expedita fuga hic odio! Molestias necessitatibus hic non maxime itaque...', |
117 | | - author: 'Paolo Prodossimo Lopes', |
118 | | - createdAt: '10 Apr 2024', |
119 | | - }, |
120 | | - { |
121 | | - id: '11', |
122 | | - imageUrl: |
123 | | - 'https://images.unsplash.com/photo-1457369804613-52c61a468e7d?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D', |
124 | | - title: 'How to create a complete component using view code for Swift', |
125 | | - slug: 'article-2', |
126 | | - excerpt: |
127 | | - 'Lorem ipsum, dolor sit amet consectetur adipisicing elit. Ex incidunt, excepturi porro vel repellat deserunt culpa veritatis tempora, fugit consectetur expedita fuga hic odio! Molestias necessitatibus hic non maxime itaque...', |
128 | | - author: 'Paolo Prodossimo Lopes', |
129 | | - createdAt: '10 Apr 2024', |
130 | | - }, |
131 | | - { |
132 | | - id: '12', |
133 | | - imageUrl: |
134 | | - 'https://images.unsplash.com/photo-1457369804613-52c61a468e7d?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D', |
135 | | - title: |
136 | | - 'How to create a complete component using view code for Swift Batata', |
137 | | - slug: 'article-3', |
138 | | - excerpt: |
139 | | - 'Lorem ipsum, dolor sit amet consectetur adipisicing elit. Ex incidunt, excepturi porro vel repellat deserunt culpa veritatis tempora, fugit consectetur expedita fuga hic odio! Molestias necessitatibus hic non maxime itaque...', |
140 | | - author: 'Paolo Prodossimo Lopes', |
141 | | - createdAt: '10 Apr 2024', |
142 | | - }, |
143 | | - ] |
| 64 | + const [allArticles, setAllArticles] = useState<Article[]>([]) |
144 | 65 | const [articles, setArticles] = useState(allArticles) |
145 | 66 |
|
| 67 | + articlesRepository.findMany().then((articles) => { |
| 68 | + setAllArticles(articles) |
| 69 | + setArticles(articles) |
| 70 | + }) |
| 71 | + |
146 | 72 | function submitActionHandler(search: SearchForm) { |
147 | 73 | if (search.query.length === 0) { |
148 | 74 | setArticles(allArticles) |
|
0 commit comments