From 318647fa0c04ad304707302ce61e8ee275ad9fa2 Mon Sep 17 00:00:00 2001 From: Nelson Kopliku Date: Fri, 9 Aug 2024 10:07:56 +0200 Subject: [PATCH] Wire activity log pagination ui to API --- .../ActivityLogOverview.jsx | 11 ++++ assets/js/common/Table/Table.jsx | 1 + assets/js/lib/api/activityLogs.js | 5 +- .../pages/ActivityLogPage/ActivityLogPage.jsx | 51 ++++++++++++++++++- 4 files changed, 65 insertions(+), 3 deletions(-) diff --git a/assets/js/common/ActivityLogOverview/ActivityLogOverview.jsx b/assets/js/common/ActivityLogOverview/ActivityLogOverview.jsx index 84cf453358..41a20ff588 100644 --- a/assets/js/common/ActivityLogOverview/ActivityLogOverview.jsx +++ b/assets/js/common/ActivityLogOverview/ActivityLogOverview.jsx @@ -51,11 +51,22 @@ function ActivityLogOverview({ loading = false, onActivityLogEntryClick = noop, onCloseActivityLogEntryDetails = noop, + onChangeItemsPerPage = noop, + canNavigateToPreviousPage = false, + onPreviousPage = noop, + canNavigateToNextPage = false, + onNextPage = noop, }) { const [selectedEntry, setEntry] = useState({}); const activityLogTableConfig = { pagination: true, + cursorPagination: true, + canNavigateToPreviousPage, + onPreviousPage, + canNavigateToNextPage, + onNextPage, + onChangeItemsPerPage, usePadding: false, columns: [ { diff --git a/assets/js/common/Table/Table.jsx b/assets/js/common/Table/Table.jsx index 48d72c0c64..078edda060 100644 --- a/assets/js/common/Table/Table.jsx +++ b/assets/js/common/Table/Table.jsx @@ -61,6 +61,7 @@ const getFilterFunction = (column, value) => : getDefaultFilterFunction(value, column.key); const itemsPerPageOptions = [10, 20, 50, 75, 100]; +export const defaultItemsPerPage = itemsPerPageOptions[0]; function Table({ config, diff --git a/assets/js/lib/api/activityLogs.js b/assets/js/lib/api/activityLogs.js index 8a69fb9135..7f58b9c1d3 100644 --- a/assets/js/lib/api/activityLogs.js +++ b/assets/js/lib/api/activityLogs.js @@ -1,3 +1,6 @@ import { networkClient } from '@lib/network'; -export const getActivityLog = () => networkClient.get(`/activity_log`); +export const getActivityLog = (filters = {}) => + networkClient.get(`/activity_log`, { + params: filters, + }); diff --git a/assets/js/pages/ActivityLogPage/ActivityLogPage.jsx b/assets/js/pages/ActivityLogPage/ActivityLogPage.jsx index 9129c4dd3c..686a644fd0 100644 --- a/assets/js/pages/ActivityLogPage/ActivityLogPage.jsx +++ b/assets/js/pages/ActivityLogPage/ActivityLogPage.jsx @@ -2,6 +2,7 @@ import React, { useState, useEffect } from 'react'; import PageHeader from '@common/PageHeader'; import ActivityLogOverview from '@common/ActivityLogOverview'; +import { defaultItemsPerPage } from '@common/Table/Table'; import { getActivityLog } from '@lib/api/activityLogs'; @@ -10,16 +11,26 @@ function ActivityLogPage() { const [isLoading, setLoading] = useState(true); const [activityLogDetailModalOpen, setActivityLogDetailModalOpen] = useState(false); + const [currentPaginationData, setCurrentPaginationData] = useState({}); + const [currentlyAppliedPagination, setCurrentlyAppliedPagination] = useState({ + first: defaultItemsPerPage, + }); - useEffect(() => { - getActivityLog() + const loadActivityLog = (filters) => { + setLoading(true); + getActivityLog(filters) .then((response) => { setActivityLog(response.data?.data ?? []); + setCurrentPaginationData(response.data?.pagination ?? {}); }) .catch(() => setActivityLog([])) .finally(() => { setLoading(false); }); + }; + + useEffect(() => { + loadActivityLog(currentlyAppliedPagination); }, []); return ( @@ -33,6 +44,42 @@ function ActivityLogPage() { onCloseActivityLogEntryDetails={() => setActivityLogDetailModalOpen(false) } + onChangeItemsPerPage={(itemsPerPage) => { + const newPagination = { + ...currentlyAppliedPagination, + ...(currentlyAppliedPagination?.first + ? { first: itemsPerPage } + : { last: itemsPerPage }), + }; + loadActivityLog(newPagination); + setCurrentlyAppliedPagination(newPagination); + }} + canNavigateToPreviousPage={ + !isLoading && currentPaginationData.has_previous_page + } + onPreviousPage={() => { + const newPagination = { + last: + currentlyAppliedPagination.first || + currentlyAppliedPagination.last, + before: currentPaginationData.start_cursor, + }; + loadActivityLog(newPagination); + setCurrentlyAppliedPagination(newPagination); + }} + canNavigateToNextPage={ + !isLoading && currentPaginationData.has_next_page + } + onNextPage={() => { + const newPagination = { + first: + currentlyAppliedPagination.first || + currentlyAppliedPagination.last, + after: currentPaginationData.end_cursor, + }; + loadActivityLog(newPagination); + setCurrentlyAppliedPagination(newPagination); + }} /> );