assignment5 Blogpost code review #1

Merged
sumitdml123 merged 6 commits from dev into main 2025-01-14 11:35:16 +00:00
3 changed files with 55 additions and 73 deletions
Showing only changes of commit 0147c13674 - Show all commits

View file

@ -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 updatedData = [postWithId, ...filteredData];
setFilteredData(updatedData);
};
const handleDeletePost = (id) => {
const updatedData = data?.filter((post) => post?.id !== id);
setData(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>
{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>
))}
))
) : (
<li>No posts found</li>
)}
</ul>
<AddPostForm onAddPost={handleAddPost} />
<Pagination
currentPage={currentPage}
totalPages={totalPages}
onPageChange={(page)=>setCurrentPage(page)}></Pagination>
onPageChange={(page) => setCurrentPage(page)}
/>
</div>
);
}

View file

@ -3,18 +3,16 @@ import Body from "./Body";
import SearchBar from "./SearchBar";
import "../styleing/layout.css";
import { api_url } from "../env";
veerjot_dml marked this conversation as resolved
Review

wrong way of importing env fix this

wrong way of importing env fix this
import DarkMode from "./DarkMode";
const Layout = () => {
const [data, setData] = useState([]);
const [filteredData, setFilteredData] = useState([]);
const [currentPage, setCurrentPage] = useState(1);
const fetchData = async () => {
veerjot_dml marked this conversation as resolved
Review

refactor code and make reusable function /hook

refactor code and make reusable function /hook
try {
const res = await fetch(api_url);
const fetchedData = await res?.json();
const fetchedData = await res.json();
setData(fetchedData);
setFilteredData(fetchedData);
} catch (error) {
@ -24,16 +22,19 @@ const Layout = () => {
const handleSearch = (searchTerm) => {
if(!searchTerm){
if (!searchTerm) {
setFilteredData(data);
}
else{
} 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())
);
setFilteredData(filtered);
if(filtered.length>0){
setFilteredData([filtered[0]]);
veerjot_dml marked this conversation as resolved
Review

fix this

fix this
}else{
setFilteredData([]);
}
setCurrentPage(1);
}
};
@ -44,17 +45,18 @@ const Layout = () => {
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>
);
};

View file

@ -1,4 +1,3 @@
import React, { useState } from 'react';
import '../styleing/SearchBar.css';