From 5c3fe519826c4436fe61c68148fb1c59d39d130d Mon Sep 17 00:00:00 2001 From: Keith Grant Date: Tue, 11 Feb 2020 12:09:13 -0800 Subject: [PATCH] fix credential list page number after deleting --- .../CredentialList/CredentialList.jsx | 37 ++++++++++++------- .../OrganizationList/OrganizationList.jsx | 3 +- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/awx/ui_next/src/screens/Credential/CredentialList/CredentialList.jsx b/awx/ui_next/src/screens/Credential/CredentialList/CredentialList.jsx index 3374fd90bb..67ef629e31 100644 --- a/awx/ui_next/src/screens/Credential/CredentialList/CredentialList.jsx +++ b/awx/ui_next/src/screens/Credential/CredentialList/CredentialList.jsx @@ -1,5 +1,5 @@ import React, { useState, useEffect } from 'react'; -import { useLocation } from 'react-router-dom'; +import { useLocation, useHistory } from 'react-router-dom'; import { withI18n } from '@lingui/react'; import { t } from '@lingui/macro'; import { CredentialsAPI } from '@api'; @@ -11,7 +11,12 @@ import PaginatedDataList, { ToolbarAddButton, ToolbarDeleteButton, } from '@components/PaginatedDataList'; -import { getQSConfig, parseQueryString } from '@util/qs'; +import { + getQSConfig, + parseQueryString, + removeParams, + encodeNonDefaultQueryString, +} from '@util/qs'; import { CredentialListItem } from '.'; const QS_CONFIG = getQSConfig('credential', { @@ -30,6 +35,7 @@ function CredentialList({ i18n }) { const [selected, setSelected] = useState([]); const location = useLocation(); + const history = useHistory(); const loadCredentials = async ({ search }) => { const params = parseQueryString(QS_CONFIG, search); @@ -92,20 +98,23 @@ function CredentialList({ i18n }) { setDeletionError(error); } + adjustPagination(); + setSelected([]); + }; + + const adjustPagination = () => { const params = parseQueryString(QS_CONFIG, location.search); - try { - const { - data: { count, results }, - } = await CredentialsAPI.read(params); - - setCredentials(results); - setCredentialCount(count); - setSelected([]); - } catch (error) { - setContentError(error); + if (params.page > 1 && selected.length === credentials.length) { + const newParams = removeParams(QS_CONFIG, params, { page: params.page }); + history.push( + `${location.pathname}?${encodeNonDefaultQueryString( + QS_CONFIG, + newParams + )}` + ); + } else { + loadCredentials(location); } - - setHasContentLoading(false); }; const canAdd = diff --git a/awx/ui_next/src/screens/Organization/OrganizationList/OrganizationList.jsx b/awx/ui_next/src/screens/Organization/OrganizationList/OrganizationList.jsx index 2b278d2585..e97d0b439e 100644 --- a/awx/ui_next/src/screens/Organization/OrganizationList/OrganizationList.jsx +++ b/awx/ui_next/src/screens/Organization/OrganizationList/OrganizationList.jsx @@ -96,8 +96,9 @@ function OrganizationsList({ i18n }) { newParams )}` ); + } else { + await fetchOrganizations(); } - await fetchOrganizations(); }; const hasContentLoading = isDeleteLoading || isOrgsLoading;