Skip to content

Commit 3115225

Browse files
committed
59
1 parent 4a8f444 commit 3115225

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/components/BlogList.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ import { useHistory } from 'react-router';
55
import LoadingSpinner from '../components/LoadingSpinner';
66
import { bool } from 'prop-types';
77
import Pagination from './Pagination';
8+
import { useLocation } from 'react-router-dom';
89

910
const BlogList = ({ isAdmin }) => {
1011
const history = useHistory();
12+
const location = useLocation();
13+
const params = new URLSearchParams(location.search);
14+
const pageParam = params.get('page');
1115
const [posts, setPosts] = useState([]);
1216
const [loading, setLoading] = useState(true);
1317
const [currentPage, setCurrentPage] = useState(1);
@@ -19,8 +23,10 @@ const BlogList = ({ isAdmin }) => {
1923
setNumberOfPages(Math.ceil(numberOfPosts/limit));
2024
}, [numberOfPosts]);
2125

26+
const onClickPageButton = (page) => {
27+
history.push(`${location.pathname}?page=${page}`)
28+
}
2229
const getPosts = (page = 1) => {
23-
setCurrentPage(page);
2430
let params = {
2531
_page: page,
2632
_limit: limit,
@@ -41,17 +47,18 @@ const BlogList = ({ isAdmin }) => {
4147
})
4248
}
4349

50+
useEffect(() => {
51+
setCurrentPage(parseInt(pageParam) || 1);
52+
getPosts(parseInt(pageParam) || 1);
53+
}, [pageParam]);
54+
4455
const deleteBlog = (e, id) => {
4556
e.stopPropagation();
4657

4758
axios.delete(`http://localhost:3001/posts/${id}`).then(() => {
4859
setPosts(prevPosts => prevPosts.filter(post => post.id !== id));
4960
});
5061
};
51-
52-
useEffect(() => {
53-
getPosts();
54-
}, []);
5562

5663
if (loading) {
5764
return (
@@ -89,7 +96,7 @@ const BlogList = ({ isAdmin }) => {
8996
{numberOfPages > 1 && <Pagination
9097
currentPage={currentPage}
9198
numberOfPages={numberOfPages}
92-
onClick={getPosts}
99+
onClick={onClickPageButton}
93100
/>}
94101
</div>
95102
)

0 commit comments

Comments
 (0)