From 38fb483a93c040835673f9f1657709f3f296c5be Mon Sep 17 00:00:00 2001 From: andrew Date: Wed, 20 Sep 2023 00:23:55 -0700 Subject: [PATCH] Add paging --- devblog/devblog/ClientApp/src/pages/Posts.tsx | 54 +++++++++++++++++-- .../ClientApp/src/pages/styles/Posts.css | 30 +++++++++++ .../devblog/Controllers/PostsController.cs | 27 ++++++++-- devblog/devblog/Interfaces/IPostService.cs | 11 +++- devblog/devblog/Services/PostService.cs | 18 ++++++- 5 files changed, 127 insertions(+), 13 deletions(-) diff --git a/devblog/devblog/ClientApp/src/pages/Posts.tsx b/devblog/devblog/ClientApp/src/pages/Posts.tsx index 95fc54cc..6ba464c3 100644 --- a/devblog/devblog/ClientApp/src/pages/Posts.tsx +++ b/devblog/devblog/ClientApp/src/pages/Posts.tsx @@ -1,4 +1,6 @@ import { useEffect, useState } from "react"; +import { MdChevronLeft } from "react-icons/md" +import { MdChevronRight } from "react-icons/md" import IPost from "../interfaces/IPostProps"; import Post from "../components/Post"; import { GetIsAdmin } from "../components/AuthenticationService"; @@ -8,22 +10,64 @@ import "./styles/Posts.css"; const Posts = () => { const [posts, setPosts] = useState([]); const [isAdmin, setIsAdmin] = useState(false); + const [pageNum, setPageNum] = useState(1); + const [totalPages, setTotalPages] = useState(0); + + const pageDown = (() => { + if (pageNum > 0) { + setPageNum(pageNum - 1); + } + + console.log(totalPages); + }); + + const pageUp = (() => { + setPageNum(pageNum + 1); + console.log(totalPages); + }); - // fetch posts from server useEffect(() => { - setIsAdmin(GetIsAdmin); - fetch(`api/posts`) + fetch(`api/posts/page/${pageNum}`) .then((res) => { return res.json(); }) .then((data) => setPosts(data)); + }, [pageNum]); + + + useEffect(() => { + setIsAdmin(GetIsAdmin); + fetch("api/posts/page/count") + .then((res) => { return res.json(); }) + .then((data) => { + setTotalPages(data) + }); }, []); + const Pager = () => { + return
+ {pageNum > 1 ? ( + + ) : ( + + )} + + {pageNum} + + {pageNum < totalPages ? ( + + ) : ( + + )} +
+ } + return (
+ {isAdmin && Create Post} {posts.length === 0 ?

Loading...

: posts.map((p) => )} +
); - } -export default Posts; \ No newline at end of file +export default Posts; diff --git a/devblog/devblog/ClientApp/src/pages/styles/Posts.css b/devblog/devblog/ClientApp/src/pages/styles/Posts.css index a5de0777..7f0b60dc 100644 --- a/devblog/devblog/ClientApp/src/pages/styles/Posts.css +++ b/devblog/devblog/ClientApp/src/pages/styles/Posts.css @@ -4,4 +4,34 @@ flex-direction: column; justify-content: center; margin: 150px 20px 0px 20px; +} + +.pager:first-child { + display: flex; + margin-bottom: 20px; +} + +.pager:last-child { + display: flex; + margin-bottom: 100px; +} + +.pager span { + font-family: 'Chivo Mono', monospace; + font-size: 20px; + line-height: 35px; +} + +.arrow-hidden { + visibility: hidden; +} + +.pager svg { + font-size: 35px; + opacity: 0.7; +} + +.pager svg:hover { + cursor: pointer; + opacity: 1.0; } \ No newline at end of file diff --git a/devblog/devblog/Controllers/PostsController.cs b/devblog/devblog/Controllers/PostsController.cs index a687a798..a06de709 100644 --- a/devblog/devblog/Controllers/PostsController.cs +++ b/devblog/devblog/Controllers/PostsController.cs @@ -22,16 +22,35 @@ public PostsController(IPostService posts, IImgService imgService) /// Retrieves all posts /// /// List - [HttpGet] - public async Task> Get() + //[HttpGet] + //public async Task> Get() + //{ + // var posts = await _posts.Get(); + // return posts; + //} + + [HttpGet("page/{pageNum}")] + public async Task> GetPage(int pageNum) { - var posts = await _posts.Get(); + var posts = await _posts.GetPage(pageNum); return posts; } /// - /// Retrieves all posts + /// Gets the total page count + /// + /// int + [HttpGet("page/count")] + public async Task GetPageCount() + { + var count = await _posts.GetPageCount(); + return count; + } + + /// + /// Retrieves Specified Post /// + /// Post Id /// List [HttpGet("{id}")] public async Task Get(int id) diff --git a/devblog/devblog/Interfaces/IPostService.cs b/devblog/devblog/Interfaces/IPostService.cs index 204d0c0d..bd278224 100644 --- a/devblog/devblog/Interfaces/IPostService.cs +++ b/devblog/devblog/Interfaces/IPostService.cs @@ -13,10 +13,17 @@ public interface IPostService Task Create(PostUpload post); /// - /// Retrieves all posts + /// Gets the total page count /// + /// int + Task GetPageCount(); + + /// + /// Retrieves all posts (5 max) for specified page + /// + /// The page number to get posts from /// List - Task> Get(); + Task> GetPage(int pageNum); /// /// Retrieves a specified post diff --git a/devblog/devblog/Services/PostService.cs b/devblog/devblog/Services/PostService.cs index 945a7cba..e3b6af43 100644 --- a/devblog/devblog/Services/PostService.cs +++ b/devblog/devblog/Services/PostService.cs @@ -65,16 +65,30 @@ public async Task Create(PostUpload post) } /// - /// Retrieves all posts + /// Gets the total page count /// + /// int + public async Task GetPageCount() + { + var postCount = await _db.Post.CountAsync(); + var pageCount = (int)Math.Ceiling(postCount / 5.0); + return pageCount; + } + + /// + /// Retrieves all posts (5 max) for specified page + /// + /// The page number to get posts from /// List - public async Task> Get() + public async Task> GetPage(int pageNum) { var posts = await _db.Post.OrderByDescending(x => x.Date) .Include(x => x.Comments) .Include(x => x.Imgs) .Include(x => x.UpVotes) .Include(x => x.DownVotes) + .Skip((pageNum - 1) * 5) + .Take(5) .ToListAsync(); return posts;