posts fix
This commit is contained in:
@@ -1,17 +1,26 @@
|
|||||||
"use client";
|
"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";
|
import React, { useEffect, useState } from "react";
|
||||||
|
|
||||||
export default function PostsPage() {
|
export default function PostsPage() {
|
||||||
const [postText, setPostText] = useState("");
|
const [postText, setPostText] = useState("");
|
||||||
const [posts, setPosts] = useState<any[]>([]); // type of post??
|
const [posts, setPosts] = useState<any[]>([]);
|
||||||
|
const [userReactions, setUserReactions] = useState<{
|
||||||
|
[index: number]: { liked: boolean; warned: boolean };
|
||||||
|
}>({});
|
||||||
const { isAuthenticated, session } = useDevice();
|
const { isAuthenticated, session } = useDevice();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isAuthenticated && session?.username) {
|
if (isAuthenticated && session?.username) {
|
||||||
fetch(`/api/user/${session.username}`)
|
fetch(`/api/user/${session.username}`).then((res) => res.json());
|
||||||
.then((res) => res.json())
|
|
||||||
}
|
}
|
||||||
}, [isAuthenticated, session?.username]);
|
}, [isAuthenticated, session?.username]);
|
||||||
|
|
||||||
@@ -22,7 +31,8 @@ export default function PostsPage() {
|
|||||||
date: new Date().toLocaleString(),
|
date: new Date().toLocaleString(),
|
||||||
likes: 0,
|
likes: 0,
|
||||||
warnings: 0,
|
warnings: 0,
|
||||||
imageUrl: "", // placeholder for image logic
|
imageUrl: "", // placeholder for image logick
|
||||||
|
author: session.username, // add username to track authorship
|
||||||
};
|
};
|
||||||
setPosts([newPost, ...posts]);
|
setPosts([newPost, ...posts]);
|
||||||
setPostText("");
|
setPostText("");
|
||||||
@@ -30,14 +40,36 @@ export default function PostsPage() {
|
|||||||
|
|
||||||
const handleLike = (index: number) => {
|
const handleLike = (index: number) => {
|
||||||
const updatedPosts = [...posts];
|
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);
|
setPosts(updatedPosts);
|
||||||
|
setUserReactions(reactions);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleWarning = (index: number) => {
|
const handleWarning = (index: number) => {
|
||||||
const updatedPosts = [...posts];
|
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);
|
setPosts(updatedPosts);
|
||||||
|
setUserReactions(reactions);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -76,7 +108,7 @@ export default function PostsPage() {
|
|||||||
<div className="w-10 h-10 rounded-full bg-neutral-800 border border-gray-300" />
|
<div className="w-10 h-10 rounded-full bg-neutral-800 border border-gray-300" />
|
||||||
<div>
|
<div>
|
||||||
<p className="font-semibold text-[color:var(--color-warning-300)]">
|
<p className="font-semibold text-[color:var(--color-warning-300)]">
|
||||||
[put username here - i tried] {/*isAuthenticated ? session.username : ""*/}
|
{post.author || "Anonymous"}
|
||||||
</p>
|
</p>
|
||||||
<p className="text-sm text-gray-400">{post.date}</p>
|
<p className="text-sm text-gray-400">{post.date}</p>
|
||||||
</div>
|
</div>
|
||||||
@@ -84,7 +116,7 @@ export default function PostsPage() {
|
|||||||
|
|
||||||
{post.imageUrl && (
|
{post.imageUrl && (
|
||||||
<img
|
<img
|
||||||
src={post.imageUrl} // placeholder logic - friend can wire this to Mongo
|
src={post.imageUrl}
|
||||||
alt="Post related"
|
alt="Post related"
|
||||||
className="w-full max-h-64 object-cover rounded mb-4"
|
className="w-full max-h-64 object-cover rounded mb-4"
|
||||||
/>
|
/>
|
||||||
@@ -95,13 +127,21 @@ export default function PostsPage() {
|
|||||||
<div className="flex gap-4 items-center">
|
<div className="flex gap-4 items-center">
|
||||||
<button
|
<button
|
||||||
onClick={() => handleLike(index)}
|
onClick={() => handleLike(index)}
|
||||||
className="px-3 py-1 rounded bg-success-600 text-white hover:bg-primary-600 text-sm"
|
className={`px-3 py-1 rounded text-sm ${
|
||||||
|
userReactions[index]?.liked
|
||||||
|
? "bg-success-800"
|
||||||
|
: "bg-success-600 hover:bg-primary-600"
|
||||||
|
} text-white`}
|
||||||
>
|
>
|
||||||
👍 Like ({post.likes})
|
👍 Like ({post.likes})
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
onClick={() => handleWarning(index)}
|
onClick={() => handleWarning(index)}
|
||||||
className="px-3 py-1 rounded bg-primary-500 text-white hover:bg-red-600 text-sm"
|
className={`px-3 py-1 rounded text-sm ${
|
||||||
|
userReactions[index]?.warned
|
||||||
|
? "bg-red-800"
|
||||||
|
: "bg-primary-500 hover:bg-red-600"
|
||||||
|
} text-white`}
|
||||||
>
|
>
|
||||||
😭 Stop drinking that ({post.warnings})
|
😭 Stop drinking that ({post.warnings})
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
Reference in New Issue
Block a user