From 357f370b56072369a057d931d06ffef8c50b77d7 Mon Sep 17 00:00:00 2001 From: MisbehavedNinjaRadiator <120029998+MisbehavedNinjaRadiator@users.noreply.github.com.> Date: Wed, 18 Jun 2025 15:48:21 +0200 Subject: [PATCH] Feed works with random testposts --- .../backend/src/controllers/postController.ts | 8 +- code/frontend/src/TestPost.tsx | 30 ++++++ code/frontend/src/feed/Feed.tsx | 91 +++++++++++-------- code/frontend/src/feed/feed.css | 39 +++++++- code/frontend/src/testPost.css | 17 ++++ 5 files changed, 141 insertions(+), 44 deletions(-) create mode 100644 code/frontend/src/TestPost.tsx create mode 100644 code/frontend/src/testPost.css diff --git a/code/backend/src/controllers/postController.ts b/code/backend/src/controllers/postController.ts index 941ad72..2cdc0f1 100644 --- a/code/backend/src/controllers/postController.ts +++ b/code/backend/src/controllers/postController.ts @@ -13,6 +13,7 @@ export const uploadPost = async (req: Request, res: Response) => { const user: JwtPayload = req.user!; // Get the user from the request const { description, status, tags } = uploadPostSchema.parse(req.body); const BUCKET = "images"; // Name of the bucket where the images are stored + try { const uploads = await Promise.all( files.map(async (file) => { @@ -57,16 +58,15 @@ export const uploadPost = async (req: Request, res: Response) => { })), }, }, - }); // create a new post in the database + }); + // create a new post in the database res.status(StatusCodes.CREATED).json({ message: "Upload successful", post: post, }); } catch (err) { console.error(err); - res - .status(StatusCodes.INTERNAL_SERVER_ERROR) - .json({ error: "Upload failed" }); + res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error: "Upload failed" }); } }; diff --git a/code/frontend/src/TestPost.tsx b/code/frontend/src/TestPost.tsx new file mode 100644 index 0000000..3b78cb4 --- /dev/null +++ b/code/frontend/src/TestPost.tsx @@ -0,0 +1,30 @@ +import React, { useMemo } from "react"; +import "./testPost.css"; + +interface TestPostProps { + postId: number; +} + +const getRandomColor = () => { + const letters = "0123456789ABCDEF"; + let color = "#"; + for (let i = 0; i < 6; i++) { + color += letters[Math.floor(Math.random() * 16)]; + } + return color; +}; + +const TestPost: React.FC = ({ postId }) => { + const bgColor = useMemo(() => getRandomColor(), []); + + return ( +
+ {postId} +
+ ); +}; + +export default TestPost; diff --git a/code/frontend/src/feed/Feed.tsx b/code/frontend/src/feed/Feed.tsx index 00e5ee6..2299266 100644 --- a/code/frontend/src/feed/Feed.tsx +++ b/code/frontend/src/feed/Feed.tsx @@ -1,42 +1,61 @@ -import React from "react"; -import Header from "../header"; -import Footer from "../footer/Footer"; +import React, { useState, useEffect, useRef } from "react"; +import TestPost from "../TestPost"; +import "./feed.css"; function Feed() { - return ( - //
-
+ const [posts, setPosts] = useState([]); + const [loading, setLoading] = useState(false); + const [hasMore, setHasMore] = useState(true); + const feedRef = useRef(null); + const PAGE_SIZE = 10; -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+ const fetchPosts = () => { + if (loading) return; + setLoading(true); +//random shit to differentiate test posts + setTimeout(() => { + setPosts((prev) => { + const newPosts = []; + for (let i = 0; i < PAGE_SIZE; i++) { + newPosts.push(Math.floor(Math.random() * 100) + 1); + } + return [...prev, ...newPosts]; + }); + setLoading(false); + }, 800); + }; - //