diff --git a/src/app/(app)/posts/page.tsx b/src/app/(app)/posts/page.tsx index fd62a42..574eff1 100644 --- a/src/app/(app)/posts/page.tsx +++ b/src/app/(app)/posts/page.tsx @@ -1,19 +1,28 @@ "use client"; -import { useDevice } from "@/lib/context/DeviceContext"; + +// HELP NOT WORKING -> import { useDevice } from "@/lib/context/DeviceContext"; USERNAME!! +const useDevice = () => ({ + isAuthenticated: true, + session: { username: "demo_user" }, + }); + + import React, { useEffect, useState } from "react"; export default function PostsPage() { const [postText, setPostText] = useState(""); - const [posts, setPosts] = useState([]); // type of post?? + const [posts, setPosts] = useState([]); + const [userReactions, setUserReactions] = useState<{ + [index: number]: { liked: boolean; warned: boolean }; + }>({}); const { isAuthenticated, session } = useDevice(); useEffect(() => { - if (isAuthenticated && session?.username) { - fetch(`/api/user/${session.username}`) - .then((res) => res.json()) - } - }, [isAuthenticated, session?.username]); + if (isAuthenticated && session?.username) { + fetch(`/api/user/${session.username}`).then((res) => res.json()); + } + }, [isAuthenticated, session?.username]); const handlePostSubmit = (e: React.FormEvent) => { e.preventDefault(); @@ -22,7 +31,8 @@ export default function PostsPage() { date: new Date().toLocaleString(), likes: 0, warnings: 0, - imageUrl: "", // placeholder for image logic + imageUrl: "", // placeholder for image logick + author: session.username, // add username to track authorship }; setPosts([newPost, ...posts]); setPostText(""); @@ -30,21 +40,43 @@ export default function PostsPage() { const handleLike = (index: number) => { const updatedPosts = [...posts]; - updatedPosts[index].likes += 1; + const reactions = { ...userReactions }; + + const alreadyLiked = reactions[index]?.liked; + + updatedPosts[index].likes += alreadyLiked ? -1 : 1; + + reactions[index] = { + ...reactions[index], + liked: !alreadyLiked, + }; + setPosts(updatedPosts); + setUserReactions(reactions); }; const handleWarning = (index: number) => { const updatedPosts = [...posts]; - updatedPosts[index].warnings += 1; + const reactions = { ...userReactions }; + + const alreadyWarned = reactions[index]?.warned; + + updatedPosts[index].warnings += alreadyWarned ? -1 : 1; + + reactions[index] = { + ...reactions[index], + warned: !alreadyWarned, + }; + setPosts(updatedPosts); + setUserReactions(reactions); }; return (

- Posts + Posts

@@ -76,7 +108,7 @@ export default function PostsPage() {

- [put username here - i tried] {/*isAuthenticated ? session.username : ""*/} + {post.author || "Anonymous"}

{post.date}

@@ -84,7 +116,7 @@ export default function PostsPage() { {post.imageUrl && ( Post related @@ -95,13 +127,21 @@ export default function PostsPage() {
@@ -113,4 +153,4 @@ export default function PostsPage() {
); -} \ No newline at end of file +}