mirror of
https://github.com/bubblecup-12/VogelSocialMedia.git
synced 2025-07-06 05:18:48 +00:00
Feed works with random testposts
This commit is contained in:
parent
e3e6649a51
commit
357f370b56
5 changed files with 141 additions and 44 deletions
|
@ -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" });
|
||||
}
|
||||
};
|
||||
|
||||
|
|
30
code/frontend/src/TestPost.tsx
Normal file
30
code/frontend/src/TestPost.tsx
Normal 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;
|
|
@ -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 (
|
||||
//<Header/>
|
||||
<div className="feedContainer">
|
||||
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;
|
||||
|
||||
<Header/>
|
||||
<Header/>
|
||||
<Header/>
|
||||
<Header/>
|
||||
<Header/>
|
||||
<Header/>
|
||||
<Header/>
|
||||
<Header/>
|
||||
<Header/>
|
||||
<Header/>
|
||||
<Header/>
|
||||
<Header/>
|
||||
<Header/>
|
||||
<Header/>
|
||||
<Header/>
|
||||
<Header/>
|
||||
<Header/>
|
||||
<Header/>
|
||||
<Header/>
|
||||
<Header/>
|
||||
<Header/>
|
||||
<Header/>
|
||||
<Header/>
|
||||
<Header/>
|
||||
<Header/>
|
||||
<Footer/>
|
||||
</div>
|
||||
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);
|
||||
};
|
||||
|
||||
// <Footer/>
|
||||
);
|
||||
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 (
|
||||
<div className="feedContainer">
|
||||
<main className="feedContent" ref={feedRef}>
|
||||
{posts.map((postId, idx) => (
|
||||
<TestPost key={idx} postId={postId} />
|
||||
))}
|
||||
{loading && <div className="loading">Loading more posts...</div>}
|
||||
</main>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default Feed;
|
||||
export default Feed;
|
||||
|
|
|
@ -1,5 +1,36 @@
|
|||
|
||||
.feedContainer {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
;
|
||||
}
|
||||
display: flex;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
17
code/frontend/src/testPost.css
Normal file
17
code/frontend/src/testPost.css
Normal 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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue