mirror of
https://github.com/ansible/awx.git
synced 2026-05-10 10:57:35 -02:30
Adopt useSelected
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import React, { useState, useEffect, useCallback } from 'react';
|
import React, { useEffect, useCallback } from 'react';
|
||||||
import { useLocation, useRouteMatch } from 'react-router-dom';
|
import { useLocation, useRouteMatch } from 'react-router-dom';
|
||||||
import { withI18n } from '@lingui/react';
|
import { withI18n } from '@lingui/react';
|
||||||
import { t } from '@lingui/macro';
|
import { t } from '@lingui/macro';
|
||||||
@@ -12,6 +12,7 @@ import PaginatedDataList, {
|
|||||||
ToolbarDeleteButton,
|
ToolbarDeleteButton,
|
||||||
} from '../../../components/PaginatedDataList';
|
} from '../../../components/PaginatedDataList';
|
||||||
import useRequest, { useDeleteItems } from '../../../util/useRequest';
|
import useRequest, { useDeleteItems } from '../../../util/useRequest';
|
||||||
|
import useSelected from '../../../util/useSelected';
|
||||||
import { getQSConfig, parseQueryString } from '../../../util/qs';
|
import { getQSConfig, parseQueryString } from '../../../util/qs';
|
||||||
import UserListItem from './UserListItem';
|
import UserListItem from './UserListItem';
|
||||||
|
|
||||||
@@ -24,7 +25,6 @@ const QS_CONFIG = getQSConfig('user', {
|
|||||||
function UserList({ i18n }) {
|
function UserList({ i18n }) {
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const match = useRouteMatch();
|
const match = useRouteMatch();
|
||||||
const [selected, setSelected] = useState([]);
|
|
||||||
|
|
||||||
const {
|
const {
|
||||||
result: { users, itemCount, actions },
|
result: { users, itemCount, actions },
|
||||||
@@ -55,7 +55,9 @@ function UserList({ i18n }) {
|
|||||||
fetchUsers();
|
fetchUsers();
|
||||||
}, [fetchUsers]);
|
}, [fetchUsers]);
|
||||||
|
|
||||||
const isAllSelected = selected.length === users.length && selected.length > 0;
|
const { selected, isAllSelected, handleSelect, setSelected } = useSelected(
|
||||||
|
users
|
||||||
|
);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
isLoading: isDeleteLoading,
|
isLoading: isDeleteLoading,
|
||||||
@@ -81,18 +83,6 @@ function UserList({ i18n }) {
|
|||||||
const hasContentLoading = isDeleteLoading || isLoading;
|
const hasContentLoading = isDeleteLoading || isLoading;
|
||||||
const canAdd = actions && actions.POST;
|
const canAdd = actions && actions.POST;
|
||||||
|
|
||||||
const handleSelectAll = isSelected => {
|
|
||||||
setSelected(isSelected ? [...users] : []);
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleSelect = row => {
|
|
||||||
if (selected.some(s => s.id === row.id)) {
|
|
||||||
setSelected(selected.filter(s => s.id !== row.id));
|
|
||||||
} else {
|
|
||||||
setSelected(selected.concat(row));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<PageSection>
|
<PageSection>
|
||||||
@@ -139,7 +129,9 @@ function UserList({ i18n }) {
|
|||||||
{...props}
|
{...props}
|
||||||
showSelectAll
|
showSelectAll
|
||||||
isAllSelected={isAllSelected}
|
isAllSelected={isAllSelected}
|
||||||
onSelectAll={handleSelectAll}
|
onSelectAll={isSelected =>
|
||||||
|
setSelected(isSelected ? [...users] : [])
|
||||||
|
}
|
||||||
qsConfig={QS_CONFIG}
|
qsConfig={QS_CONFIG}
|
||||||
additionalControls={[
|
additionalControls={[
|
||||||
...(canAdd
|
...(canAdd
|
||||||
|
|||||||
Reference in New Issue
Block a user