mirror of
https://github.com/open-metadata/OpenMetadata.git
synced 2025-09-30 11:26:23 +00:00
Fix pagination for Persona listing (#22975)
* Fix pagination for Persona * fix failing tests
This commit is contained in:
parent
1fc766d7b7
commit
3bd3158bee
@ -11,6 +11,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
import { act, fireEvent, render, screen } from '@testing-library/react';
|
import { act, fireEvent, render, screen } from '@testing-library/react';
|
||||||
|
import { CursorType } from '../../../enums/pagination.enum';
|
||||||
import { getAllPersonas } from '../../../rest/PersonaAPI';
|
import { getAllPersonas } from '../../../rest/PersonaAPI';
|
||||||
import { PersonaPage } from './PersonaPage';
|
import { PersonaPage } from './PersonaPage';
|
||||||
|
|
||||||
@ -82,6 +83,13 @@ jest.mock('../../../hooks/paging/usePaging', () => ({
|
|||||||
currentPage: 1,
|
currentPage: 1,
|
||||||
showPagination: true,
|
showPagination: true,
|
||||||
pageSize: 10,
|
pageSize: 10,
|
||||||
|
paging: {
|
||||||
|
[CursorType.AFTER]: '1',
|
||||||
|
},
|
||||||
|
pagingCursor: {
|
||||||
|
cursorType: CursorType.AFTER,
|
||||||
|
cursorValue: '',
|
||||||
|
},
|
||||||
handlePageChange: jest.fn(),
|
handlePageChange: jest.fn(),
|
||||||
handlePagingChange: jest.fn(),
|
handlePagingChange: jest.fn(),
|
||||||
handlePageSizeChange: jest.fn(),
|
handlePageSizeChange: jest.fn(),
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
*/
|
*/
|
||||||
import { Button, Skeleton } from 'antd';
|
import { Button, Skeleton } from 'antd';
|
||||||
import Card from 'antd/lib/card/Card';
|
import Card from 'antd/lib/card/Card';
|
||||||
|
import { AxiosError } from 'axios';
|
||||||
import { isEmpty } from 'lodash';
|
import { isEmpty } from 'lodash';
|
||||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
@ -28,12 +29,14 @@ import { GlobalSettingsMenuCategory } from '../../../constants/GlobalSettings.co
|
|||||||
import { PAGE_HEADERS } from '../../../constants/PageHeaders.constant';
|
import { PAGE_HEADERS } from '../../../constants/PageHeaders.constant';
|
||||||
import { ERROR_PLACEHOLDER_TYPE } from '../../../enums/common.enum';
|
import { ERROR_PLACEHOLDER_TYPE } from '../../../enums/common.enum';
|
||||||
import { TabSpecificField } from '../../../enums/entity.enum';
|
import { TabSpecificField } from '../../../enums/entity.enum';
|
||||||
|
import { CursorType } from '../../../enums/pagination.enum';
|
||||||
import { Persona } from '../../../generated/entity/teams/persona';
|
import { Persona } from '../../../generated/entity/teams/persona';
|
||||||
import { Paging } from '../../../generated/type/paging';
|
import { Paging } from '../../../generated/type/paging';
|
||||||
import { useAuth } from '../../../hooks/authHooks';
|
import { useAuth } from '../../../hooks/authHooks';
|
||||||
import { usePaging } from '../../../hooks/paging/usePaging';
|
import { usePaging } from '../../../hooks/paging/usePaging';
|
||||||
import { getAllPersonas } from '../../../rest/PersonaAPI';
|
import { getAllPersonas } from '../../../rest/PersonaAPI';
|
||||||
import { getSettingPageEntityBreadCrumb } from '../../../utils/GlobalSettingsUtils';
|
import { getSettingPageEntityBreadCrumb } from '../../../utils/GlobalSettingsUtils';
|
||||||
|
import { showErrorToast } from '../../../utils/ToastUtils';
|
||||||
import './persona-page.less';
|
import './persona-page.less';
|
||||||
|
|
||||||
const PERSONA_PAGE_SIZE = 12;
|
const PERSONA_PAGE_SIZE = 12;
|
||||||
@ -49,9 +52,10 @@ export const PersonaPage = ({ pageTitle }: { pageTitle: string }) => {
|
|||||||
const {
|
const {
|
||||||
currentPage,
|
currentPage,
|
||||||
handlePageChange,
|
handlePageChange,
|
||||||
|
handlePagingChange,
|
||||||
pageSize,
|
pageSize,
|
||||||
paging,
|
paging,
|
||||||
handlePagingChange,
|
pagingCursor,
|
||||||
showPagination,
|
showPagination,
|
||||||
} = usePaging(PERSONA_PAGE_SIZE);
|
} = usePaging(PERSONA_PAGE_SIZE);
|
||||||
|
|
||||||
@ -60,28 +64,44 @@ export const PersonaPage = ({ pageTitle }: { pageTitle: string }) => {
|
|||||||
[]
|
[]
|
||||||
);
|
);
|
||||||
|
|
||||||
const fetchPersonas = useCallback(async (params?: Partial<Paging>) => {
|
const fetchPersonas = useCallback(
|
||||||
try {
|
async (params?: Partial<Paging>) => {
|
||||||
setIsLoading(true);
|
try {
|
||||||
const { data, paging } = await getAllPersonas({
|
setIsLoading(true);
|
||||||
limit: pageSize,
|
const { data, paging } = await getAllPersonas({
|
||||||
fields: TabSpecificField.USERS,
|
limit: pageSize,
|
||||||
after: params?.after,
|
fields: TabSpecificField.USERS,
|
||||||
before: params?.before,
|
after: params?.after,
|
||||||
});
|
before: params?.before,
|
||||||
|
});
|
||||||
|
|
||||||
setPersona(data);
|
setPersona(data);
|
||||||
handlePagingChange(paging);
|
handlePagingChange(paging);
|
||||||
} catch {
|
} catch (error) {
|
||||||
// Error
|
showErrorToast(error as AxiosError);
|
||||||
} finally {
|
} finally {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
}, []);
|
},
|
||||||
|
[pageSize, handlePagingChange]
|
||||||
|
);
|
||||||
|
|
||||||
|
// Initial load and when URL params change
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchPersonas();
|
const cursorValue = pagingCursor.cursorValue;
|
||||||
}, [pageSize]);
|
const cursorType = pagingCursor.cursorType as CursorType;
|
||||||
|
|
||||||
|
if (cursorType && cursorValue) {
|
||||||
|
fetchPersonas({ [cursorType]: cursorValue });
|
||||||
|
} else {
|
||||||
|
fetchPersonas();
|
||||||
|
}
|
||||||
|
}, [
|
||||||
|
pagingCursor.cursorType,
|
||||||
|
pagingCursor.cursorValue,
|
||||||
|
pageSize,
|
||||||
|
fetchPersonas,
|
||||||
|
]);
|
||||||
|
|
||||||
const handleAddNewPersona = useCallback(() => {
|
const handleAddNewPersona = useCallback(() => {
|
||||||
setAddEditPersona({} as Persona);
|
setAddEditPersona({} as Persona);
|
||||||
@ -112,14 +132,18 @@ export const PersonaPage = ({ pageTitle }: { pageTitle: string }) => {
|
|||||||
|
|
||||||
const handlePersonaAddEditSave = useCallback(() => {
|
const handlePersonaAddEditSave = useCallback(() => {
|
||||||
handlePersonalAddEditCancel();
|
handlePersonalAddEditCancel();
|
||||||
fetchPersonas();
|
fetchPersonas({ [CursorType.AFTER]: paging[CursorType.AFTER] });
|
||||||
}, [fetchPersonas]);
|
}, [fetchPersonas, paging]);
|
||||||
|
|
||||||
const handlePersonaPageChange = useCallback(
|
const handlePersonaPageChange = useCallback(
|
||||||
({ currentPage, cursorType }: PagingHandlerParams) => {
|
({ currentPage, cursorType }: PagingHandlerParams) => {
|
||||||
handlePageChange(currentPage);
|
const cursorValue = cursorType ? paging[cursorType] : undefined;
|
||||||
if (cursorType) {
|
handlePageChange(currentPage, {
|
||||||
fetchPersonas({ [cursorType]: paging[cursorType] });
|
cursorType: cursorType as CursorType,
|
||||||
|
cursorValue,
|
||||||
|
});
|
||||||
|
if (cursorType && cursorValue) {
|
||||||
|
fetchPersonas({ [cursorType]: cursorValue });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[handlePageChange, fetchPersonas, paging]
|
[handlePageChange, fetchPersonas, paging]
|
||||||
@ -187,7 +211,7 @@ export const PersonaPage = ({ pageTitle }: { pageTitle: string }) => {
|
|||||||
|
|
||||||
{/* Pagination at bottom of page */}
|
{/* Pagination at bottom of page */}
|
||||||
{showPagination && (
|
{showPagination && (
|
||||||
<div className="d-flex justify-center align-center m-b-sm">
|
<div className="d-flex justify-center align-center m-b-sm m-t-sm">
|
||||||
<NextPrevious
|
<NextPrevious
|
||||||
currentPage={currentPage}
|
currentPage={currentPage}
|
||||||
isLoading={isLoading}
|
isLoading={isLoading}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user