diff --git a/admin/src/components/NewPosts.tsx b/admin/src/components/NewPosts.tsx index 3edcd1b2..47259489 100644 --- a/admin/src/components/NewPosts.tsx +++ b/admin/src/components/NewPosts.tsx @@ -1,6 +1,24 @@ import { PiNewspaperClippingLight } from "react-icons/pi" +import axios from "axios"; +import { useEffect, useState } from "react"; +import { IPost } from '../types'; const NewPosts = () => { + const [posts, setposts] = useState([]); + + const fetchPost = async () => { + try { + const response = await axios.get('/api/v1/posts?page=1&pageSize=6'); + setposts(response.data.posts.reverse()); + // console.log(response.data.posts); + } catch (error) { + console.log(error); + } + }; + + useEffect(() => { + fetchPost(); + }, []); return (
@@ -11,84 +29,41 @@ const NewPosts = () => { New Posts
-
- +
+
- - - - - - - - + + + + + + + + - - - - - - - - - - - + + - - - - + + + + + ))}
- Title - - Author - - createdAt - - Likes - - Comments - - Action -
TitleAuthorCreated AtCommentsReactionsAction
- Navbar component - -
-
Neil Sims
-
neil.sims@flowbite.com
-
-
- 10th June 2024 - - 7 - - 7 - - -
- Navbar component - + {posts.slice(0,6).map(posts => ( +
{posts.title}
-
Neil Sims
-
neil.sims@flowbite.com
-
-
- 10th June 2024 - - 7 - - 7 - +
{posts.author.username}
+
{posts.author.email}
+ + +
{new Date(posts.createdAt).toLocaleDateString()}{posts.comments.length}{posts.reactions.length} -
-
+
) } diff --git a/admin/src/components/TrendingPosts.tsx b/admin/src/components/TrendingPosts.tsx index 848153bd..5268e3eb 100644 --- a/admin/src/components/TrendingPosts.tsx +++ b/admin/src/components/TrendingPosts.tsx @@ -1,96 +1,72 @@ +import axios from "axios"; +import { useEffect, useState } from "react"; import { MdAutoGraph } from "react-icons/md"; +import { IPost } from '../types'; const TrendingPosts = () => { + const [trendingPosts, setTrendingPosts] = useState([]); + + const fetchPost = async () => { + try { + const response = await axios.get('/api/v1/posts/trending'); + setTrendingPosts(response.data.trendingPosts); + // console.log(response.data.trendingPosts); + } catch (error) { + console.log(error); + } + }; + + useEffect(() => { + fetchPost(); + }, []); + return (
-
+
-
- -
- Trending Posts +
+ +
+ Trending Posts
-
-
- - + +
+
+ - - - - - - - - - - - - - - - - + + + + + + - - + + + {trendingPosts.slice(0,6).map(post => ( + + - - + + + - - - - -
- Title - - Author - - createdAt - - Likes - - Comments - - Action -
- Navbar component - -
-
Neil Sims
-
neil.sims@flowbite.com
-
-
- 10th June 2024 - - 7 - - 7 - - - TitleAuthorCreated AtCommentsReactionsAction
- Navbar component -
{post.title} -
-
Neil Sims
-
neil.sims@flowbite.com
-
+
+
{post.author.username}
+
{post.author.email}
+
- 10th June 2024 - - 7 + {new Date(post.createdAt).toLocaleDateString()}{post.comments.length}{post.reactions.length} + - 7 - - -
+ + ))} + +
- ) -} + ); +}; -export default TrendingPosts +export default TrendingPosts; diff --git a/admin/src/types.ts b/admin/src/types.ts new file mode 100644 index 00000000..c92034ce --- /dev/null +++ b/admin/src/types.ts @@ -0,0 +1,33 @@ +export interface IPost { + id: string; + title: string; + description: string; + codeSnippet: string; + jsCodeSnippet: string; + tags: string[]; + createdAt:number; + author: { + id: string; + username: string; + email: string; + totalFollowers:number + }, + comments:[] + reactions:[]; + favoritePosts: []; + userReaction: 'Like' | 'Celebrate' | 'Support' | 'Love' | 'Insightful' | 'Funny' | null; + } + + export interface IUser { + id: string; + username: string; + email: string; + verified: boolean; + createdAt:string; + posts: IPost[]; + favoritePosts?: IPost[]; + isFollowing: boolean; + _count: { + following: number + } + } \ No newline at end of file diff --git a/backend/src/routes/post/controller.ts b/backend/src/routes/post/controller.ts index fb0cbc03..a06d7ddd 100644 --- a/backend/src/routes/post/controller.ts +++ b/backend/src/routes/post/controller.ts @@ -279,10 +279,15 @@ export const getPostsWithPagination = async (req: Request, res: Response) => { jsCodeSnippet: true, description: true, tags: true, + likes:true, + createdAt:true, + comments:true, + reactions:true, author: { select: { id: true, username: true, + email:true }, }, }, @@ -323,6 +328,9 @@ export const getTrendingPostsController = async (req: Request, res: Response) => jsCodeSnippet: true, description: true, tags: true, + likes:true, + createdAt:true, + comments:true, author: { select: { id: true, diff --git a/backend/src/routes/post/route.ts b/backend/src/routes/post/route.ts index 1bac5f1b..1da7e529 100644 --- a/backend/src/routes/post/route.ts +++ b/backend/src/routes/post/route.ts @@ -1,13 +1,8 @@ import { Router } from "express"; import authMiddleware from "../../middleware/auth" - - - - import { getTrendingPostsController,aiCustomization, createCommentController, createPostController, deletePostController, favoritePostController, getAllTagsController, getCommentsController, getFavoritePostsController, getLeaderboardController, getPostController, getPostReactionsController, getPostsWithPagination, getUserReactionController, reactToPostController, removeReactionController, unfavoritePostController, updatePostController } from "./controller"; - const postRouter = Router(); postRouter.get('/', getPostsWithPagination); diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index d7e8a19a..d6ea8e7c 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -31,8 +31,8 @@ import EditPost from "./pages/EditPost"; import useTheme from './hooks/useTheme'; import CodeEditor from "./pages/CodeEditor"; import TrendingPosts from "./pages/TrendingPosts"; -import axios from "axios"; -axios.defaults.baseURL = "http://localhost:3001/"; +// import axios from "axios"; +// axios.defaults.baseURL = "http://localhost:3001/"; function App() { const { theme, toggleTheme } = useTheme();