updating filter option
This commit is contained in:
parent
8ee2146a5e
commit
0147c13674
|
@ -1,71 +1,52 @@
|
|||
import React, { useState, useEffect } from 'react';
|
||||
import { api_url } from '../env';
|
||||
import React from 'react';
|
||||
import "../styleing/body.css";
|
||||
import Pagination from './Pagination';
|
||||
|
||||
|
||||
import AddPostForm from './addPost';
|
||||
|
||||
function Body() {
|
||||
const [data, setData] = useState([]);
|
||||
const [error, setError] = useState(null);
|
||||
const [filteredData, setFilteredData] = useState([]);
|
||||
const [currentPage,setCurrentPage] = useState(1);
|
||||
function Body({ filteredData, setFilteredData, currentPage, setCurrentPage }) {
|
||||
const postsPerPage = 6;
|
||||
const fetchData = async () => {
|
||||
try {
|
||||
const res = await fetch(api_url);
|
||||
const d = await res?.json();
|
||||
setData(d);
|
||||
setFilteredData(d)
|
||||
} catch (e) {
|
||||
setError(e);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
fetchData();
|
||||
}, []);
|
||||
|
||||
const handleAddPost = (newPost) => {
|
||||
const postWithId = { ...newPost, id: Date.now() };
|
||||
const updatedData = [postWithId,...data];
|
||||
setData(updatedData);
|
||||
const postWithId = { ...newPost, id: Date.now() };
|
||||
const updatedData = [postWithId, ...filteredData];
|
||||
setFilteredData(updatedData);
|
||||
};
|
||||
|
||||
|
||||
const handleDeletePost = (id) => {
|
||||
const updatedData = data?.filter((post) => post?.id !== id);
|
||||
setData(updatedData);
|
||||
setFilteredData(updatedData);
|
||||
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);
|
||||
|
||||
if (error) {
|
||||
return <div>Error: {error.message}</div>;
|
||||
}
|
||||
const indexOfFirstPost = indexOfLastPost - postsPerPage;
|
||||
const currentPosts = filteredData.slice(indexOfFirstPost, indexOfLastPost);
|
||||
const totalPages = Math.ceil(filteredData.length / postsPerPage);
|
||||
|
||||
return (
|
||||
<div className="App">
|
||||
<ul>
|
||||
|
||||
{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>
|
||||
<ul>
|
||||
|
||||
{filteredData.length > 0 ? (
|
||||
filteredData.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>
|
||||
))}
|
||||
</ul>
|
||||
<AddPostForm onAddPost={handleAddPost} />
|
||||
<Pagination
|
||||
))
|
||||
) : (
|
||||
<li>No posts found</li>
|
||||
)}
|
||||
</ul>
|
||||
<AddPostForm onAddPost={handleAddPost} />
|
||||
<Pagination
|
||||
currentPage={currentPage}
|
||||
totalPages={totalPages}
|
||||
onPageChange={(page)=>setCurrentPage(page)}></Pagination>
|
||||
onPageChange={(page) => setCurrentPage(page)}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -3,18 +3,16 @@ import Body from "./Body";
|
|||
import SearchBar from "./SearchBar";
|
||||
import "../styleing/layout.css";
|
||||
import { api_url } from "../env";
|
||||
import DarkMode from "./DarkMode";
|
||||
|
||||
const Layout = () => {
|
||||
const [data, setData] = useState([]);
|
||||
const [data, setData] = useState([]);
|
||||
const [filteredData, setFilteredData] = useState([]);
|
||||
const [currentPage, setCurrentPage] = useState(1);
|
||||
|
||||
|
||||
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) {
|
||||
|
@ -22,39 +20,43 @@ const Layout = () => {
|
|||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
const handleSearch = (searchTerm) => {
|
||||
if(!searchTerm){
|
||||
setFilteredData(data);
|
||||
if (!searchTerm) {
|
||||
setFilteredData(data);
|
||||
} else {
|
||||
const filtered = data.filter(
|
||||
(post) =>
|
||||
post?.title.toLowerCase().includes(searchTerm.toLowerCase()) ||
|
||||
post?.body.toLowerCase().includes(searchTerm.toLowerCase())
|
||||
);
|
||||
if(filtered.length>0){
|
||||
setFilteredData([filtered[0]]);
|
||||
}else{
|
||||
setFilteredData([]);
|
||||
}
|
||||
setCurrentPage(1);
|
||||
}
|
||||
else{
|
||||
const filtered = data.filter(
|
||||
(post) =>
|
||||
post?.title.toLowerCase().includes(searchTerm?.toLowerCase()) ||
|
||||
post?.body.toLowerCase().includes(searchTerm?.toLowerCase())
|
||||
);
|
||||
setFilteredData(filtered);
|
||||
setCurrentPage(1);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
fetchData();
|
||||
fetchData();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div className="contentPage">
|
||||
{/* <DarkMode/> */}
|
||||
<div className="header">
|
||||
<h2 className="heading">Latest Blogs</h2>
|
||||
<SearchBar onSearch={handleSearch} />
|
||||
</div>
|
||||
|
||||
<div className="body">
|
||||
|
||||
<Body data={filteredData} currentPage={currentPage} setCurrentPage={setCurrentPage} />
|
||||
<Body
|
||||
filteredData={filteredData}
|
||||
setFilteredData={setFilteredData}
|
||||
currentPage={currentPage}
|
||||
setCurrentPage={setCurrentPage}
|
||||
/>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
import React, { useState } from 'react';
|
||||
import '../styleing/SearchBar.css';
|
||||
|
||||
|
@ -8,7 +7,7 @@ const SearchBar = ({ onSearch }) => {
|
|||
const handleInputChange = (event) => {
|
||||
const value = event.target.value;
|
||||
setSearchTerm(value);
|
||||
onSearch(value);
|
||||
onSearch(value);
|
||||
};
|
||||
|
||||
return (
|
||||
|
@ -17,7 +16,7 @@ const SearchBar = ({ onSearch }) => {
|
|||
type="text"
|
||||
className="search"
|
||||
value={searchTerm}
|
||||
onChange={handleInputChange}
|
||||
onChange={handleInputChange}
|
||||
placeholder="Search for posts..."
|
||||
/>
|
||||
</div>
|
||||
|
|
Loading…
Reference in a new issue