Feed works with random testposts

This commit is contained in:
MisbehavedNinjaRadiator 2025-06-18 15:48:21 +02:00 committed by MisbehavedNinjaRadiator
parent e3e6649a51
commit 357f370b56
5 changed files with 141 additions and 44 deletions

View file

@ -13,6 +13,7 @@ export const uploadPost = async (req: Request, res: Response) => {
const user: JwtPayload = req.user!; // Get the user from the request const user: JwtPayload = req.user!; // Get the user from the request
const { description, status, tags } = uploadPostSchema.parse(req.body); const { description, status, tags } = uploadPostSchema.parse(req.body);
const BUCKET = "images"; // Name of the bucket where the images are stored const BUCKET = "images"; // Name of the bucket where the images are stored
try { try {
const uploads = await Promise.all( const uploads = await Promise.all(
files.map(async (file) => { 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({ res.status(StatusCodes.CREATED).json({
message: "Upload successful", message: "Upload successful",
post: post, post: post,
}); });
} catch (err) { } catch (err) {
console.error(err); console.error(err);
res res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error: "Upload failed" });
.status(StatusCodes.INTERNAL_SERVER_ERROR)
.json({ error: "Upload failed" });
} }
}; };

View file

@ -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<TestPostProps> = ({ postId }) => {
const bgColor = useMemo(() => getRandomColor(), []);
return (
<div
className="testPostCard"
style={{ backgroundColor: bgColor }}
>
<span className="testPostNumber">{postId}</span>
</div>
);
};
export default TestPost;

View file

@ -1,41 +1,60 @@
import React from "react"; import React, { useState, useEffect, useRef } from "react";
import Header from "../header"; import TestPost from "../TestPost";
import Footer from "../footer/Footer"; import "./feed.css";
function Feed() { function Feed() {
const [posts, setPosts] = useState<number[]>([]);
const [loading, setLoading] = useState(false);
const [hasMore, setHasMore] = useState(true);
const feedRef = useRef<HTMLDivElement | null>(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);
};
useEffect(() => {
fetchPosts();
}, []);
useEffect(() => {
const onScroll = () => {
const feed = feedRef.current;
if (!feed || loading || !hasMore) return;
if (feed.scrollTop + feed.clientHeight >= feed.scrollHeight - 100) {
fetchPosts();
}
};
const feed = feedRef.current;
feed?.addEventListener("scroll", onScroll);
return () => {
feed?.removeEventListener("scroll", onScroll);
};
}, [loading, hasMore]);
return ( return (
//<Header/>
<div className="feedContainer"> <div className="feedContainer">
<main className="feedContent" ref={feedRef}>
<Header/> {posts.map((postId, idx) => (
<Header/> <TestPost key={idx} postId={postId} />
<Header/> ))}
<Header/> {loading && <div className="loading">Loading more posts...</div>}
<Header/> </main>
<Header/>
<Header/>
<Header/>
<Header/>
<Header/>
<Header/>
<Header/>
<Header/>
<Header/>
<Header/>
<Header/>
<Header/>
<Header/>
<Header/>
<Header/>
<Header/>
<Header/>
<Header/>
<Header/>
<Header/>
<Footer/>
</div> </div>
// <Footer/>
); );
} }

View file

@ -1,5 +1,36 @@
.feedContainer { .feedContainer {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
; height: 100vh;
overflow: hidden;
background-color: #f9f9f9;
} }
.feedContent {
flex: 1;
overflow-y: auto;
padding: 1rem;
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 1rem;
}
.loading {
width: 100%;
text-align: center;
margin-top: 1rem;
font-weight: bold;
color: #333;
}
/* Desktop responsive behavior */
@media (min-width: 768px) {
.feedContent {
width: 600px;
margin: 0 auto;
padding: 2rem 0;
}
}

View file

@ -0,0 +1,17 @@
.testPostCard {
width: 100%;
height: 180px;
border-radius: 10px;
display: flex;
justify-content: center;
align-items: center;
font-size: 3rem;
font-weight: bold;
color: white;
user-select: none;
box-shadow: 0 3px 6px rgba(0, 0, 0, 0.2);
}
.testPostNumber {
pointer-events: none;
}