|
1 | 1 | 'use client' |
2 | 2 |
|
3 | | -import { client } from 'programou/utils/sanity/client' |
4 | | -import { useState } from 'react' |
5 | | -import { z } from 'zod' |
| 3 | +import { useEffect, useState } from 'react' |
6 | 4 | import { Article } from './Article' |
7 | 5 | import { ArticleList } from './ArticleList' |
8 | 6 | import { EmptyState } from './EmptyState' |
| 7 | +import { SanityArticlesRepository } from './SanityArticlesRepository' |
9 | 8 | import { SeachInputField, SearchForm } from './SeachInputField' |
10 | 9 |
|
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 | 10 | const articlesRepository = new SanityArticlesRepository() |
62 | 11 |
|
63 | 12 | export default function ArticlePage() { |
64 | 13 | const [allArticles, setAllArticles] = useState<Article[]>([]) |
65 | 14 | const [articles, setArticles] = useState(allArticles) |
66 | 15 |
|
67 | | - articlesRepository.findMany().then((articles) => { |
68 | | - setAllArticles(articles) |
69 | | - setArticles(articles) |
70 | | - }) |
| 16 | + useEffect(() => { |
| 17 | + articlesRepository.findMany().then((articles) => { |
| 18 | + setAllArticles(articles) |
| 19 | + setArticles(articles) |
| 20 | + }) |
| 21 | + }, [setAllArticles, setArticles]) |
71 | 22 |
|
72 | 23 | function submitActionHandler(search: SearchForm) { |
73 | 24 | if (search.query.length === 0) { |
74 | 25 | setArticles(allArticles) |
75 | 26 | } else { |
76 | 27 | setArticles( |
77 | | - allArticles.filter( |
78 | | - (article) => |
79 | | - article.title.includes(search.query) || |
80 | | - article.excerpt.includes(search.query) || |
81 | | - article.author.includes(search.query), |
82 | | - ), |
| 28 | + allArticles.filter((article) => { |
| 29 | + const searchLower = search.query.toLowerCase() |
| 30 | + return ( |
| 31 | + article.title.toLowerCase().includes(searchLower) || |
| 32 | + article.description.toLowerCase().includes(searchLower) || |
| 33 | + article.author.toLowerCase().includes(searchLower) |
| 34 | + ) |
| 35 | + }), |
83 | 36 | ) |
84 | 37 | } |
85 | 38 | } |
|
0 commit comments