update pagination
This commit is contained in:
parent
0147c13674
commit
f1038b42c7
|
@ -15,25 +15,25 @@ function Body({ filteredData, setFilteredData, currentPage, setCurrentPage }) {
|
|||
|
||||
|
||||
const handleDeletePost = (id) => {
|
||||
const updatedData = filteredData.filter((post) => post.id !== id);
|
||||
const updatedData = filteredData?.filter((post) => post?.id !== id);
|
||||
setFilteredData(updatedData);
|
||||
};
|
||||
|
||||
|
||||
const indexOfLastPost = currentPage * postsPerPage;
|
||||
const indexOfFirstPost = indexOfLastPost - postsPerPage;
|
||||
const currentPosts = filteredData.slice(indexOfFirstPost, indexOfLastPost);
|
||||
const totalPages = Math.ceil(filteredData.length / postsPerPage);
|
||||
const currentPosts = filteredData?.slice(indexOfFirstPost, indexOfLastPost);
|
||||
const totalPages = Math.ceil(filteredData?.length / postsPerPage);
|
||||
|
||||
return (
|
||||
<div className="App">
|
||||
<ul>
|
||||
|
||||
{filteredData.length > 0 ? (
|
||||
filteredData.map((post) => (
|
||||
<li key={post.id}>
|
||||
<h1>Title: {post.title}</h1>
|
||||
<p><b>Content:</b> {post.body}</p>
|
||||
currentPosts.map((post) => (
|
||||
<li key={post?.id}>
|
||||
<h1>Title: {post?.title}</h1>
|
||||
<p><b>Content:</b> {post?.body}</p>
|
||||
<button onClick={() => handleDeletePost(post.id)}>Delete</button>
|
||||
</li>
|
||||
))
|
||||
|
|
|
@ -12,7 +12,7 @@ const Layout = () => {
|
|||
const fetchData = async () => {
|
||||
try {
|
||||
const res = await fetch(api_url);
|
||||
const fetchedData = await res.json();
|
||||
const fetchedData = await res?.json();
|
||||
setData(fetchedData);
|
||||
setFilteredData(fetchedData);
|
||||
} catch (error) {
|
||||
|
@ -27,8 +27,8 @@ const Layout = () => {
|
|||
} else {
|
||||
const filtered = data.filter(
|
||||
(post) =>
|
||||
post?.title.toLowerCase().includes(searchTerm.toLowerCase()) ||
|
||||
post?.body.toLowerCase().includes(searchTerm.toLowerCase())
|
||||
post?.title.toLowerCase().includes(searchTerm?.toLowerCase()) ||
|
||||
post?.body.toLowerCase().includes(searchTerm?.toLowerCase())
|
||||
);
|
||||
if(filtered.length>0){
|
||||
setFilteredData([filtered[0]]);
|
||||
|
|
|
@ -5,7 +5,7 @@ const SearchBar = ({ onSearch }) => {
|
|||
const [searchTerm, setSearchTerm] = useState('');
|
||||
|
||||
const handleInputChange = (event) => {
|
||||
const value = event.target.value;
|
||||
const value = event?.target?.value;
|
||||
setSearchTerm(value);
|
||||
onSearch(value);
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue