diff --git a/client/src/pages/CommunityPage.tsx b/client/src/pages/CommunityPage.tsx index b1d4e19..e7ff157 100644 --- a/client/src/pages/CommunityPage.tsx +++ b/client/src/pages/CommunityPage.tsx @@ -59,15 +59,11 @@ export default function CommunityPage() { const [selectedPost, setSelectedPost] = useState(null); const [communityList, setCommunityList] = useState([]); const [search, setSearch] = useState(""); // Search Function - const [userInformation, setUserInformation] = useState>( - {} - ); - const [currentUserInfo, setCurrentUserInfo] = useState(null); - const [imageErrorFlags, setImageErrorFlags] = useState< - Record - >({}); + const [userInformation, setUserInformation] = useState>({}); + const [imageErrorFlags, setImageErrorFlags] = useState>({}); const [tags, setTags] = useState([]); const [selectedTag, setSelectedTag] = useState(null); + const [currentUserId, setCurrentUserId] = useState(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() {
{communityList.map((post) => { const profilePictureUrl = getProfilePicture(post.userId); + const canEditOrDelete = currentUserId === post.userId; + return (
- -
- e.stopPropagation()} - > - - -
- - { - navigate(`edit/${post.id}`); - }} - > - Edit - - handleDeleteClick(post)} - > - Delete - - -
+ {canEditOrDelete && ( + +
+ e.stopPropagation()} + > + + +
+ + { + navigate(`edit/${post.id}`); + }} + > + Edit + + handleDeleteClick(post)} + > + Delete + + +
+ )}
diff --git a/client/src/pages/PostPage.tsx b/client/src/pages/PostPage.tsx index 8447120..4ef8d50 100644 --- a/client/src/pages/PostPage.tsx +++ b/client/src/pages/PostPage.tsx @@ -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(null); const [userInformation, setUserInformation] = useState>({}); const [imageErrorFlags, setImageErrorFlags] = useState>({}); + const [currentUserId, setCurrentUserId] = useState(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,34 +182,36 @@ const PostPage: React.FC = () => {

{userInformation[post.userId]?.firstName} {userInformation[post.userId]?.lastName}

- - - - - - { - navigate(`../edit/${post.id}`); - }} - > - Edit - - handleDeleteClick(post)} - > - Delete - - - + {currentUserId === post.userId && ( // Check if the current user is the post author + + + + + + { + navigate(`../edit/${post.id}`); + }} + > + Edit + + handleDeleteClick(post)} + > + Delete + + + + )}