mirror of
https://github.com/bubblecup-12/VogelSocialMedia.git
synced 2025-07-06 15: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 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" });
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
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 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() {
|
||||||
return (
|
const [posts, setPosts] = useState<number[]>([]);
|
||||||
//<Header/>
|
const [loading, setLoading] = useState(false);
|
||||||
<div className="feedContainer">
|
const [hasMore, setHasMore] = useState(true);
|
||||||
|
const feedRef = useRef<HTMLDivElement | null>(null);
|
||||||
|
const PAGE_SIZE = 10;
|
||||||
|
|
||||||
<Header/>
|
const fetchPosts = () => {
|
||||||
<Header/>
|
if (loading) return;
|
||||||
<Header/>
|
setLoading(true);
|
||||||
<Header/>
|
//random shit to differentiate test posts
|
||||||
<Header/>
|
setTimeout(() => {
|
||||||
<Header/>
|
setPosts((prev) => {
|
||||||
<Header/>
|
const newPosts = [];
|
||||||
<Header/>
|
for (let i = 0; i < PAGE_SIZE; i++) {
|
||||||
<Header/>
|
newPosts.push(Math.floor(Math.random() * 100) + 1);
|
||||||
<Header/>
|
}
|
||||||
<Header/>
|
return [...prev, ...newPosts];
|
||||||
<Header/>
|
});
|
||||||
<Header/>
|
setLoading(false);
|
||||||
<Header/>
|
}, 800);
|
||||||
<Header/>
|
};
|
||||||
<Header/>
|
|
||||||
<Header/>
|
|
||||||
<Header/>
|
|
||||||
<Header/>
|
|
||||||
<Header/>
|
|
||||||
<Header/>
|
|
||||||
<Header/>
|
|
||||||
<Header/>
|
|
||||||
<Header/>
|
|
||||||
<Header/>
|
|
||||||
<Footer/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
// <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 {
|
.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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
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