diff --git a/src/app/(app)/posts/page.tsx b/src/app/(app)/posts/page.tsx index 8c7204b..f7da9b4 100644 --- a/src/app/(app)/posts/page.tsx +++ b/src/app/(app)/posts/page.tsx @@ -1,150 +1,175 @@ "use client"; 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 [imageFile, setImageFile] = useState(null); + 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 = async (e: React.FormEvent) => { + e.preventDefault(); - const handleLike = (index: number) => { - const updatedPosts = [...posts]; - const reactions = { ...userReactions }; + if (!imageFile) { + alert("Please select an image to upload."); + return; + } - const alreadyLiked = reactions[index]?.liked; + const formData = new FormData(); + formData.append("image", imageFile); + formData.append("text", postText); - updatedPosts[index].likes += alreadyLiked ? -1 : 1; + try { + const response = await fetch("/api/post", { + method: "POST", + body: formData, + }); - reactions[index] = { - ...reactions[index], - liked: !alreadyLiked, - }; + const data = await response.json(); + if (response.ok) { + alert("Post uploaded successfully!"); + setPosts([data.postData, ...posts]); + setPostText(""); + setImageFile(null); + } else { + alert(data.message || "Failed to upload post."); + } + } catch (error) { + console.error("Error uploading post:", error); + alert("An error occurred while uploading the post."); + } + }; - setPosts(updatedPosts); - setUserReactions(reactions); - }; + const handleLike = (index: number) => { + const updatedPosts = [...posts]; + const reactions = { ...userReactions }; - const handleWarning = (index: number) => { - const updatedPosts = [...posts]; - const reactions = { ...userReactions }; + const alreadyLiked = reactions[index]?.liked; - const alreadyWarned = reactions[index]?.warned; + updatedPosts[index].likes += alreadyLiked ? -1 : 1; - updatedPosts[index].warnings += alreadyWarned ? -1 : 1; + reactions[index] = { + ...reactions[index], + liked: !alreadyLiked, + }; - reactions[index] = { - ...reactions[index], - warned: !alreadyWarned, - }; + setPosts(updatedPosts); + setUserReactions(reactions); + }; - setPosts(updatedPosts); - setUserReactions(reactions); - }; + const handleWarning = (index: number) => { + const updatedPosts = [...posts]; + const reactions = { ...userReactions }; - return ( -
-
-

- Posts -

-
+ const alreadyWarned = reactions[index]?.warned; -
-
-