mirror of
https://github.com/ansible/awx.git
synced 2026-02-23 22:16:00 -03:30
convert CredentialList to useRequest
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import React, { useState, useEffect } from 'react';
|
import React, { useState, useEffect, useCallback } from 'react';
|
||||||
import { useLocation, useHistory } 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';
|
||||||
@@ -11,6 +11,7 @@ import PaginatedDataList, {
|
|||||||
ToolbarAddButton,
|
ToolbarAddButton,
|
||||||
ToolbarDeleteButton,
|
ToolbarDeleteButton,
|
||||||
} from '@components/PaginatedDataList';
|
} from '@components/PaginatedDataList';
|
||||||
|
import useRequest from '@util/useRequest';
|
||||||
import {
|
import {
|
||||||
getQSConfig,
|
getQSConfig,
|
||||||
parseQueryString,
|
parseQueryString,
|
||||||
@@ -26,53 +27,74 @@ const QS_CONFIG = getQSConfig('credential', {
|
|||||||
});
|
});
|
||||||
|
|
||||||
function CredentialList({ i18n }) {
|
function CredentialList({ i18n }) {
|
||||||
const [actions, setActions] = useState(null);
|
const [showDeletionError, setShowDeletionError] = useState(false);
|
||||||
const [contentError, setContentError] = useState(null);
|
|
||||||
const [credentialCount, setCredentialCount] = useState(0);
|
|
||||||
const [credentials, setCredentials] = useState([]);
|
|
||||||
const [deletionError, setDeletionError] = useState(null);
|
|
||||||
const [hasContentLoading, setHasContentLoading] = useState(true);
|
|
||||||
const [selected, setSelected] = useState([]);
|
const [selected, setSelected] = useState([]);
|
||||||
|
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
|
|
||||||
const loadCredentials = async ({ search }) => {
|
const {
|
||||||
const params = parseQueryString(QS_CONFIG, search);
|
result: { credentials, credentialCount, actions },
|
||||||
setContentError(null);
|
error: contentError,
|
||||||
setHasContentLoading(true);
|
isLoading,
|
||||||
try {
|
request: fetchCredentials,
|
||||||
const [
|
} = useRequest(
|
||||||
{
|
useCallback(async () => {
|
||||||
data: { count, results },
|
const params = parseQueryString(QS_CONFIG, location.search);
|
||||||
},
|
const [creds, credActions] = await Promise.all([
|
||||||
{
|
|
||||||
data: { actions: optionActions },
|
|
||||||
},
|
|
||||||
] = await Promise.all([
|
|
||||||
CredentialsAPI.read(params),
|
CredentialsAPI.read(params),
|
||||||
loadCredentialActions(),
|
CredentialsAPI.readOptions(),
|
||||||
]);
|
]);
|
||||||
|
return {
|
||||||
setCredentials(results);
|
credentials: creds.data.results,
|
||||||
setCredentialCount(count);
|
credentialCount: creds.data.count,
|
||||||
setActions(optionActions);
|
actions: credActions.data.actions,
|
||||||
} catch (error) {
|
};
|
||||||
setContentError(error);
|
}, [location]),
|
||||||
} finally {
|
{
|
||||||
setHasContentLoading(false);
|
credentials: [],
|
||||||
|
credentialCount: 0,
|
||||||
|
actions: {},
|
||||||
}
|
}
|
||||||
};
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
loadCredentials(location);
|
fetchCredentials();
|
||||||
}, [location]); // eslint-disable-line react-hooks/exhaustive-deps
|
}, [fetchCredentials]);
|
||||||
|
|
||||||
const loadCredentialActions = () => {
|
const {
|
||||||
if (actions) {
|
isLoading: isDeleteLoading,
|
||||||
return Promise.resolve({ data: { actions } });
|
error: deletionError,
|
||||||
|
request: deleteCredentials,
|
||||||
|
} = useRequest(
|
||||||
|
useCallback(async () => {
|
||||||
|
return Promise.all(selected.map(({ id }) => CredentialsAPI.destroy(id)));
|
||||||
|
}, [selected])
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (deletionError) {
|
||||||
|
setShowDeletionError(true);
|
||||||
|
}
|
||||||
|
}, [deletionError]);
|
||||||
|
|
||||||
|
const handleDelete = async () => {
|
||||||
|
await deleteCredentials();
|
||||||
|
adjustPagination();
|
||||||
|
setSelected([]);
|
||||||
|
};
|
||||||
|
|
||||||
|
const adjustPagination = () => {
|
||||||
|
const params = parseQueryString(QS_CONFIG, location.search);
|
||||||
|
if (params.page > 1 && selected.length === credentials.length) {
|
||||||
|
const newParams = encodeNonDefaultQueryString(
|
||||||
|
QS_CONFIG,
|
||||||
|
replaceParams(params, { page: params.page - 1 })
|
||||||
|
);
|
||||||
|
history.push(`${location.pathname}?${newParams}`);
|
||||||
|
} else {
|
||||||
|
fetchCredentials();
|
||||||
}
|
}
|
||||||
return CredentialsAPI.readOptions();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSelectAll = isSelected => {
|
const handleSelectAll = isSelected => {
|
||||||
@@ -87,34 +109,6 @@ function CredentialList({ i18n }) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDelete = async () => {
|
|
||||||
setHasContentLoading(true);
|
|
||||||
|
|
||||||
try {
|
|
||||||
await Promise.all(
|
|
||||||
selected.map(credential => CredentialsAPI.destroy(credential.id))
|
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
setDeletionError(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
adjustPagination();
|
|
||||||
setSelected([]);
|
|
||||||
};
|
|
||||||
|
|
||||||
const adjustPagination = () => {
|
|
||||||
const params = parseQueryString(QS_CONFIG, location.search);
|
|
||||||
if (params.page > 1 && selected.length === credentials.length) {
|
|
||||||
const newParams = encodeNonDefaultQueryString(
|
|
||||||
QS_CONFIG,
|
|
||||||
replaceParams(params, { page: params.page - 1 })
|
|
||||||
);
|
|
||||||
history.push(`${location.pathname}?${newParams}`);
|
|
||||||
} else {
|
|
||||||
loadCredentials(location);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const canAdd =
|
const canAdd =
|
||||||
actions && Object.prototype.hasOwnProperty.call(actions, 'POST');
|
actions && Object.prototype.hasOwnProperty.call(actions, 'POST');
|
||||||
const isAllSelected =
|
const isAllSelected =
|
||||||
@@ -125,7 +119,7 @@ function CredentialList({ i18n }) {
|
|||||||
<Card>
|
<Card>
|
||||||
<PaginatedDataList
|
<PaginatedDataList
|
||||||
contentError={contentError}
|
contentError={contentError}
|
||||||
hasContentLoading={hasContentLoading}
|
hasContentLoading={isLoading || isDeleteLoading}
|
||||||
items={credentials}
|
items={credentials}
|
||||||
itemCount={credentialCount}
|
itemCount={credentialCount}
|
||||||
qsConfig={QS_CONFIG}
|
qsConfig={QS_CONFIG}
|
||||||
@@ -162,10 +156,10 @@ function CredentialList({ i18n }) {
|
|||||||
/>
|
/>
|
||||||
</Card>
|
</Card>
|
||||||
<AlertModal
|
<AlertModal
|
||||||
isOpen={deletionError}
|
isOpen={showDeletionError}
|
||||||
variant="danger"
|
variant="danger"
|
||||||
title={i18n._(t`Error!`)}
|
title={i18n._(t`Error!`)}
|
||||||
onClose={() => setDeletionError(null)}
|
onClose={() => setShowDeletionError(false)}
|
||||||
>
|
>
|
||||||
{i18n._(t`Failed to delete one or more credentials.`)}
|
{i18n._(t`Failed to delete one or more credentials.`)}
|
||||||
<ErrorDetail error={deletionError} />
|
<ErrorDetail error={deletionError} />
|
||||||
|
|||||||
Reference in New Issue
Block a user