assignment5 Blogpost code review #1
|
@ -1,71 +1,52 @@
|
||||||
import React, { useState, useEffect } from 'react';
|
import React from 'react';
|
||||||
import { api_url } from '../env';
|
|
||||||
import "../styleing/body.css";
|
import "../styleing/body.css";
|
||||||
import Pagination from './Pagination';
|
import Pagination from './Pagination';
|
||||||
|
|
||||||
|
|
||||||
import AddPostForm from './addPost';
|
import AddPostForm from './addPost';
|
||||||
|
|
||||||
function Body() {
|
function Body({ filteredData, setFilteredData, currentPage, setCurrentPage }) {
|
||||||
const [data, setData] = useState([]);
|
|
||||||
const [error, setError] = useState(null);
|
|
||||||
const [filteredData, setFilteredData] = useState([]);
|
|
||||||
const [currentPage,setCurrentPage] = useState(1);
|
|
||||||
const postsPerPage = 6;
|
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 handleAddPost = (newPost) => {
|
||||||
const postWithId = { ...newPost, id: Date.now() };
|
const postWithId = { ...newPost, id: Date.now() };
|
||||||
const updatedData = [postWithId,...data];
|
const updatedData = [postWithId, ...filteredData];
|
||||||
setData(updatedData);
|
|
||||||
setFilteredData(updatedData);
|
setFilteredData(updatedData);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const handleDeletePost = (id) => {
|
const handleDeletePost = (id) => {
|
||||||
const updatedData = data?.filter((post) => post?.id !== id);
|
const updatedData = filteredData.filter((post) => post.id !== id);
|
||||||
setData(updatedData);
|
setFilteredData(updatedData);
|
||||||
setFilteredData(updatedData);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const indexOfLastPost = currentPage * postsPerPage;
|
const indexOfLastPost = currentPage * postsPerPage;
|
||||||
const indexOfFirstPost = indexOfLastPost-postsPerPage;
|
const indexOfFirstPost = indexOfLastPost - postsPerPage;
|
||||||
const currentPosts = filteredData.slice(indexOfFirstPost,indexOfLastPost);
|
const currentPosts = filteredData.slice(indexOfFirstPost, indexOfLastPost);
|
||||||
const totalPages = Math.ceil(filteredData?.length/postsPerPage);
|
const totalPages = Math.ceil(filteredData.length / postsPerPage);
|
||||||
|
|
||||||
if (error) {
|
|
||||||
return <div>Error: {error.message}</div>;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="App">
|
<div className="App">
|
||||||
<ul>
|
<ul>
|
||||||
|
|
||||||
{currentPosts.map((post) => (
|
{filteredData.length > 0 ? (
|
||||||
<li key={post?.id}>
|
filteredData.map((post) => (
|
||||||
|
<li key={post.id}>
|
||||||
<h1> Title :{post?.title}</h1>
|
<h1>Title: {post.title}</h1>
|
||||||
<p> <b>Content :</b>{post?.body}</p>
|
<p><b>Content:</b> {post.body}</p>
|
||||||
<button onClick={() => handleDeletePost(post?.id)}>Delete</button>
|
<button onClick={() => handleDeletePost(post.id)}>Delete</button>
|
||||||
</li>
|
</li>
|
||||||
))}
|
))
|
||||||
</ul>
|
) : (
|
||||||
<AddPostForm onAddPost={handleAddPost} />
|
<li>No posts found</li>
|
||||||
<Pagination
|
)}
|
||||||
|
</ul>
|
||||||
|
<AddPostForm onAddPost={handleAddPost} />
|
||||||
|
<Pagination
|
||||||
currentPage={currentPage}
|
currentPage={currentPage}
|
||||||
totalPages={totalPages}
|
totalPages={totalPages}
|
||||||
onPageChange={(page)=>setCurrentPage(page)}></Pagination>
|
onPageChange={(page) => setCurrentPage(page)}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,18 +3,16 @@ import Body from "./Body";
|
||||||
import SearchBar from "./SearchBar";
|
import SearchBar from "./SearchBar";
|
||||||
import "../styleing/layout.css";
|
import "../styleing/layout.css";
|
||||||
import { api_url } from "../env";
|
import { api_url } from "../env";
|
||||||
veerjot_dml marked this conversation as resolved
|
|||||||
import DarkMode from "./DarkMode";
|
|
||||||
|
|
||||||
const Layout = () => {
|
const Layout = () => {
|
||||||
const [data, setData] = useState([]);
|
const [data, setData] = useState([]);
|
||||||
const [filteredData, setFilteredData] = useState([]);
|
const [filteredData, setFilteredData] = useState([]);
|
||||||
const [currentPage, setCurrentPage] = useState(1);
|
const [currentPage, setCurrentPage] = useState(1);
|
||||||
|
|
||||||
|
|
||||||
const fetchData = async () => {
|
const fetchData = async () => {
|
||||||
veerjot_dml marked this conversation as resolved
veerjot_dml
commented
refactor code and make reusable function /hook refactor code and make reusable function /hook
|
|||||||
try {
|
try {
|
||||||
const res = await fetch(api_url);
|
const res = await fetch(api_url);
|
||||||
const fetchedData = await res?.json();
|
const fetchedData = await res.json();
|
||||||
setData(fetchedData);
|
setData(fetchedData);
|
||||||
setFilteredData(fetchedData);
|
setFilteredData(fetchedData);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -22,39 +20,43 @@ const Layout = () => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const handleSearch = (searchTerm) => {
|
const handleSearch = (searchTerm) => {
|
||||||
if(!searchTerm){
|
if (!searchTerm) {
|
||||||
setFilteredData(data);
|
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]]);
|
||||||
veerjot_dml marked this conversation as resolved
veerjot_dml
commented
fix this fix this
|
|||||||
|
}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(() => {
|
useEffect(() => {
|
||||||
fetchData();
|
fetchData();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="contentPage">
|
<div className="contentPage">
|
||||||
{/* <DarkMode/> */}
|
|
||||||
<div className="header">
|
<div className="header">
|
||||||
<h2 className="heading">Latest Blogs</h2>
|
<h2 className="heading">Latest Blogs</h2>
|
||||||
<SearchBar onSearch={handleSearch} />
|
<SearchBar onSearch={handleSearch} />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="body">
|
<div className="body">
|
||||||
|
<Body
|
||||||
<Body data={filteredData} currentPage={currentPage} setCurrentPage={setCurrentPage} />
|
filteredData={filteredData}
|
||||||
|
setFilteredData={setFilteredData}
|
||||||
|
currentPage={currentPage}
|
||||||
|
setCurrentPage={setCurrentPage}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
import React, { useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
import '../styleing/SearchBar.css';
|
import '../styleing/SearchBar.css';
|
||||||
|
|
||||||
|
@ -8,7 +7,7 @@ const SearchBar = ({ onSearch }) => {
|
||||||
const handleInputChange = (event) => {
|
const handleInputChange = (event) => {
|
||||||
const value = event.target.value;
|
const value = event.target.value;
|
||||||
setSearchTerm(value);
|
setSearchTerm(value);
|
||||||
onSearch(value);
|
onSearch(value);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -17,7 +16,7 @@ const SearchBar = ({ onSearch }) => {
|
||||||
type="text"
|
type="text"
|
||||||
className="search"
|
className="search"
|
||||||
value={searchTerm}
|
value={searchTerm}
|
||||||
onChange={handleInputChange}
|
onChange={handleInputChange}
|
||||||
placeholder="Search for posts..."
|
placeholder="Search for posts..."
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in a new issue
wrong way of importing env fix this