diff --git a/src/app/(app)/posts/page.tsx b/src/app/(app)/posts/page.tsx index f7da9b4..056129d 100644 --- a/src/app/(app)/posts/page.tsx +++ b/src/app/(app)/posts/page.tsx @@ -2,33 +2,45 @@ import { useDevice } from "@/lib/context/DeviceContext"; import React, { useEffect, useState } from "react"; +import Post from "../../../lib/components/Post"; export default function PostsPage() { - const [postText, setPostText] = useState(""); const [posts, setPosts] = useState([]); const [userReactions, setUserReactions] = useState<{ [index: number]: { liked: boolean; warned: boolean }; }>({}); const [imageFile, setImageFile] = useState(null); + const [alertMessage, setAlertMessage] = useState(null); // State for alert message const { isAuthenticated, session } = useDevice(); + // Fetch posts on component mount useEffect(() => { - if (isAuthenticated && session?.username) { - fetch(`/api/user/${session.username}`).then((res) => res.json()); + if (isAuthenticated && session?.id) { + fetch(`/api/post`) + .then((res) => res.json()) + .then((data) => { + if (data.posts) { + setPosts(data.posts); + } else { + console.error("Failed to fetch posts:", data.message); + } + }) + .catch((err) => { + console.error("Error fetching posts:", err); + }); } - }, [isAuthenticated, session?.username]); + }, [isAuthenticated, session?.id]); const handlePostSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!imageFile) { - alert("Please select an image to upload."); + setAlertMessage("Please select an image to upload."); return; } const formData = new FormData(); formData.append("image", imageFile); - formData.append("text", postText); try { const response = await fetch("/api/post", { @@ -38,51 +50,109 @@ export default function PostsPage() { const data = await response.json(); if (response.ok) { - alert("Post uploaded successfully!"); - setPosts([data.postData, ...posts]); - setPostText(""); - setImageFile(null); + setAlertMessage(`Post uploaded successfully! You earned ${data.points} points!`); + setPosts([data.postData, ...posts]); // Add the new post to the list + setImageFile(null); // Clear the file input } else { - alert(data.message || "Failed to upload post."); + setAlertMessage(data.message || "Failed to upload post."); } } catch (error) { console.error("Error uploading post:", error); - alert("An error occurred while uploading the post."); + setAlertMessage("An error occurred while uploading the post."); } }; - const handleLike = (index: number) => { - const updatedPosts = [...posts]; + const handleLike = async (index: number) => { + const post = posts[index]; const reactions = { ...userReactions }; - const alreadyLiked = reactions[index]?.liked; + try { + const response = await fetch(`/api/post/${post.id}`, { + method: "POST", + body: new URLSearchParams({ like: "true" }), + }); - updatedPosts[index].likes += alreadyLiked ? -1 : 1; + if (response.ok) { + const updatedPosts = [...posts]; + const alreadyLiked = reactions[index]?.liked; - reactions[index] = { - ...reactions[index], - liked: !alreadyLiked, - }; + // Update reactions in the post + updatedPosts[index].reactions.push({ liked: !alreadyLiked, warned: false }); - setPosts(updatedPosts); - setUserReactions(reactions); + // Update local state + reactions[index] = { + ...reactions[index], + liked: !alreadyLiked, + }; + + setPosts(updatedPosts); + setUserReactions(reactions); + } else { + const data = await response.json(); + alert(data.message || "Failed to like the post."); + } + } catch (error) { + console.error("Error liking post:", error); + alert("An error occurred while liking the post."); + } }; - const handleWarning = (index: number) => { - const updatedPosts = [...posts]; + const handleWarning = async (index: number) => { + const post = posts[index]; const reactions = { ...userReactions }; - const alreadyWarned = reactions[index]?.warned; + try { + const response = await fetch(`/api/post/${post.id}`, { + method: "POST", + body: new URLSearchParams({ warn: "true" }), + }); - updatedPosts[index].warnings += alreadyWarned ? -1 : 1; + if (response.ok) { + const updatedPosts = [...posts]; + const alreadyWarned = reactions[index]?.warned; - reactions[index] = { - ...reactions[index], - warned: !alreadyWarned, - }; + // Update reactions in the post + updatedPosts[index].reactions.push({ liked: false, warned: !alreadyWarned }); - setPosts(updatedPosts); - setUserReactions(reactions); + // Update local state + reactions[index] = { + ...reactions[index], + warned: !alreadyWarned, + }; + + setPosts(updatedPosts); + setUserReactions(reactions); + } else { + const data = await response.json(); + alert(data.message || "Failed to warn the post."); + } + } catch (error) { + console.error("Error warning post:", error); + alert("An error occurred while warning the post."); + } + }; + + const handleDelete = async (index: number) => { + const post = posts[index]; + + try { + const response = await fetch(`/api/post/${post.id}`, { + method: "DELETE", + }); + + if (response.ok) { + alert("Post deleted successfully!"); + const updatedPosts = [...posts]; + updatedPosts.splice(index, 1); // Remove the deleted post from the list + setPosts(updatedPosts); + } else { + const data = await response.json(); + alert(data.message || "Failed to delete the post."); + } + } catch (error) { + console.error("Error deleting post:", error); + alert("An error occurred while deleting the post."); + } }; return ( @@ -93,15 +163,15 @@ export default function PostsPage() { + {/* Alert Message */} + {alertMessage && ( +
+ {alertMessage} +
+ )} +
-