diff --git a/backend/routes/api.js b/backend/routes/api.js
index 55c68615..ae3d82f6 100644
--- a/backend/routes/api.js
+++ b/backend/routes/api.js
@@ -707,10 +707,20 @@ router.post("/getItemDetails", async (req, res) => {
//DB Queries - History
router.get("/getHistory", async (req, res) => {
try {
+ // Extract page and limit from query parameters
+ const page = parseInt(req.query.page) || 0; // default to page 1 if not provided
+ const limit = parseInt(req.query.limit) || 10; // default to 10 items per page if not provided
+ const offset = (page) * limit;
+
+ // Fetch rows with pagination
const { rows } = await db.query(
- `SELECT * FROM jf_playback_activity order by "ActivityDateInserted" desc`,
+ `SELECT * FROM jf_playback_activity
+ ORDER BY "ActivityDateInserted" DESC
+ LIMIT $1 OFFSET $2`,
+ [limit, offset]
);
+ // Group results as per the existing logic
const groupedResults = {};
rows.forEach((row) => {
if (groupedResults[row.NowPlayingItemId + row.EpisodeId]) {
@@ -734,12 +744,21 @@ router.get("/getHistory", async (req, res) => {
}
});
- res.send(Object.values(groupedResults));
+ // Return paginated results
+ res.send({
+ page,
+ limit,
+ totalItems: rows.length, // Total number of items in the current page
+ totalGroups: Object.keys(groupedResults).length, // Total number of grouped results in the current page
+ data: Object.values(groupedResults),
+ });
} catch (error) {
console.log(error);
+ res.status(500).send("Server error");
}
});
+
router.post("/getLibraryHistory", async (req, res) => {
try {
const { libraryid } = req.body;
diff --git a/frontend/src/pages/activity.js b/frontend/src/pages/activity.js
index d046c6db..f70473d2 100644
--- a/frontend/src/pages/activity.js
+++ b/frontend/src/pages/activity.js
@@ -1,5 +1,4 @@
import React, { useState, useEffect } from "react";
-
import axios from "axios";
import "./css/activity.css";
@@ -9,10 +8,11 @@ import ActivityTable from "./components/activity/activity-table";
import Loading from "./components/general/loading";
function Activity() {
- const [data, setData] = useState();
+ const [data, setData] = useState([]);
const [config, setConfig] = useState(null);
- const [itemCount, setItemCount] = useState(10);
+ const [itemCount, setItemCount] = useState(10); // Nombre d'éléments par page
+ const [page, setPage] = useState(0); // Page actuelle
useEffect(() => {
const fetchConfig = async () => {
@@ -34,44 +34,32 @@ function Activity() {
Authorization: `Bearer ${config.token}`,
"Content-Type": "application/json",
},
+ params: {
+ page,
+ limit: itemCount,
+ },
})
- .then((data) => {
- setData(data.data);
+ .then((response) => {
+ setData(response.data.data);
})
.catch((error) => {
console.log(error);
});
};
- if (!data && config) {
+ if (config) {
fetchLibraries();
- }
-
- if (!config) {
+ } else {
fetchConfig();
}
- const intervalId = setInterval(fetchLibraries, 60000 * 60);
- return () => clearInterval(intervalId);
- }, [data, config]);
+ return () => {};
+ }, [config, page, itemCount]);
- if (!data) {
+ if (!data.length) {
return