diff --git a/src/Component/Body.js b/src/Component/Body.js
index 5e573de..a6c898b 100644
--- a/src/Component/Body.js
+++ b/src/Component/Body.js
@@ -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
Error: {error.message}
;
- }
+ const indexOfFirstPost = indexOfLastPost - postsPerPage;
+ const currentPosts = filteredData.slice(indexOfFirstPost, indexOfLastPost);
+ const totalPages = Math.ceil(filteredData.length / postsPerPage);
return (
-
-
- {currentPosts.map((post) => (
- -
-
-
Title :{post?.title}
- Content :{post?.body}
-
+
+
+ {filteredData.length > 0 ? (
+ filteredData.map((post) => (
+ -
+
Title: {post.title}
+ Content: {post.body}
+
- ))}
-
-
- No posts found
+ )}
+
+
+
setCurrentPage(page)}>
+ onPageChange={(page) => setCurrentPage(page)}
+ />
);
}
diff --git a/src/Component/Layout.js b/src/Component/Layout.js
index 4927d2b..0c55a54 100644
--- a/src/Component/Layout.js
+++ b/src/Component/Layout.js
@@ -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 (
- {/*
*/}
Latest Blogs
-
-
-
+
-
);
};
diff --git a/src/Component/SearchBar.js b/src/Component/SearchBar.js
index dd08fe0..f7555ec 100644
--- a/src/Component/SearchBar.js
+++ b/src/Component/SearchBar.js
@@ -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..."
/>