Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add manual pagination #429

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 36 additions & 24 deletions src/Configuration/Customers/CustomerDataTable/CustomersPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
useMemo,
useState,
useCallback,
useEffect,
} from 'react';
import PropTypes from 'prop-types';
import debounce from 'lodash.debounce';
Expand All @@ -28,33 +27,42 @@
);

const CustomersPage = () => {
const [enterpriseList, setEnterpriseList] = useState([]);
const [enterpriseList, setEnterpriseList] = useState({
itemCount: 0,
pageCount: 0,
results: [],
});
const [isLoading, setIsLoading] = useState(true);

const fetchData = useCallback(
async () => {
try {
const { data } = await LmsApiService.fetchEnterpriseCustomerSupportTool();
const result = camelCaseObject(data);
setEnterpriseList(result);
} catch (error) {
logError(error);
} finally {
setIsLoading(false);
}
},
[],
);
const fetchData = useCallback(async (args) => {
try {
setIsLoading(true);
const options = {};
args.filters.forEach((filter) => {
const { id, value } = filter;

Check warning on line 41 in src/Configuration/Customers/CustomerDataTable/CustomersPage.jsx

View check run for this annotation

Codecov / codecov/patch

src/Configuration/Customers/CustomerDataTable/CustomersPage.jsx#L41

Added line #L41 was not covered by tests
if (id === 'name') {
options.user_query = value;

Check warning on line 43 in src/Configuration/Customers/CustomerDataTable/CustomersPage.jsx

View check run for this annotation

Codecov / codecov/patch

src/Configuration/Customers/CustomerDataTable/CustomersPage.jsx#L43

Added line #L43 was not covered by tests
}
});
options.page = args.pageIndex + 1;
const { data } = await LmsApiService.fetchEnterpriseCustomerSupportTool(options);
const result = camelCaseObject(data);
setEnterpriseList({
itemCount: result.count,
pageCount: result.numPages,
results: result.results,
});
} catch (error) {
logError(error);

Check warning on line 55 in src/Configuration/Customers/CustomerDataTable/CustomersPage.jsx

View check run for this annotation

Codecov / codecov/patch

src/Configuration/Customers/CustomerDataTable/CustomersPage.jsx#L55

Added line #L55 was not covered by tests
} finally {
setIsLoading(false);
}
}, []);

const debouncedFetchData = useMemo(() => debounce(
fetchData,
300,
), [fetchData]);

useEffect(() => {
debouncedFetchData();
}, [debouncedFetchData]);

return (
<Container className="mt-5">
<h1>Customers</h1>
Expand All @@ -67,18 +75,22 @@
}}
renderRowSubComponent={({ row }) => <CustomerDetailRowSubComponent row={row} />}
isPaginated
manualPagination
manualFilters
isFilterable
fetchData={debouncedFetchData}
defaultColumnValues={{ Filter: TextFilter }}
itemCount={enterpriseList?.length || 0}
data={enterpriseList || []}
itemCount={enterpriseList.itemCount}
pageCount={enterpriseList.pageCount}
data={enterpriseList.results || []}
columns={[
{
id: 'expander',
Header: expandAllRowsHandler,
Cell: DataTable.ExpandRow,
},
{
id: 'customer details',
id: 'name',
Header: 'Customer details',
accessor: 'name',
Cell: CustomerDetailLink,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,29 @@ import { IntlProvider } from '@edx/frontend-platform/i18n';
import CustomersPage from '../CustomersPage';
import LmsApiService from '../../../../data/services/EnterpriseApiService';

const mockData = [{
name: 'Ubuntu',
slug: 'test-ubuntu',
uuid: 'test-enterprise-uuid',
activeIntegrations: [{
channelCode: 'test-channel',
created: 'jan 1, 1992',
modified: 'jan 2, 1992',
displayName: 'test channel',
active: true,
const mockData = {
count: 1,
numPages: 1,
results: [{
name: 'Ubuntu',
slug: 'test-ubuntu',
uuid: 'test-enterprise-uuid',
activeIntegrations: [{
channelCode: 'test-channel',
created: 'jan 1, 1992',
modified: 'jan 2, 1992',
displayName: 'test channel',
active: true,
}],
activeSsoConfigurations: [{
created: 'jan 1, 1992',
modified: 'jan 2, 1992',
displayName: 'test channel',
active: true,
}],
enableGenerationOfApiCredentials: true,
}],
activeSsoConfigurations: [{
created: 'jan 1, 1992',
modified: 'jan 2, 1992',
displayName: 'test channel',
active: true,
}],
enableGenerationOfApiCredentials: true,
}];
};

jest.mock('lodash.debounce', () => jest.fn((fn) => fn));
jest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const CustomerViewContainer = () => {
const fetchData = useCallback(
async () => {
try {
const response = await getEnterpriseCustomer({ uuid: id });
setEnterpriseCustomer(response[0]);
const { results } = await getEnterpriseCustomer({ uuid: id });
setEnterpriseCustomer(results[0]);
} catch (error) {
logError(error);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ jest.mock('../../data/utils', () => ({

describe('CustomerViewContainer', () => {
it('renders data', async () => {
getEnterpriseCustomer.mockReturnValue([{
uuid: 'test-id',
name: 'Test Customer Name',
slug: 'customer-6',
created: '2024-07-23T20:02:57.651943Z',
modified: '2024-07-23T20:02:57.651943Z',
}]);
getEnterpriseCustomer.mockReturnValue({
results: [{
uuid: 'test-id',
name: 'Test Customer Name',
slug: 'customer-6',
created: '2024-07-23T20:02:57.651943Z',
modified: '2024-07-23T20:02:57.651943Z',
}],
});
formatDate.mockReturnValue('July 23, 2024');
render(
<IntlProvider locale="en">
Expand Down