Skip to content

Commit 25a0d32

Browse files
authored
Merge pull request #429 from openedx/knguyen2/ent-9546
feat: add manual pagination
2 parents 8222aef + 8996219 commit 25a0d32

File tree

4 files changed

+69
-51
lines changed

4 files changed

+69
-51
lines changed

src/Configuration/Customers/CustomerDataTable/CustomersPage.jsx

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import React, {
22
useMemo,
33
useState,
44
useCallback,
5-
useEffect,
65
} from 'react';
76
import PropTypes from 'prop-types';
87
import debounce from 'lodash.debounce';
@@ -28,33 +27,42 @@ const expandAllRowsHandler = ({ getToggleAllRowsExpandedProps }) => (
2827
);
2928

3029
const CustomersPage = () => {
31-
const [enterpriseList, setEnterpriseList] = useState([]);
30+
const [enterpriseList, setEnterpriseList] = useState({
31+
itemCount: 0,
32+
pageCount: 0,
33+
results: [],
34+
});
3235
const [isLoading, setIsLoading] = useState(true);
33-
34-
const fetchData = useCallback(
35-
async () => {
36-
try {
37-
const { data } = await LmsApiService.fetchEnterpriseCustomerSupportTool();
38-
const result = camelCaseObject(data);
39-
setEnterpriseList(result);
40-
} catch (error) {
41-
logError(error);
42-
} finally {
43-
setIsLoading(false);
44-
}
45-
},
46-
[],
47-
);
36+
const fetchData = useCallback(async (args) => {
37+
try {
38+
setIsLoading(true);
39+
const options = {};
40+
args.filters.forEach((filter) => {
41+
const { id, value } = filter;
42+
if (id === 'name') {
43+
options.user_query = value;
44+
}
45+
});
46+
options.page = args.pageIndex + 1;
47+
const { data } = await LmsApiService.fetchEnterpriseCustomerSupportTool(options);
48+
const result = camelCaseObject(data);
49+
setEnterpriseList({
50+
itemCount: result.count,
51+
pageCount: result.numPages,
52+
results: result.results,
53+
});
54+
} catch (error) {
55+
logError(error);
56+
} finally {
57+
setIsLoading(false);
58+
}
59+
}, []);
4860

4961
const debouncedFetchData = useMemo(() => debounce(
5062
fetchData,
5163
300,
5264
), [fetchData]);
5365

54-
useEffect(() => {
55-
debouncedFetchData();
56-
}, [debouncedFetchData]);
57-
5866
return (
5967
<Container className="mt-5">
6068
<h1>Customers</h1>
@@ -67,18 +75,22 @@ const CustomersPage = () => {
6775
}}
6876
renderRowSubComponent={({ row }) => <CustomerDetailRowSubComponent row={row} />}
6977
isPaginated
78+
manualPagination
79+
manualFilters
7080
isFilterable
81+
fetchData={debouncedFetchData}
7182
defaultColumnValues={{ Filter: TextFilter }}
72-
itemCount={enterpriseList?.length || 0}
73-
data={enterpriseList || []}
83+
itemCount={enterpriseList.itemCount}
84+
pageCount={enterpriseList.pageCount}
85+
data={enterpriseList.results || []}
7486
columns={[
7587
{
7688
id: 'expander',
7789
Header: expandAllRowsHandler,
7890
Cell: DataTable.ExpandRow,
7991
},
8092
{
81-
id: 'customer details',
93+
id: 'name',
8294
Header: 'Customer details',
8395
accessor: 'name',
8496
Cell: CustomerDetailLink,

src/Configuration/Customers/CustomerDataTable/tests/CustomersPage.test.jsx

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,29 @@ import { IntlProvider } from '@edx/frontend-platform/i18n';
66
import CustomersPage from '../CustomersPage';
77
import LmsApiService from '../../../../data/services/EnterpriseApiService';
88

9-
const mockData = [{
10-
name: 'Ubuntu',
11-
slug: 'test-ubuntu',
12-
uuid: 'test-enterprise-uuid',
13-
activeIntegrations: [{
14-
channelCode: 'test-channel',
15-
created: 'jan 1, 1992',
16-
modified: 'jan 2, 1992',
17-
displayName: 'test channel',
18-
active: true,
9+
const mockData = {
10+
count: 1,
11+
numPages: 1,
12+
results: [{
13+
name: 'Ubuntu',
14+
slug: 'test-ubuntu',
15+
uuid: 'test-enterprise-uuid',
16+
activeIntegrations: [{
17+
channelCode: 'test-channel',
18+
created: 'jan 1, 1992',
19+
modified: 'jan 2, 1992',
20+
displayName: 'test channel',
21+
active: true,
22+
}],
23+
activeSsoConfigurations: [{
24+
created: 'jan 1, 1992',
25+
modified: 'jan 2, 1992',
26+
displayName: 'test channel',
27+
active: true,
28+
}],
29+
enableGenerationOfApiCredentials: true,
1930
}],
20-
activeSsoConfigurations: [{
21-
created: 'jan 1, 1992',
22-
modified: 'jan 2, 1992',
23-
displayName: 'test channel',
24-
active: true,
25-
}],
26-
enableGenerationOfApiCredentials: true,
27-
}];
31+
};
2832

2933
jest.mock('lodash.debounce', () => jest.fn((fn) => fn));
3034
jest

src/Configuration/Customers/CustomerDetailView/CustomerViewContainer.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ const CustomerViewContainer = () => {
2525
const fetchData = useCallback(
2626
async () => {
2727
try {
28-
const response = await getEnterpriseCustomer({ uuid: id });
29-
setEnterpriseCustomer(response[0]);
28+
const { results } = await getEnterpriseCustomer({ uuid: id });
29+
setEnterpriseCustomer(results[0]);
3030
} catch (error) {
3131
logError(error);
3232
} finally {

src/Configuration/Customers/CustomerDetailView/tests/CustomerViewContainer.test.jsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ jest.mock('../../data/utils', () => ({
2323

2424
describe('CustomerViewContainer', () => {
2525
it('renders data', async () => {
26-
getEnterpriseCustomer.mockReturnValue([{
27-
uuid: 'test-id',
28-
name: 'Test Customer Name',
29-
slug: 'customer-6',
30-
created: '2024-07-23T20:02:57.651943Z',
31-
modified: '2024-07-23T20:02:57.651943Z',
32-
}]);
26+
getEnterpriseCustomer.mockReturnValue({
27+
results: [{
28+
uuid: 'test-id',
29+
name: 'Test Customer Name',
30+
slug: 'customer-6',
31+
created: '2024-07-23T20:02:57.651943Z',
32+
modified: '2024-07-23T20:02:57.651943Z',
33+
}],
34+
});
3335
formatDate.mockReturnValue('July 23, 2024');
3436
render(
3537
<IntlProvider locale="en">

0 commit comments

Comments
 (0)