Skip to content

Commit 832e1ce

Browse files
fix: manually reset page index when using manualPagination
1 parent 9c139c4 commit 832e1ce

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/components/LearnerActivityTable/index.jsx

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useIntl } from '@edx/frontend-platform/i18n';
44
import { DataTable, TextFilter } from '@openedx/paragon';
55
import { connect } from 'react-redux';
66
import { useLocation, useNavigate } from 'react-router-dom';
7-
import { useMemo, useCallback, useEffect } from 'react';
7+
import { useMemo, useCallback, useEffect, useState } from 'react';
88
import useCourseEnrollments from './data/hooks/useCourseEnrollments';
99
import { PAGE_SIZE } from '../../data/constants/table';
1010
import {
@@ -37,6 +37,10 @@ const LearnerActivityTable = ({ id, enterpriseId, activity }) => {
3737
const navigate = useNavigate();
3838
const { setTableHasData } = useTableData();
3939

40+
const [pagination, setPagination] = useState({
41+
pageIndex: 0, //initial page index
42+
pageSize: 5, //default page size
43+
});
4044
// Parse the current page from URL query parameters - adjust for zero-based indexing
4145
const queryParams = useMemo(() => new URLSearchParams(location.search), [location.search]);
4246
const pageFromUrl = parseInt(queryParams.get(`${id}-page`), 10) || 1; // Default to page 1 in URL
@@ -67,7 +71,7 @@ const LearnerActivityTable = ({ id, enterpriseId, activity }) => {
6771
useEffect(() => {
6872
fetchDataImmediate({
6973
pageIndex: currentPageFromUrl,
70-
pageSize: PAGE_SIZE,
74+
pageSize: pagination.pageSize,
7175
sortBy: [],
7276
}, true);
7377
}, []);
@@ -77,6 +81,15 @@ const LearnerActivityTable = ({ id, enterpriseId, activity }) => {
7781
setTableHasData(id, hasData);
7882
}, [id, hasData]);
7983

84+
useEffect(() => {
85+
if (setPagination) {
86+
setPagination((pagination) => ({
87+
pageIndex: 0,
88+
pageSize: pagination.pageSize,
89+
}));
90+
}
91+
}, [setPagination]);
92+
8093
// Wrap fetchCourseEnrollments to update the URL when pagination changes
8194
const fetchTableData = useCallback((tableState) => {
8295
const newPageForUrl = tableState.pageIndex + 1; // Convert zero-based index to one-based for URL
@@ -184,15 +197,21 @@ const LearnerActivityTable = ({ id, enterpriseId, activity }) => {
184197
defaultColumnValues={{ Filter: TextFilter }}
185198
columns={columns}
186199
initialState={{
187-
pageIndex: currentPageFromUrl, // Use page from URL
188-
pageSize: PAGE_SIZE,
200+
// pageIndex: currentPageFromUrl, // Use page from URL
201+
// pageSize: PAGE_SIZE,
189202
sortBy: [{ id: 'lastActivityDate', desc: true }],
190203
selectedRowsOrdered: [],
191204
}}
192205
fetchData={fetchTableData}
193206
data={courseEnrollments.results}
194207
itemCount={courseEnrollments.itemCount}
195208
pageCount={courseEnrollments.pageCount}
209+
initialTableOptions={{
210+
onPaginationChange: setPagination,
211+
state: {
212+
pagination
213+
},
214+
}}
196215
/>
197216
);
198217
};

0 commit comments

Comments
 (0)