Skip to content

Commit 2f04d05

Browse files
committed
61
1 parent ff8d5b8 commit 2f04d05

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

src/components/BlogForm.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useEffect, useState } from 'react';
22
import axios from 'axios';
33
import { useHistory, useParams } from 'react-router';
44
import { bool } from 'prop-types';
5+
import propTypes from 'prop-types';
56

67
const BlogForm = ({ editing }) => {
78
const history = useHistory();
@@ -122,7 +123,7 @@ const BlogForm = ({ editing }) => {
122123
};
123124

124125
BlogForm.propTypes = {
125-
editing: bool
126+
editing: propTypes.bool
126127
}
127128

128129
BlogForm.defaultProps = {

src/components/BlogList.js

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { useState, useEffect, useCallback } from 'react';
33
import Card from '../components/Card';
44
import { useHistory } from 'react-router';
55
import LoadingSpinner from '../components/LoadingSpinner';
6-
import { bool } from 'prop-types';
76
import Pagination from './Pagination';
87
import { useLocation } from 'react-router-dom';
8+
import propTypes from 'prop-types';
99

1010
const BlogList = ({ isAdmin }) => {
1111
const history = useHistory();
@@ -17,6 +17,7 @@ const BlogList = ({ isAdmin }) => {
1717
const [currentPage, setCurrentPage] = useState(1);
1818
const [numberOfPosts, setNumberOfPosts] = useState(0);
1919
const [numberOfPages, setNumberOfPages] = useState(0);
20+
const [searchText, setSearchText] = useState('');
2021
const limit = 5;
2122

2223
useEffect(() => {
@@ -33,6 +34,7 @@ const BlogList = ({ isAdmin }) => {
3334
_limit: limit,
3435
_sort: 'id',
3536
_order: 'desc',
37+
title_like: searchText
3638
}
3739

3840
if (!isAdmin) {
@@ -46,7 +48,7 @@ const BlogList = ({ isAdmin }) => {
4648
setPosts(res.data);
4749
setLoading(false);
4850
})
49-
}, [isAdmin])
51+
}, [isAdmin, searchText])
5052

5153
useEffect(() => {
5254
setCurrentPage(parseInt(pageParam) || 1);
@@ -67,9 +69,6 @@ const BlogList = ({ isAdmin }) => {
6769
);
6870
}
6971

70-
if (posts.length === 0) {
71-
return (<div>No blog posts found</div>)
72-
}
7372
const renderBlogList = () => {
7473
return posts.map(post => {
7574
return (
@@ -91,20 +90,38 @@ const BlogList = ({ isAdmin }) => {
9190
})
9291
}
9392

93+
const onSearch = () => {
94+
getPosts(1);
95+
}
96+
9497
return (
9598
<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+
102119
</div>
103120
)
104121
};
105122

106123
BlogList.propTypes = {
107-
isAdmin: bool
124+
isAdmin: propTypes.bool
108125
};
109126

110127
BlogList.defaultProps = {

0 commit comments

Comments
 (0)