fix credential list page number after deleting

This commit is contained in:
Keith Grant
2020-02-11 12:09:13 -08:00
parent f61af39f08
commit 5c3fe51982
2 changed files with 25 additions and 15 deletions

View File

@@ -1,5 +1,5 @@
import React, { useState, useEffect } from 'react'; 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 { withI18n } from '@lingui/react';
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
import { CredentialsAPI } from '@api'; import { CredentialsAPI } from '@api';
@@ -11,7 +11,12 @@ import PaginatedDataList, {
ToolbarAddButton, ToolbarAddButton,
ToolbarDeleteButton, ToolbarDeleteButton,
} from '@components/PaginatedDataList'; } from '@components/PaginatedDataList';
import { getQSConfig, parseQueryString } from '@util/qs'; import {
getQSConfig,
parseQueryString,
removeParams,
encodeNonDefaultQueryString,
} from '@util/qs';
import { CredentialListItem } from '.'; import { CredentialListItem } from '.';
const QS_CONFIG = getQSConfig('credential', { const QS_CONFIG = getQSConfig('credential', {
@@ -30,6 +35,7 @@ function CredentialList({ i18n }) {
const [selected, setSelected] = useState([]); const [selected, setSelected] = useState([]);
const location = useLocation(); const location = useLocation();
const history = useHistory();
const loadCredentials = async ({ search }) => { const loadCredentials = async ({ search }) => {
const params = parseQueryString(QS_CONFIG, search); const params = parseQueryString(QS_CONFIG, search);
@@ -92,20 +98,23 @@ function CredentialList({ i18n }) {
setDeletionError(error); setDeletionError(error);
} }
adjustPagination();
setSelected([]);
};
const adjustPagination = () => {
const params = parseQueryString(QS_CONFIG, location.search); const params = parseQueryString(QS_CONFIG, location.search);
try { if (params.page > 1 && selected.length === credentials.length) {
const { const newParams = removeParams(QS_CONFIG, params, { page: params.page });
data: { count, results }, history.push(
} = await CredentialsAPI.read(params); `${location.pathname}?${encodeNonDefaultQueryString(
QS_CONFIG,
setCredentials(results); newParams
setCredentialCount(count); )}`
setSelected([]); );
} catch (error) { } else {
setContentError(error); loadCredentials(location);
} }
setHasContentLoading(false);
}; };
const canAdd = const canAdd =

View File

@@ -96,8 +96,9 @@ function OrganizationsList({ i18n }) {
newParams newParams
)}` )}`
); );
} else {
await fetchOrganizations();
} }
await fetchOrganizations();
}; };
const hasContentLoading = isDeleteLoading || isOrgsLoading; const hasContentLoading = isDeleteLoading || isOrgsLoading;