Edit/Delete post access control
This commit is contained in:
@@ -59,15 +59,11 @@ export default function CommunityPage() {
|
||||
const [selectedPost, setSelectedPost] = useState<Post | null>(null);
|
||||
const [communityList, setCommunityList] = useState<Post[]>([]);
|
||||
const [search, setSearch] = useState(""); // Search Function
|
||||
const [userInformation, setUserInformation] = useState<Record<string, User>>(
|
||||
{}
|
||||
);
|
||||
const [currentUserInfo, setCurrentUserInfo] = useState(null);
|
||||
const [imageErrorFlags, setImageErrorFlags] = useState<
|
||||
Record<string, boolean>
|
||||
>({});
|
||||
const [userInformation, setUserInformation] = useState<Record<string, User>>({});
|
||||
const [imageErrorFlags, setImageErrorFlags] = useState<Record<string, boolean>>({});
|
||||
const [tags, setTags] = useState<Tag[]>([]);
|
||||
const [selectedTag, setSelectedTag] = useState<string | null>(null);
|
||||
const [currentUserId, setCurrentUserId] = useState<string | null>(null);
|
||||
|
||||
let accessToken = localStorage.getItem("accessToken");
|
||||
if (!accessToken) {
|
||||
@@ -151,9 +147,9 @@ export default function CommunityPage() {
|
||||
useEffect(() => {
|
||||
const getCurrentUserInformation = async () => {
|
||||
try {
|
||||
const user = await retrieveUserInformation(); // Get the user ID
|
||||
setCurrentUserInfo(user); // Set the user ID in the state
|
||||
console.log(currentUserInfo);
|
||||
const user = await retrieveUserInformation(); // Get the user information
|
||||
console.log(user)
|
||||
setCurrentUserId(user.id); // Store user ID
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
@@ -237,6 +233,8 @@ export default function CommunityPage() {
|
||||
<div className="flex flex-col gap-4">
|
||||
{communityList.map((post) => {
|
||||
const profilePictureUrl = getProfilePicture(post.userId);
|
||||
const canEditOrDelete = currentUserId === post.userId;
|
||||
|
||||
return (
|
||||
<section
|
||||
className="flex flex-row gap-4 bg-primary-50 dark:bg-primary-950 border border-none rounded-2xl p-4"
|
||||
@@ -257,6 +255,7 @@ export default function CommunityPage() {
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex flex-row-reverse justify-center items-center">
|
||||
{canEditOrDelete && (
|
||||
<Dropdown>
|
||||
<div>
|
||||
<DropdownTrigger
|
||||
@@ -289,6 +288,7 @@ export default function CommunityPage() {
|
||||
</DropdownItem>
|
||||
</DropdownMenu>
|
||||
</Dropdown>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
|
||||
@@ -27,6 +27,7 @@ import {
|
||||
import { retrieveUserInformationById } from "../security/usersbyid";
|
||||
import CommentInputModule from "../components/CommentInputModule";
|
||||
import CommentsModule from "../components/CommentsModule";
|
||||
import { retrieveUserInformation } from "../security/users";
|
||||
|
||||
interface Post {
|
||||
title: string;
|
||||
@@ -56,6 +57,7 @@ const PostPage: React.FC = () => {
|
||||
const [selectedPost, setSelectedPost] = useState<Post | null>(null);
|
||||
const [userInformation, setUserInformation] = useState<Record<string, User>>({});
|
||||
const [imageErrorFlags, setImageErrorFlags] = useState<Record<string, boolean>>({});
|
||||
const [currentUserId, setCurrentUserId] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (id) {
|
||||
@@ -91,6 +93,19 @@ const PostPage: React.FC = () => {
|
||||
}
|
||||
}, [post]);
|
||||
|
||||
useEffect(() => {
|
||||
const getCurrentUserInformation = async () => {
|
||||
try {
|
||||
const user = await retrieveUserInformation(); // Get the user information
|
||||
console.log(user)
|
||||
setCurrentUserId(user.id); // Store user ID
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
};
|
||||
getCurrentUserInformation();
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
// Scroll to the top of the page when the component mounts
|
||||
window.scrollTo(0, 0);
|
||||
@@ -167,6 +182,7 @@ const PostPage: React.FC = () => {
|
||||
<p className="text-md text-neutral-500">{userInformation[post.userId]?.firstName} {userInformation[post.userId]?.lastName}</p>
|
||||
</div>
|
||||
<div className="flex flex-row-reverse justify-center items-center">
|
||||
{currentUserId === post.userId && ( // Check if the current user is the post author
|
||||
<Dropdown>
|
||||
<DropdownTrigger
|
||||
className="justify-center items-center">
|
||||
@@ -195,6 +211,7 @@ const PostPage: React.FC = () => {
|
||||
</DropdownItem>
|
||||
</DropdownMenu>
|
||||
</Dropdown>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
|
||||
Reference in New Issue
Block a user