From 1fd749955d7b5427ab0f4e01ed535c075fdc93a0 Mon Sep 17 00:00:00 2001 From: meet Date: Thu, 25 Jul 2024 21:44:09 +0530 Subject: [PATCH] Showing admin which user reacted to which post --- admin/src/App.tsx | 2 + admin/src/components/SideBar.tsx | 2 + admin/src/pages/Reactions.tsx | 95 ++++++++++++++++++++++++++ admin/src/types.ts | 8 +++ backend/src/routes/admin/controller.ts | 42 ++++++++++++ backend/src/routes/admin/route.ts | 4 +- 6 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 admin/src/pages/Reactions.tsx diff --git a/admin/src/App.tsx b/admin/src/App.tsx index 3ccd5b26..1abe8c78 100644 --- a/admin/src/App.tsx +++ b/admin/src/App.tsx @@ -15,6 +15,7 @@ import Graphs from "./pages/Graphs"; import ContactMessages from "./pages/ContactMessages"; import Comments from "./pages/Comments"; import Layout from "./components/Layout"; +import Reactions from "./pages/Reactions"; // import axios from "axios"; // axios.defaults.baseURL = "http://localhost:3001/"; @@ -46,6 +47,7 @@ function App() { } /> } /> } /> + } /> } /> diff --git a/admin/src/components/SideBar.tsx b/admin/src/components/SideBar.tsx index 0e202605..dbf9ce04 100644 --- a/admin/src/components/SideBar.tsx +++ b/admin/src/components/SideBar.tsx @@ -9,6 +9,7 @@ import { VscGraphScatter } from "react-icons/vsc"; import { MdOutlineAttachEmail } from "react-icons/md"; import { FaRegComments } from "react-icons/fa"; import GoogleTranslate from './GoogleTranslate'; +import { VscReactions } from "react-icons/vsc"; const SideBar = ({ sidebarOpen, toggleSidebar }: { sidebarOpen: boolean, toggleSidebar: () => void }) => { const location = useLocation(); @@ -35,6 +36,7 @@ const SideBar = ({ sidebarOpen, toggleSidebar }: { sidebarOpen: boolean, toggleS All Posts Messages Comments + Reactions Statistics diff --git a/admin/src/pages/Reactions.tsx b/admin/src/pages/Reactions.tsx new file mode 100644 index 00000000..b007dcdd --- /dev/null +++ b/admin/src/pages/Reactions.tsx @@ -0,0 +1,95 @@ +import { useState, useEffect } from "react"; +import axios from "axios"; +import { useRecoilValue } from "recoil"; +import { tokenState } from "../store/atoms/auth"; +import { ColorRing } from 'react-loader-spinner'; +import { MdAddReaction } from "react-icons/md"; +import { IReaction} from "../types"; + +const Reactions = () => { + const [reactions, setReactions] = useState([]); + const [loading, setLoading] = useState(true); + const token = useRecoilValue(tokenState); + + document.title = "Style Share Admin | Manage Reactions 📊" + + useEffect(() => { + const fetchReactions = async () => { + try { + const response = await axios.get("/api/v1/admin/getreactions", { + headers: { + Authorization: `Bearer ${token}`, + }, + }); + setReactions(response.data.reactions.reverse()); + setLoading(false); + } catch (error) { + console.error("Error fetching reactions:", error); + setLoading(true); + } + }; + + fetchReactions(); + }, [token]); + + return ( +
+
+
+ +
+ +
+ All Reactions +
+
+ {loading ? +
+ +
+ : +
+ + + + + + + + + + + + + {reactions.map(reaction => ( + + + + + + + + + ))} + +
PhotoUserReaction TypePost TitleAuthorReacted At
+ profile-pic + +
+ {reaction.user.username} + {reaction.user.email} +
+
{reaction.type}{reaction.post.title}{reaction.post.author.username}{new Date(reaction.createdAt).toLocaleDateString()}
+
+ } +
+
+ ); +}; + +export default Reactions; diff --git a/admin/src/types.ts b/admin/src/types.ts index 159887c3..fa5347fe 100644 --- a/admin/src/types.ts +++ b/admin/src/types.ts @@ -37,6 +37,7 @@ export interface IPost { comments:[]; following: []; isFollowing: boolean; + avatar?:string; } export interface IStats { @@ -55,4 +56,11 @@ export interface IContactMessage{ subject:string, message:string, createdAt:number +} + +export interface IReaction { + type: 'Like' | 'Celebrate' | 'Support' | 'Love' | 'Insightful' | 'Funny'; + createdAt: number; + user: IUser; + post: IPost; } \ No newline at end of file diff --git a/backend/src/routes/admin/controller.ts b/backend/src/routes/admin/controller.ts index 25555a99..44829f23 100644 --- a/backend/src/routes/admin/controller.ts +++ b/backend/src/routes/admin/controller.ts @@ -472,4 +472,46 @@ export const deleteCommentController = async (req: UserAuthRequest, res: Respons error: "An unexpected exception occurred!", }); } +}; + +export const getPostReactionsController = async (req: Request, res: Response) => { + try { + const reactions = await prisma.reaction.findMany({ + select: { + type: true, + createdAt: true, + user: { + select: { + id: true, + username: true, + email: true, + }, + }, + post: { + select: { + id: true, + title: true, + description: true, + author: { + select: { + id: true, + username: true, + email: true, + }, + }, + }, + }, + }, + }); + + res.status(200).json({ + message: "Successfully fetched all reactions!", + reactions, + }); + } catch (error) { + console.error(error); + res.status(500).json({ + error: "An unexpected exception occurred!", + }); + } }; \ No newline at end of file diff --git a/backend/src/routes/admin/route.ts b/backend/src/routes/admin/route.ts index ead1e6f3..d0b0a219 100644 --- a/backend/src/routes/admin/route.ts +++ b/backend/src/routes/admin/route.ts @@ -1,5 +1,5 @@ import {Router} from 'express'; -import { adminLoginController, adminProfileController, allUserForAdmin, blockUserController, unblockUserController, getAdminPostsController, getAdminTrendingPostsController, getAdminStatsController, getGraphsStatsController, updatePostController, deletePostController, getPostByIdController, getAllContactMessages, deleteCommentController } from './controller'; +import { getPostReactionsController,adminLoginController, adminProfileController, allUserForAdmin, blockUserController, unblockUserController, getAdminPostsController, getAdminTrendingPostsController, getAdminStatsController, getGraphsStatsController, updatePostController, deletePostController, getPostByIdController, getAllContactMessages, deleteCommentController } from './controller'; import { isAdmin } from '../../middleware/adminAuth'; const adminRouter = Router(); @@ -32,4 +32,6 @@ adminRouter.get("/geallcontactmessages", isAdmin,getAllContactMessages); adminRouter.delete('/comments/delete/:commentId', isAdmin, deleteCommentController); +adminRouter.get('/getreactions', isAdmin, getPostReactionsController); + export default adminRouter; \ No newline at end of file