Skip to content

Commit

Permalink
Wire activity log pagination ui to API
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonkopliku committed Aug 9, 2024
1 parent 308f869 commit 318647f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
11 changes: 11 additions & 0 deletions assets/js/common/ActivityLogOverview/ActivityLogOverview.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down
1 change: 1 addition & 0 deletions assets/js/common/Table/Table.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion assets/js/lib/api/activityLogs.js
Original file line number Diff line number Diff line change
@@ -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,
});
51 changes: 49 additions & 2 deletions assets/js/pages/ActivityLogPage/ActivityLogPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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 (
Expand All @@ -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);
}}
/>
</>
);
Expand Down

0 comments on commit 318647f

Please sign in to comment.