@@ -3,9 +3,9 @@ import { useState, useEffect, useCallback } from 'react';
3
3
import Card from '../components/Card' ;
4
4
import { useHistory } from 'react-router' ;
5
5
import LoadingSpinner from '../components/LoadingSpinner' ;
6
- import { bool } from 'prop-types' ;
7
6
import Pagination from './Pagination' ;
8
7
import { useLocation } from 'react-router-dom' ;
8
+ import propTypes from 'prop-types' ;
9
9
10
10
const BlogList = ( { isAdmin } ) => {
11
11
const history = useHistory ( ) ;
@@ -17,6 +17,7 @@ const BlogList = ({ isAdmin }) => {
17
17
const [ currentPage , setCurrentPage ] = useState ( 1 ) ;
18
18
const [ numberOfPosts , setNumberOfPosts ] = useState ( 0 ) ;
19
19
const [ numberOfPages , setNumberOfPages ] = useState ( 0 ) ;
20
+ const [ searchText , setSearchText ] = useState ( '' ) ;
20
21
const limit = 5 ;
21
22
22
23
useEffect ( ( ) => {
@@ -33,6 +34,7 @@ const BlogList = ({ isAdmin }) => {
33
34
_limit : limit ,
34
35
_sort : 'id' ,
35
36
_order : 'desc' ,
37
+ title_like : searchText
36
38
}
37
39
38
40
if ( ! isAdmin ) {
@@ -46,7 +48,7 @@ const BlogList = ({ isAdmin }) => {
46
48
setPosts ( res . data ) ;
47
49
setLoading ( false ) ;
48
50
} )
49
- } , [ isAdmin ] )
51
+ } , [ isAdmin , searchText ] )
50
52
51
53
useEffect ( ( ) => {
52
54
setCurrentPage ( parseInt ( pageParam ) || 1 ) ;
@@ -67,9 +69,6 @@ const BlogList = ({ isAdmin }) => {
67
69
) ;
68
70
}
69
71
70
- if ( posts . length === 0 ) {
71
- return ( < div > No blog posts found</ div > )
72
- }
73
72
const renderBlogList = ( ) => {
74
73
return posts . map ( post => {
75
74
return (
@@ -91,20 +90,38 @@ const BlogList = ({ isAdmin }) => {
91
90
} )
92
91
}
93
92
93
+ const onSearch = ( ) => {
94
+ getPosts ( 1 ) ;
95
+ }
96
+
94
97
return (
95
98
< div >
96
- { renderBlogList ( ) }
97
- { numberOfPages > 1 && < Pagination
98
- currentPage = { currentPage }
99
- numberOfPages = { numberOfPages }
100
- onClick = { onClickPageButton }
101
- /> }
99
+ < input
100
+ type = "text"
101
+ placeholder = "Search.."
102
+ className = "form-control"
103
+ value = { searchText }
104
+ onChange = { ( e ) => setSearchText ( e . target . value ) }
105
+ onKeyUp = { onSearch }
106
+ />
107
+ < hr />
108
+ { posts . length === 0
109
+ ? < div > No blog posts found</ div >
110
+ : < >
111
+ { renderBlogList ( ) }
112
+ { numberOfPages > 1 && < Pagination
113
+ currentPage = { currentPage }
114
+ numberOfPages = { numberOfPages }
115
+ onClick = { onClickPageButton }
116
+ /> }
117
+ </ > }
118
+
102
119
</ div >
103
120
)
104
121
} ;
105
122
106
123
BlogList . propTypes = {
107
- isAdmin : bool
124
+ isAdmin : propTypes . bool
108
125
} ;
109
126
110
127
BlogList . defaultProps = {
0 commit comments