Changes done
This commit is contained in:
parent
7aed6cf077
commit
3bad991d2d
47
src/App.jsx
47
src/App.jsx
|
@ -5,18 +5,57 @@ const App = () => {
|
||||||
const posts = [
|
const posts = [
|
||||||
{
|
{
|
||||||
id: 1,
|
id: 1,
|
||||||
user: "John Doe",
|
user: "Sachin Tendulkar",
|
||||||
time: "2 hours ago",
|
time: "2 hours ago",
|
||||||
content: "This is a sample post content. Implementing concepts of Likes and Comments.",
|
content: "Hailed as the world's most prolific batsman of all time...",
|
||||||
image: "/images/Blog.webp",
|
image: "/images/sachin.webp",
|
||||||
}
|
},
|
||||||
|
{
|
||||||
|
id: 2,
|
||||||
|
user: "Vansh Vatsa",
|
||||||
|
time: "1 hour ago",
|
||||||
|
content: "Jaspreet Bumrah awesome performance at Australia.",
|
||||||
|
image: "/images/jaspreet.jpeg",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 3,
|
||||||
|
user: "Ro-Hit Sharma",
|
||||||
|
time: "15 mins ago",
|
||||||
|
content: "Rohit Sharma Score 264 against Sri Lanka at the Eden Gardens.",
|
||||||
|
image: "/images/rohit-sharma.avif",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 4,
|
||||||
|
user: "Virat Kohli",
|
||||||
|
time: "1 year ago",
|
||||||
|
content: "Kohli has garnered 10 ICC Awards, making him the most awarded...",
|
||||||
|
image: "/images/kholi.jpg",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 5,
|
||||||
|
user: "MS Dhoni",
|
||||||
|
time: "2 year ago",
|
||||||
|
content: " He has scored 16 centuries and 106 fifties in his international career",
|
||||||
|
image: "/images/dhoni.avif",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 6,
|
||||||
|
user: "Mohammad Siraj",
|
||||||
|
time: "15 mins ago",
|
||||||
|
content: "His contributions were pivotal as India claimed a famous 2-1 away win against Australia. Siraj started to shine as an ODI bowler as well, and was lethal particularly in the powerplay.",
|
||||||
|
image: "/images/siraj.webp",
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
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">
|
||||||
|
<div className="space-y-8">
|
||||||
{posts.map((post) => (
|
{posts.map((post) => (
|
||||||
<Post key={post.id} {...post} />
|
<Post key={post.id} {...post} />
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,27 @@
|
||||||
import React, { useState } from "react";
|
import React, { useState, useEffect } from "react";
|
||||||
import ActionButtons from "./ActionButtons";
|
import ActionButtons from "./ActionButtons";
|
||||||
import CommentForm from "./CommentForm";
|
import CommentForm from "./CommentForm";
|
||||||
import CommentsModal from "./CommentsModal";
|
import CommentsModal from "./CommentsModal";
|
||||||
|
|
||||||
const Post = ({ user, time, content, image }) => {
|
const Post = ({ id, user, time, content, image }) => {
|
||||||
const [likes, setLikes] = useState(0);
|
const [likes, setLikes] = useState(() => {
|
||||||
const [comments, setComments] = useState([]);
|
const storedLikes = localStorage.getItem(`post-${id}-likes`);
|
||||||
|
return storedLikes ? JSON.parse(storedLikes) : 0;
|
||||||
|
});
|
||||||
|
|
||||||
|
const [comments, setComments] = useState(() => {
|
||||||
|
const storedComments = localStorage.getItem(`post-${id}-comments`);
|
||||||
|
return storedComments ? JSON.parse(storedComments) : [];
|
||||||
|
});
|
||||||
|
|
||||||
const [newComment, setNewComment] = useState("");
|
const [newComment, setNewComment] = useState("");
|
||||||
const [showComments, setShowComments] = useState(false);
|
const [showComments, setShowComments] = useState(false);
|
||||||
|
useEffect(() => {
|
||||||
|
localStorage.setItem(`post-${id}-likes`, JSON.stringify(likes));
|
||||||
|
}, [id, likes]);
|
||||||
|
useEffect(() => {
|
||||||
|
localStorage.setItem(`post-${id}-comments`, JSON.stringify(comments));
|
||||||
|
}, [id, comments]);
|
||||||
|
|
||||||
const handleLike = () => setLikes((prev) => prev + 1);
|
const handleLike = () => setLikes((prev) => prev + 1);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue