Changes done in post and app file
This commit is contained in:
parent
47c8cafe48
commit
7aed6cf077
57
src/App.jsx
57
src/App.jsx
|
@ -1,54 +1,21 @@
|
||||||
import React, { useState, useEffect } from "react";
|
import React from "react";
|
||||||
import Post from "./components/Post";
|
import Post from "./components/Post";
|
||||||
import ActionButtons from "./components/ActionButtons";
|
|
||||||
import CommentForm from "./components/CommentForm";
|
|
||||||
import CommentsModal from "./components/CommentsModal";
|
|
||||||
|
|
||||||
const App = () => {
|
const App = () => {
|
||||||
// State declarations
|
const posts = [
|
||||||
const [likes, setLikes] = useState(0);
|
{
|
||||||
const [comments, setComments] = useState([]);
|
id: 1,
|
||||||
const [newComment, setNewComment] = useState("");
|
user: "John Doe",
|
||||||
const [showComments, setShowComments] = useState(false);
|
time: "2 hours ago",
|
||||||
|
content: "This is a sample post content. Implementing concepts of Likes and Comments.",
|
||||||
// Persist likes and comments in localStorage
|
image: "/images/Blog.webp",
|
||||||
useEffect(() => {
|
|
||||||
localStorage.setItem("likes", likes);
|
|
||||||
}, [likes]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
localStorage.setItem("comments", JSON.stringify(comments));
|
|
||||||
}, [comments]);
|
|
||||||
|
|
||||||
// Handlers
|
|
||||||
const handleLike = () => setLikes((prev) => prev + 1);
|
|
||||||
|
|
||||||
const handleSubmit = (e) => {
|
|
||||||
e.preventDefault();
|
|
||||||
if (newComment.trim()) {
|
|
||||||
setComments((prev) => [...prev, { text: newComment }]);
|
|
||||||
setNewComment("");
|
|
||||||
}
|
}
|
||||||
};
|
];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="container mx-auto p-4 bg-white shadow rounded-lg max-w-2xl">
|
<div className="container mx-auto p-4 bg-white shadow rounded-lg max-w-2xl">
|
||||||
<Post />
|
{posts.map((post) => (
|
||||||
<div className="flex justify-between mb-4 pb-2 border-b">
|
<Post key={post.id} {...post} />
|
||||||
<span>{likes} likes</span>
|
))}
|
||||||
<span>{comments.length} comments</span>
|
|
||||||
</div>
|
|
||||||
<ActionButtons handleLike={handleLike} setShowComments={setShowComments} />
|
|
||||||
<CommentForm
|
|
||||||
newComment={newComment}
|
|
||||||
setNewComment={setNewComment}
|
|
||||||
handleSubmit={handleSubmit}
|
|
||||||
/>
|
|
||||||
<CommentsModal
|
|
||||||
showComments={showComments}
|
|
||||||
setShowComments={setShowComments}
|
|
||||||
comments={comments}
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,25 +1,54 @@
|
||||||
import React from "react";
|
import React, { useState } from "react";
|
||||||
|
import ActionButtons from "./ActionButtons";
|
||||||
|
import CommentForm from "./CommentForm";
|
||||||
|
import CommentsModal from "./CommentsModal";
|
||||||
|
|
||||||
const Post = () => (
|
const Post = ({ user, time, content, image }) => {
|
||||||
<div className="mb-4">
|
const [likes, setLikes] = useState(0);
|
||||||
|
const [comments, setComments] = useState([]);
|
||||||
|
const [newComment, setNewComment] = useState("");
|
||||||
|
const [showComments, setShowComments] = useState(false);
|
||||||
|
|
||||||
|
const handleLike = () => setLikes((prev) => prev + 1);
|
||||||
|
|
||||||
|
const handleSubmit = (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
if (newComment.trim()) {
|
||||||
|
setComments((prev) => [...prev, { text: newComment }]);
|
||||||
|
setNewComment("");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="mb-6 p-4 bg-white shadow rounded-lg">
|
||||||
<div className="flex items-center mb-4">
|
<div className="flex items-center mb-4">
|
||||||
<div className="w-10 h-10 rounded-full bg-gray-200">
|
<div className="w-10 h-10 rounded-full bg-gray-200">
|
||||||
<img src="/images/logo.webp" alt="User Avatar" />
|
<img src="/images/logo.webp" alt={`${user}'s Avatar`} />
|
||||||
</div>
|
</div>
|
||||||
<div className="ml-3">
|
<div className="ml-3">
|
||||||
<p className="font-semibold">John Doe</p>
|
<p className="font-semibold">{user}</p>
|
||||||
<p className="text-gray-500 text-sm">2 hours ago</p>
|
<p className="text-gray-500 text-sm">{time}</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<p className="text-gray-800 mb-4">
|
<p className="text-gray-800 mb-4">{content}</p>
|
||||||
This is a sample post content. Implementing concepts of Likes and Comments.
|
{image && <img src={image} alt="Post content" className="w-full rounded-lg mb-4" />}
|
||||||
</p>
|
<div className="flex justify-between mb-4 pb-2 border-b">
|
||||||
<img
|
<span>{likes} likes</span>
|
||||||
src="/images/Blog.webp"
|
<span>{comments.length} comments</span>
|
||||||
alt="Post content"
|
</div>
|
||||||
className="w-full rounded-lg"
|
<ActionButtons handleLike={handleLike} setShowComments={setShowComments} />
|
||||||
|
<CommentForm
|
||||||
|
newComment={newComment}
|
||||||
|
setNewComment={setNewComment}
|
||||||
|
handleSubmit={handleSubmit}
|
||||||
|
/>
|
||||||
|
<CommentsModal
|
||||||
|
showComments={showComments}
|
||||||
|
setShowComments={setShowComments}
|
||||||
|
comments={comments}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export default Post;
|
export default Post;
|
||||||
|
|
Loading…
Reference in a new issue