Skip to content

Commit

Permalink
pagination update
Browse files Browse the repository at this point in the history
  • Loading branch information
mnosov622 committed Mar 31, 2023
1 parent 0b0a2f7 commit 3f027c2
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
42 changes: 42 additions & 0 deletions backend/endpoints/pagination.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const express = require("express");
const client = require("../mongodb");
const router = express.Router();

router.get("/", async (req, res) => {
const page = parseInt(req.query.page) || 1;
const limit = parseInt(req.query.limit) || 10;
const db = client.db("courses");
const collection = db.collection("courses");

const startIndex = (page - 1) * limit;
const endIndex = page * limit;

const results = {};

if (endIndex < (await collection.countDocuments())) {
results.next = {
page: page + 1,
limit: limit,
};
}

if (startIndex > 0) {
results.previous = {
page: page - 1,
limit: limit,
};
}

try {
results.results = await collection
.find()
.skip(startIndex)
.limit(limit)
.toArray();
res.json(results);
} catch (e) {
res.status(500).json({ message: e.message });
}
});

module.exports = router;
4 changes: 4 additions & 0 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const resetPassword = require("./endpoints/resetPassword");
const buyCourse = require("./endpoints/buy-course");
const creatorInfo = require("./endpoints/creator");
const review = require("./endpoints/review");
const pagination = require("./endpoints/pagination");

const dotenv = require("dotenv");
const client = require("./mongodb");
Expand Down Expand Up @@ -81,6 +82,9 @@ app.use("/creator", creatorInfo);
//leave a review
app.use("/review", review);

//pagination
app.use("/pagination", pagination);

MongoClient.connect(url, { useUnifiedTopology: true }, (err, client) => {
if (err) throw err;

Expand Down
60 changes: 58 additions & 2 deletions frontend/src/pages/AllCourses/AllCourses.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,43 @@ const AllCourses = () => {
const [loading, setLoading] = useState(true);
const [userCourses, setUserCourses] = useState([]);

//pagination
const [coursesPage, setCoursesPage] = useState([]);
const [page, setPage] = useState(1);
const [limit, setLimit] = useState(9);
const [nextPage, setNextPage] = useState(null);
const [prevPage, setPrevPage] = useState(null);
const [totalPages, setTotalPages] = useState(1);

useEffect(() => {
fetch(`http://localhost:8000/pagination?page=${page}&limit=${limit}`)
.then((response) => response.json())
.then((data) => {
setCoursesPage(data.results);
setNextPage(data.next);
setPrevPage(data.previous);
});

fetch(`http://localhost:8000/pagination?page=1&limit=1000`)
.then((response) => response.json())
.then((data) => {
const totalResults = data.results.length;
setTotalPages(Math.ceil(totalResults / limit));
});
}, [page, limit]);

const handleNextPage = () => {
window.scrollTo(0, 0);
setPage(nextPage.page);
setLimit(nextPage.limit);
};

const handlePrevPage = () => {
window.scrollTo(0, 0);
setPage(prevPage.page);
setLimit(prevPage.limit);
};

useEffect(() => {
//checking if user has a token (logged in)
if (localStorage.getItem("token") !== null) {
Expand Down Expand Up @@ -65,10 +102,12 @@ const AllCourses = () => {
) : (
<>
<div className="bg-light shadow text-center p-2 fs-2 mb-4">
<p>All Courses</p>
<p>
All Courses - Page {page}/{totalPages}
</p>
</div>
<div className="row">
{courses.map((course) => (
{coursesPage.map((course) => (
<div className="col-md-4 mt-2">
<CourseCard
key={course.id}
Expand All @@ -85,6 +124,23 @@ const AllCourses = () => {
</div>
))}
</div>
<div className="text-center">
<button
onClick={handlePrevPage}
disabled={!prevPage}
className="btn btn-secondary"
>
Previous Page
</button>
<button
onClick={handleNextPage}
disabled={!nextPage}
className="btn btn-primary"
style={{ marginLeft: "2%" }}
>
Next Page
</button>
</div>
</>
)}
</div>
Expand Down

2 comments on commit 3f027c2

@vercel
Copy link

@vercel vercel bot commented on 3f027c2 Mar 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lit-lab-project-hrl1 – ./backend

lit-lab-project-hrl1.vercel.app
lit-lab-project-hrl1-git-main-teracle.vercel.app
lit-lab-project-hrl1-teracle.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 3f027c2 Mar 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lit-lab-project – ./frontend

lit-lab-project.vercel.app
lit-lab-project-git-main-teracle.vercel.app
lit-lab-project-teracle.vercel.app

Please sign in to comment.