1
1
import { GetStaticProps } from 'next' ;
2
- import { getPrismicClient , listPostPrismic } from '../../services/prismic' ;
2
+ import { GET_STATIC_POST , LIST_POST_PRISMIC , PARAMS_DAFAULT_PRISMIC , SEARCH_LIST_POST_PRISMIC } from '../../services/prismic' ;
3
3
import { Search , ButtonScrollTop , Tag } from '../../components' ;
4
+ import { calcTimeOfRead , dateToPtbr , getImgUrl , getPreviewText , getTag , getTitle , getUid } from '../../helpers/util' ;
4
5
import styles from './styles.module.scss' ;
5
- import Prismic from '@prismicio/client' ;
6
- import { RichText } from 'prismic-dom' ;
7
6
import { useState } from 'react' ;
8
7
import InfiniteScroll from 'react-infinite-scroll-component' ;
9
8
import Head from 'next/head' ;
@@ -24,46 +23,24 @@ interface PostsProps {
24
23
allPosts : Post [ ] ;
25
24
}
26
25
27
- const pageSize = 4 ;
28
- const fetch = 'template-post.title,template-post.content,template-post.image,template-post.tags' ;
29
- const q = '[at(document.type,"template-post")]' ;
30
-
31
26
const handlerPosts = ( posts ) => {
32
27
let countPost = 0 ;
33
28
return posts . map ( post => {
34
29
countPost ++ ;
35
- const excerpt = post . data . content . find ( content => content . type === 'paragraph' ) ?. text ?? '' ;
36
- const isMultipleFour = countPost % 4 === 0 ;
37
- const timeOfRead = calcTimeOfRead ( post )
38
- const previewText = isMultipleFour ? excerpt . slice ( 0 , 600 ) : excerpt . slice ( 0 , 200 ) ;
30
+ const isMultiple = countPost % PARAMS_DAFAULT_PRISMIC . pageSize === 0 ;
31
+
39
32
return {
40
- slug : post . uid ,
41
- title : RichText . asText ( post . data . title ) ,
42
- excerpt : excerpt . length > 200 ? `${ previewText } ...` : '' ,
43
- image : post . data . image . url ?? '' ,
44
- tag : post . data ?. tags ? RichText . asText ( post . data . tags ) : '' ,
45
- timeOfRead,
46
- updatedAt : new Date ( post . last_publication_date ) . toLocaleDateString ( 'pt-BR' ,
47
- {
48
- day : '2-digit' ,
49
- month : '2-digit' ,
50
- year : 'numeric'
51
- } )
33
+ slug : getUid ( post ) ,
34
+ title : getTitle ( post ) ,
35
+ excerpt : getPreviewText ( post , isMultiple ) ,
36
+ image : getImgUrl ( post ) ,
37
+ tag : getTag ( post ) ,
38
+ timeOfRead : calcTimeOfRead ( post ) ,
39
+ updatedAt : dateToPtbr ( post . last_publication_date ) ,
52
40
}
53
41
} )
54
42
}
55
43
56
- const calcTimeOfRead = ( post ) => {
57
- const num_words = RichText . asText ( post . data . content ) ?. split ( ' ' ) . length ;
58
- const num_imgs = 1 ;
59
- let height_imgs = 0 ;
60
- for ( let i = 0 ; i < num_imgs ; i ++ ) {
61
- height_imgs += ( i <= 9 ) ? 12 - i : 3 ;
62
- }
63
- const seconds = ( num_words / 265 * 60 ) + height_imgs ;
64
- return Math . round ( seconds / 60 ) ;
65
- }
66
-
67
44
export default function Posts ( { allPosts } : PostsProps ) {
68
45
const [ currentPage , setCurrentPage ] = useState ( 2 ) ;
69
46
const [ posts , setPosts ] = useState ( allPosts ) ;
@@ -73,13 +50,17 @@ export default function Posts({ allPosts }: PostsProps) {
73
50
74
51
const getMorePost = async ( ) => {
75
52
if ( loadingSearch ) return ;
76
- const params = { currentPage, pageSize, fetch, q } ;
77
- params . q += search ? '[fulltext(document,"' + search + '")]' : '' ;
78
53
79
- const response = await listPostPrismic ( params ) ;
54
+ let response = null ;
55
+ if ( search ) {
56
+ response = await SEARCH_LIST_POST_PRISMIC ( { currentPage } , search )
57
+ } else {
58
+ response = await LIST_POST_PRISMIC ( { currentPage } )
59
+ }
60
+
80
61
const postsResponse = handlerPosts ( response . results ) ;
81
62
82
- if ( ! response . results_size ) {
63
+ if ( ! response ? .results_size ) {
83
64
setHasMore ( false ) ;
84
65
} else {
85
66
setCurrentPage ( currentPage + 1 ) ;
@@ -89,10 +70,8 @@ export default function Posts({ allPosts }: PostsProps) {
89
70
90
71
const searchPosts = async ( searchText ) => {
91
72
setLoadingSearch ( true ) ;
92
- const params = { currentPage : 1 , pageSize, fetch, q } ;
93
- params . q += searchText ? '[fulltext(document,"' + searchText + '")]' : '' ;
94
73
95
- const response = await listPostPrismic ( params ) ;
74
+ const response = await SEARCH_LIST_POST_PRISMIC ( { currentPage : 1 } , searchText ) ;
96
75
const postsResponse = handlerPosts ( response . results ) ;
97
76
98
77
setHasMore ( ! ! response . results_size ) ;
@@ -143,7 +122,7 @@ export default function Posts({ allPosts }: PostsProps) {
143
122
alt = { post . title }
144
123
layout = "fill"
145
124
objectFit = "cover"
146
- >
125
+ >
147
126
</ Image >
148
127
</ div >
149
128
< Link href = { `/posts/${ post . slug } ` } key = { post . slug } >
@@ -152,7 +131,7 @@ export default function Posts({ allPosts }: PostsProps) {
152
131
< time > { post . updatedAt } </ time >
153
132
< div >
154
133
< Image src = "/images/icons/book.png" width = { 20 } height = { 20 } alt = "livro aberto" />
155
- < span > { post . timeOfRead } min</ span > </ div >
134
+ < span > { post . timeOfRead } min</ span > </ div >
156
135
< Tag value = { post . tag } > </ Tag >
157
136
</ div >
158
137
< strong > { post . title } </ strong >
@@ -184,18 +163,8 @@ export default function Posts({ allPosts }: PostsProps) {
184
163
}
185
164
186
165
export const getStaticProps : GetStaticProps = async ( ) => {
187
- const prismic = getPrismicClient ( ) ;
188
-
189
- const response = await prismic . query < any > (
190
- [ Prismic . predicates . at ( 'document.type' , 'template-post' ) ] ,
191
- {
192
- page : 1 ,
193
- fetch : fetch . split ( ',' ) ,
194
- pageSize,
195
- orderings : '[document.last_publication_date desc]'
196
- } ,
197
- )
198
- const allPosts = handlerPosts ( response . results ) ;
166
+ const response = GET_STATIC_POST ( ) ;
167
+ const allPosts = handlerPosts ( response ) ;
199
168
200
169
return {
201
170
props : {
0 commit comments