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

- Posts -

-
+ return ( +
+
+

+ Posts +

+
-
-
-