Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Fetched Trending and New Posts in admin dashboard successfully Issue 366 #385

Merged
merged 8 commits into from
Jul 8, 2024
117 changes: 49 additions & 68 deletions admin/src/components/NewPosts.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
import { PiNewspaperClippingLight } from "react-icons/pi"
import axios from "axios";
import { useEffect, useState } from "react";
import { IPost } from '../types';
import { useRecoilValue } from "recoil";
import { tokenState } from "../store/atoms/auth";

const NewPosts = () => {
const [posts, setposts] = useState<IPost[]>([]);
const token = useRecoilValue(tokenState);

const fetchPost = async () => {
try {
const response = await axios.get('/api/v1/admin/posts/all',{
headers: {
Authorization: `Bearer ${token}`,
},
});
setposts(response.data.posts.reverse());
} catch (error) {
console.log(error);
}
};

useEffect(() => {
fetchPost();
}, []);
return (
<div>
<div>
Expand All @@ -11,84 +35,41 @@ const NewPosts = () => {
New Posts
</span>
</div>
<div className="lg:mx-24 mx-10 mt-4 lg:mr-11 overflow-x-auto shadow-md rounded-xl mb-5">
<table className="w-full text-sm text-left rtl:text-right text-gray-500 dark:text-gray-400">
<div className="lg:mx-24 mx-10 lg:mr-11 mt-5 overflow-x-auto shadow-md rounded-xl mb-5">
<table className="w-full text-sm text-left rtl:text-right text-gray-500 dark:text-gray-400">
<thead className="text-xs text-white uppercase bg-sky-500">
<tr>
<th scope="col" className="px-6 py-3">
Title
</th>
<th scope="col" className="px-6 py-3">
Author
</th>
<th scope="col" className="px-6 py-3">
createdAt
</th>
<th scope="col" className="px-6 py-3">
Likes
</th>
<th scope="col" className="px-6 py-3">
Comments
</th>
<th scope="col" className="px-6 py-3">
Action
</th>
</tr>
<tr>
<th scope="col" className="px-6 py-3">Title</th>
<th scope="col" className="px-6 py-3">Author</th>
<th scope="col" className="px-3 py-3">Created At</th>
<th scope="col" className="px-6 py-3">Comments</th>
<th scope="col" className="px-6 py-3">Reactions</th>
<th scope="col" className="px-6 py-3">Action</th>
</tr>
</thead>
<tbody>
<tr className="border-b bg-[#000435] border-sky-500 hover:bg-blue-950 hover:text-white">
<td className="px-8 py-4 font-semibold text-white">
Navbar component
</td>
<th className="flex items-center px-6 py-5 text-white">
<div>
<div className="text-base font-bold">Neil Sims</div>
<div className="font-medium text-gray-300 ">[email protected]</div>
</div>
</th>
<td className="px-3 py-4 font-semibold">
10th June 2024
</td>
<td className="px-8 py-4 font-semibold">
7
</td>
<td className="px-12 py-4 font-semibold">
7
</td>
<td className="px-2 py-4 ">
<button className='font-semibold rounded-md p-2 bg-sky-500 text-white px-4 hover:bg-sky-600'>
Manage
</button>
</td>
</tr>
<tr className="border-b bg-[#000435] border-sky-500 hover:bg-blue-950 hover:text-white">
<td className="px-8 py-4 font-semibold text-white">
Navbar component
</td>
<th className="flex items-center px-6 py-5 text-white">
{posts.slice(0,6).map(posts => (
<tr key={posts.id} className="border-b bg-[#000435] border-sky-500 hover:bg-blue-950 hover:text-white">
<td className="px-8 py-4 font-semibold text-white">{posts.title}</td>
<th className="flex items-center px-6 py-5 text-white">
<div>
<div className="text-base font-bold">Neil Sims</div>
<div className="font-medium text-gray-300 ">[email protected]</div>
</div>
</th>
<td className="px-3 py-4 font-semibold">
10th June 2024
</td>
<td className="px-8 py-4 font-semibold">
7
</td>
<td className="px-12 py-4 font-semibold">
7
</td>
<td className="px-2 py-4 ">
<div className="text-base font-bold">{posts.author.username}</div>
<div className="font-medium text-gray-300 ">{posts.author.email}</div>
</div>
</th>
<td className="px-3 py-4 font-semibold">{new Date(posts.createdAt).toLocaleDateString()}</td>
<td className="px-14 py-4 font-semibold">{posts.comments.length}</td>
<td className="px-12 py-4 font-semibold">{posts.reactions.length}</td>
<td className="px-2 py-4">
<button className='font-semibold rounded-md p-2 bg-sky-500 text-white px-4 hover:bg-sky-600'>
Manage
</button>
</td>
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
</div>
)
}
Expand Down
144 changes: 63 additions & 81 deletions admin/src/components/TrendingPosts.tsx
Original file line number Diff line number Diff line change
@@ -1,96 +1,78 @@
import axios from "axios";
import { useEffect, useState } from "react";
import { MdAutoGraph } from "react-icons/md";
import { IPost } from '../types';
import { tokenState } from "../store/atoms/auth";
import { useRecoilValue } from "recoil";

const TrendingPosts = () => {
const [trendingPosts, setTrendingPosts] = useState<IPost[]>([]);
const token = useRecoilValue(tokenState);

const fetchPost = async () => {
try {
const response = await axios.get('/api/v1/admin/posts/trending',{
headers: {
Authorization: `Bearer ${token}`,
},
});
setTrendingPosts(response.data.trendingPosts);
} catch (error) {
console.log(error);
}
};

useEffect(() => {
fetchPost();
}, []);

return (
<div>
<div>
<div>
<span className="flex items-center mx-24 lg:mr-11 text-xl font-bold mt-7 decoration-sky-500 decoration-dotted underline">
<div className='inline-block p-2 text-white bg-[#000435] rounded-lg mr-2'>
<MdAutoGraph size={23}/>
</div>
Trending Posts
<div className='inline-block p-2 text-white bg-[#000435] rounded-lg mr-2'>
<MdAutoGraph size={23} />
</div>
Trending Posts
</span>
</div>
<div className="lg:mx-24 mx-10 lg:mr-11 mt-5 overflow-x-auto shadow-md rounded-xl mb-5">
<table className="w-full text-sm text-left rtl:text-right text-gray-500 dark:text-gray-400">
<thead className="text-xs text-white uppercase bg-sky-500">
</div>
<div className="lg:mx-24 mx-10 lg:mr-11 mt-5 overflow-x-auto shadow-md rounded-xl mb-5">
<table className="w-full text-sm text-left rtl:text-right text-gray-500 dark:text-gray-400">
<thead className="text-xs text-white uppercase bg-sky-500">
<tr>
<th scope="col" className="px-6 py-3">
Title
</th>
<th scope="col" className="px-6 py-3">
Author
</th>
<th scope="col" className="px-6 py-3">
createdAt
</th>
<th scope="col" className="px-6 py-3">
Likes
</th>
<th scope="col" className="px-6 py-3">
Comments
</th>
<th scope="col" className="px-6 py-3">
Action
</th>
</tr>
</thead>
<tbody>
<tr className="border-b bg-[#000435] border-sky-500 hover:bg-blue-950 hover:text-white">
<td className="px-8 py-4 font-semibold text-white">
Navbar component
</td>
<th className="flex items-center px-6 py-5 text-white">
<div>
<div className="text-base font-bold">Neil Sims</div>
<div className="font-medium text-gray-300 ">[email protected]</div>
</div>
</th>
<td className="px-3 py-4 font-semibold">
10th June 2024
</td>
<td className="px-8 py-4 font-semibold">
7
</td>
<td className="px-12 py-4 font-semibold">
7
</td>
<td className="px-2 py-4 ">
<button className='font-semibold rounded-md p-2 bg-sky-500 text-white px-4 hover:bg-sky-600'>
Manage
</button>
</td>
<th scope="col" className="px-6 py-3">Title</th>
<th scope="col" className="px-6 py-3">Author</th>
<th scope="col" className="px-3 py-3">Created At</th>
<th scope="col" className="px-6 py-3">Comments</th>
<th scope="col" className="px-6 py-3">Reactions</th>
<th scope="col" className="px-6 py-3">Action</th>
</tr>
<tr className="border-b bg-[#000435] border-sky-500 hover:bg-blue-950 hover:text-white">
<td className="px-8 py-4 font-semibold text-white">
Navbar component
</td>
</thead>
<tbody>
{trendingPosts.slice(0,6).map(post => (
<tr key={post.id} className="border-b bg-[#000435] border-sky-500 hover:bg-blue-950 hover:text-white">
<td className="px-8 py-4 font-semibold text-white">{post.title}</td>
<th className="flex items-center px-6 py-5 text-white">
<div>
<div className="text-base font-bold">Neil Sims</div>
<div className="font-medium text-gray-300 ">[email protected]</div>
</div>
<div>
<div className="text-base font-bold">{post.author.username}</div>
<div className="font-medium text-gray-300 ">{post.author.email}</div>
</div>
</th>
<td className="px-3 py-4 font-semibold">
10th June 2024
</td>
<td className="px-8 py-4 font-semibold">
7
<td className="px-3 py-4 font-semibold">{new Date(post.createdAt).toLocaleDateString()}</td>
<td className="px-14 py-4 font-semibold">{post.comments.length}</td>
<td className="px-12 py-4 font-semibold">{post.reactions.length}</td>
<td className="px-2 py-4">
<button className='font-semibold rounded-md p-2 bg-sky-500 text-white px-4 hover:bg-sky-600'>
Manage
</button>
</td>
<td className="px-12 py-4 font-semibold">
7
</td>
<td className="px-2 py-4 ">
<button className='font-semibold rounded-md p-2 bg-sky-500 text-white px-4 hover:bg-sky-600'>
Manage
</button>
</td>
</tr>
</tbody>
</table>
</tr>
))}
</tbody>
</table>
</div>
</div>
)
}
);
};

export default TrendingPosts
export default TrendingPosts;
33 changes: 33 additions & 0 deletions admin/src/types.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
Loading
Loading