Skip to content

Commit

Permalink
clear list selection on navigate (first batch)
Browse files Browse the repository at this point in the history
  • Loading branch information
keithjgrant committed Jun 2, 2021
1 parent 7725c6f commit 5377733
Show file tree
Hide file tree
Showing 15 changed files with 183 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ function DataListToolbar({
onExpand,
onSelectAll,
additionalControls,

qsConfig,
pagination,
}) {
Expand Down
30 changes: 12 additions & 18 deletions awx/ui_next/src/components/JobList/JobList.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect, useCallback } from 'react';
import React, { useEffect, useCallback } from 'react';
import { useLocation } from 'react-router-dom';
import { t, Plural } from '@lingui/macro';

Expand All @@ -13,7 +13,7 @@ import useRequest, {
useDismissableError,
} from '../../util/useRequest';
import { useConfig } from '../../contexts/Config';

import useSelected from '../../util/useSelected';
import { isJobRunning, getJobModel } from '../../util/jobs';
import { getQSConfig, parseQueryString } from '../../util/qs';
import JobListItem from './JobListItem';
Expand All @@ -35,8 +35,6 @@ function JobList({ defaultParams, showTypeColumn = false }) {
);

const { me } = useConfig();

const [selected, setSelected] = useState([]);
const location = useLocation();
const {
result: { results, count, relatedSearchableKeys, searchableKeys },
Expand Down Expand Up @@ -88,7 +86,14 @@ function JobList({ defaultParams, showTypeColumn = false }) {

const jobs = useWsJobs(results, fetchJobsById, qsConfig);

const isAllSelected = selected.length === jobs.length && selected.length > 0;
const {
selected,
isAllSelected,
handleSelect,
setSelected,
selectAll,
clearSelected,
} = useSelected(jobs);

const {
error: cancelJobsError,
Expand Down Expand Up @@ -143,18 +148,6 @@ function JobList({ defaultParams, showTypeColumn = false }) {
setSelected([]);
};

const handleSelectAll = isSelected => {
setSelected(isSelected ? [...jobs] : []);
};

const handleSelect = item => {
if (selected.some(s => s.id === item.id)) {
setSelected(selected.filter(s => s.id !== item.id));
} else {
setSelected(selected.concat(item));
}
};

const cannotDeleteItems = selected.filter(job => isJobRunning(job.status));

return (
Expand Down Expand Up @@ -226,14 +219,15 @@ function JobList({ defaultParams, showTypeColumn = false }) {
<HeaderCell>{t`Actions`}</HeaderCell>
</HeaderRow>
}
clearSelected={clearSelected}
toolbarSearchableKeys={searchableKeys}
toolbarRelatedSearchableKeys={relatedSearchableKeys}
renderToolbar={props => (
<DatalistToolbar
{...props}
showSelectAll
isAllSelected={isAllSelected}
onSelectAll={handleSelectAll}
onSelectAll={selectAll}
qsConfig={qsConfig}
additionalControls={[
<ToolbarDeleteButton
Expand Down
16 changes: 12 additions & 4 deletions awx/ui_next/src/components/PaginatedTable/PaginatedTable.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'styled-components/macro';
import React, { Fragment } from 'react';
import React, { useEffect } from 'react';
import PropTypes from 'prop-types';
import { TableComposable, Tbody } from '@patternfly/react-table';

import { t } from '@lingui/macro';
import { useHistory } from 'react-router-dom';
import { useHistory, useLocation } from 'react-router-dom';

import ListHeader from '../ListHeader';
import ContentEmpty from '../ContentEmpty';
Expand Down Expand Up @@ -37,9 +37,15 @@ function PaginatedTable({
showPageSizeOptions,
renderToolbar,
emptyContentMessage,
clearSelected,
ouiaId,
}) {
const history = useHistory();
const location = useLocation();

useEffect(() => {
clearSelected();
}, [location.search, clearSelected]);

const pushHistoryState = params => {
const { pathname, search } = history.location;
Expand Down Expand Up @@ -132,7 +138,7 @@ function PaginatedTable({
);

return (
<Fragment>
<>
<ListHeader
itemCount={itemCount}
renderToolbar={renderToolbar}
Expand Down Expand Up @@ -164,7 +170,7 @@ function PaginatedTable({
onPerPageSelect={handleSetPageSize}
/>
) : null}
</Fragment>
</>
);
}

Expand All @@ -187,6 +193,7 @@ PaginatedTable.propTypes = {
renderToolbar: PropTypes.func,
hasContentLoading: PropTypes.bool,
contentError: PropTypes.shape(),
clearSelected: PropTypes.func,
ouiaId: PropTypes.string,
};

Expand All @@ -200,6 +207,7 @@ PaginatedTable.defaultProps = {
showPageSizeOptions: true,
renderToolbar: props => <DataListToolbar {...props} />,
ouiaId: null,
clearSelected: () => {},
};

export { PaginatedTable as _PaginatedTable };
Expand Down
31 changes: 12 additions & 19 deletions awx/ui_next/src/components/Schedule/ScheduleList/ScheduleList.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect, useCallback } from 'react';
import React, { useEffect, useCallback } from 'react';
import { useLocation } from 'react-router-dom';
import { bool, func } from 'prop-types';

Expand All @@ -10,6 +10,7 @@ import PaginatedTable, { HeaderRow, HeaderCell } from '../../PaginatedTable';
import DataListToolbar from '../../DataListToolbar';
import { ToolbarAddButton, ToolbarDeleteButton } from '../../PaginatedDataList';
import useRequest, { useDeleteItems } from '../../../util/useRequest';
import useSelected from '../../../util/useSelected';
import { getQSConfig, parseQueryString } from '../../../util/qs';
import ScheduleListItem from './ScheduleListItem';

Expand All @@ -27,8 +28,6 @@ function ScheduleList({
launchConfig,
surveyConfig,
}) {
const [selected, setSelected] = useState([]);

const location = useLocation();

const {
Expand Down Expand Up @@ -76,8 +75,13 @@ function ScheduleList({
fetchSchedules();
}, [fetchSchedules]);

const isAllSelected =
selected.length === schedules.length && selected.length > 0;
const {
selected,
isAllSelected,
handleSelect,
selectAll,
clearSelected,
} = useSelected(schedules);

const {
isLoading: isDeleteLoading,
Expand All @@ -95,21 +99,9 @@ function ScheduleList({
}
);

const handleSelectAll = isSelected => {
setSelected(isSelected ? [...schedules] : []);
};

const handleSelect = row => {
if (selected.some(s => s.id === row.id)) {
setSelected(selected.filter(s => s.id !== row.id));
} else {
setSelected(selected.concat(row));
}
};

const handleDelete = async () => {
await deleteJobs();
setSelected([]);
clearSelected();
};

const canAdd =
Expand Down Expand Up @@ -183,6 +175,7 @@ function ScheduleList({
isMissingSurvey={isTemplate && hasMissingSurveyValue(item)}
/>
)}
clearSelected={clearSelected}
toolbarSearchColumns={[
{
name: t`Name`,
Expand All @@ -209,7 +202,7 @@ function ScheduleList({
{...props}
showSelectAll
isAllSelected={isAllSelected}
onSelectAll={handleSelectAll}
onSelectAll={selectAll}
qsConfig={QS_CONFIG}
additionalControls={[
...(canAdd
Expand Down
37 changes: 15 additions & 22 deletions awx/ui_next/src/components/TemplateList/TemplateList.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Fragment, useEffect, useState, useCallback } from 'react';
import React, { useEffect, useCallback } from 'react';
import { useLocation, Link } from 'react-router-dom';
import { t, Plural } from '@lingui/macro';
import { Card, DropdownItem } from '@patternfly/react-core';
Expand All @@ -13,6 +13,7 @@ import ErrorDetail from '../ErrorDetail';
import { ToolbarDeleteButton } from '../PaginatedDataList';
import PaginatedTable, { HeaderRow, HeaderCell } from '../PaginatedTable';
import useRequest, { useDeleteItems } from '../../util/useRequest';
import useSelected from '../../util/useSelected';
import { getQSConfig, parseQueryString } from '../../util/qs';
import useWsTemplates from '../../util/useWsTemplates';
import AddDropDownButton from '../AddDropDownButton';
Expand All @@ -35,8 +36,6 @@ function TemplateList({ defaultParams }) {
);

const location = useLocation();
const [selected, setSelected] = useState([]);

const {
result: {
results,
Expand Down Expand Up @@ -87,8 +86,14 @@ function TemplateList({ defaultParams }) {

const templates = useWsTemplates(results);

const isAllSelected =
selected.length === templates.length && selected.length > 0;
const {
selected,
isAllSelected,
handleSelect,
selectAll,
clearSelected,
} = useSelected(templates);

const {
isLoading: isDeleteLoading,
deleteItems: deleteTemplates,
Expand Down Expand Up @@ -117,19 +122,7 @@ function TemplateList({ defaultParams }) {

const handleTemplateDelete = async () => {
await deleteTemplates();
setSelected([]);
};

const handleSelectAll = isSelected => {
setSelected(isSelected ? [...templates] : []);
};

const handleSelect = template => {
if (selected.some(s => s.id === template.id)) {
setSelected(selected.filter(s => s.id !== template.id));
} else {
setSelected(selected.concat(template));
}
clearSelected();
};

const canAddJT =
Expand Down Expand Up @@ -179,7 +172,7 @@ function TemplateList({ defaultParams }) {
);

return (
<Fragment>
<>
<Card>
<PaginatedTable
contentError={contentError}
Expand All @@ -188,7 +181,7 @@ function TemplateList({ defaultParams }) {
itemCount={count}
pluralizedItemName={t`Templates`}
qsConfig={qsConfig}
onRowClick={handleSelect}
clearSelected={clearSelected}
toolbarSearchColumns={[
{
name: t`Name`,
Expand Down Expand Up @@ -235,7 +228,7 @@ function TemplateList({ defaultParams }) {
{...props}
showSelectAll
isAllSelected={isAllSelected}
onSelectAll={handleSelectAll}
onSelectAll={selectAll}
qsConfig={qsConfig}
additionalControls={[
...(canAddJT || canAddWFJT ? [addButton] : []),
Expand Down Expand Up @@ -281,7 +274,7 @@ function TemplateList({ defaultParams }) {
{t`Failed to delete one or more templates.`}
<ErrorDetail error={deletionError} />
</AlertModal>
</Fragment>
</>
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,14 @@ function CredentialList() {
fetchCredentials();
}, [fetchCredentials]);

const { selected, isAllSelected, handleSelect, setSelected } = useSelected(
credentials
);
const {
selected,
isAllSelected,
handleSelect,
setSelected,
selectAll,
clearSelected,
} = useSelected(credentials);

const {
isLoading: isDeleteLoading,
Expand Down Expand Up @@ -115,7 +120,7 @@ function CredentialList() {
items={credentials}
itemCount={credentialCount}
qsConfig={QS_CONFIG}
onRowClick={handleSelect}
clearSelected={clearSelected}
toolbarSearchableKeys={searchableKeys}
toolbarRelatedSearchableKeys={relatedSearchableKeys}
toolbarSearchColumns={[
Expand Down Expand Up @@ -160,9 +165,7 @@ function CredentialList() {
{...props}
showSelectAll
isAllSelected={isAllSelected}
onSelectAll={isSelected =>
setSelected(isSelected ? [...credentials] : [])
}
onSelectAll={selectAll}
qsConfig={QS_CONFIG}
additionalControls={[
...(canAdd
Expand Down
Loading

0 comments on commit 5377733

Please sign in to comment.