diff --git a/frontend/src/components/community/CommunityPage.js b/frontend/src/components/community/CommunityPage.js index 11f0a7bc..5bb10e9d 100644 --- a/frontend/src/components/community/CommunityPage.js +++ b/frontend/src/components/community/CommunityPage.js @@ -5,14 +5,13 @@ import "../../styles/community/CommunityPage.css"; import PostCard from "./PostCard"; import "../../styles/Page.css"; import { useNavigate } from "react-router-dom"; -import {apiClient} from "../../service/apiClient"; +import { apiClient } from "../../service/apiClient"; const CommunityPage = () => { const [searchTerm, setSearchTerm] = useState(""); const [sortOrder, setSortOrder] = useState("dsc"); const [searchActive, setSearchActive] = useState(false); const [posts, setPosts] = useState([]); - const [tags, setTags] = useState({}); const [users, setUsers] = useState({}); const navigate = useNavigate(); @@ -20,34 +19,48 @@ const CommunityPage = () => { const fetchPosts = async () => { try { const response = await apiClient.get("/posts"); - const tagsResponse = await apiClient.get("/tags"); - if (!tagsResponse.data) throw new Error("Failed to fetch tags"); - const tagsById = tagsResponse.data.reduce((acc, tag) => { - acc[tag.id] = tag.name; - return acc; - }, {}); - setTags(tagsById); - const usersResponse = await apiClient.get("/users"); - console.log(usersResponse); const usersById = usersResponse.data.reduce((acc, user) => { acc[user.id] = user.username; - console.log(acc); return acc; }, {}); setUsers(usersById); - console.log(usersById); - const transformedPosts = response.data.map((post) => ({ - "post-id": post.id, - user: usersById[post.author] || "Unknown", - title: post.title, - content: [{ type: "plain-text", "plain-text": post.content }], - comments: [], - likes: post.liked_by.length, - tags: post.tags.map((tagId) => tagsById[tagId] || "Unknown"), - "publication-date": new Date(post.created_at).toLocaleDateString(), - })); + const transformedPosts = await Promise.all( + response.data.map(async (post) => { + try { + const commentsResponse = await apiClient.get( + `/comments/post-comments/${post.id}` + ); + + return { + "post-id": post.id, + user: usersById[post.author] || "Unknown", + title: post.title, + content: [{ type: "plain-text", "plain-text": post.content }], + comments: commentsResponse.data, + likes: post.liked_by?.length || 0, + tags: post.tags || [], + "publication-date": new Date(post.created_at), + }; + } catch (error) { + console.error( + `Error fetching comments for post ${post.id}:`, + error + ); + return { + "post-id": post.id, + user: usersById[post.author] || "Unknown", + title: post.title, + content: [{ type: "plain-text", "plain-text": post.content }], + comments: 0, + likes: post.liked_by?.length || 0, + tags: post.tags || [], + "publication-date": new Date(post.created_at), + }; + } + }) + ); setPosts(transformedPosts); } catch (error) { console.error("Error fetching posts:", error); diff --git a/frontend/src/components/community/CreatePostPage.js b/frontend/src/components/community/CreatePostPage.js index c33c389e..b077b17d 100644 --- a/frontend/src/components/community/CreatePostPage.js +++ b/frontend/src/components/community/CreatePostPage.js @@ -1,7 +1,8 @@ import React, { useState, useEffect, useRef } from "react"; import { useNavigate } from "react-router-dom"; import "../../styles/community/CreatePostPage.css"; -import {apiClient} from "../../service/apiClient"; +import { apiClient } from "../../service/apiClient"; +import userService from "../../service/userService"; const CreatePostPage = () => { const navigate = useNavigate(); @@ -29,23 +30,12 @@ const CreatePostPage = () => { }, [navigate]); useEffect(() => { - const mockTags = [ - { id: 1, name: "Stock Analysis" }, - { id: 2, name: "BIST30" }, - { id: 3, name: "S&P" }, - { id: 4, name: "NASDAQ" }, - { id: 5, name: "Dow Jones" }, - { id: 6, name: "USA" }, - { id: 7, name: "Türkiye" }, - ]; - const fetchTags = async () => { try { const response = await apiClient.get("/tags"); setAvailableTags(response.data); } catch (error) { - console.error("Failed to fetch tags, using mock data", error); - setAvailableTags(mockTags); + console.error("Failed to fetch tags!", error); } }; @@ -100,10 +90,11 @@ const CreatePostPage = () => { } const tagIds = selectedTags.map((tag) => tag.id); + const userID = userService.getUserId(); const postData = { title, content: description, - author: 2, + author: userID, liked_by: [], tags: tagIds, portfolios: [], @@ -147,14 +138,17 @@ const CreatePostPage = () => {
- Published on: {new Date(post['publication-date']).toLocaleDateString()} by {post['user']} -
-{post.content[0]["plain-text"]}
-+ Published on:{" "} + {new Date(post["publication-date"]).toLocaleDateString()}{" "} + by {post["user"]} +
+{post.content[0]["plain-text"]}
+Loading tags...
+ )}