-
Notifications
You must be signed in to change notification settings - Fork 993
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #35287 - Create column selector on host index page
- Loading branch information
Showing
12 changed files
with
432 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
192 changes: 192 additions & 0 deletions
192
webpack/assets/javascripts/react_app/components/ColumnSelector/ColumnSelector.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
import React, { useState } from 'react'; | ||
import { PropTypes } from 'prop-types'; | ||
import { Button, Modal, ModalVariant, TreeView } from '@patternfly/react-core'; | ||
import { ColumnsIcon } from '@patternfly/react-icons'; | ||
import { cloneDeep } from 'lodash'; | ||
import { translate as __ } from '../../common/I18n'; | ||
import API from '../../API'; | ||
import { changeQuery } from '../../common/urlHelpers'; | ||
import './column-selector.scss'; | ||
|
||
const ColumnSelector = props => { | ||
const { | ||
data: { url, controller, categories, hasPreference }, | ||
} = props; | ||
|
||
const initialColumns = cloneDeep(categories); | ||
const [isModalOpen, setModalOpen] = useState(false); | ||
const [selectedColumns, setSelectedColumns] = useState(categories); | ||
|
||
const getColumnKeys = () => { | ||
const keys = selectedColumns | ||
.map(category => category.children) | ||
.flat() | ||
.map(column => { | ||
if (column.checkProps.checked) { | ||
return column.key; | ||
} | ||
return null; | ||
}) | ||
.filter(item => item); | ||
return keys; | ||
}; | ||
|
||
async function updateTablePreference() { | ||
if (!hasPreference) { | ||
await API.post(url, { name: 'hosts', columns: getColumnKeys() }); | ||
} else { | ||
await API.put(`${url}/${controller}`, { columns: getColumnKeys() }); | ||
} | ||
changeQuery({}); | ||
} | ||
|
||
const filterItems = (item, checkedItem) => { | ||
if (item.key === checkedItem.key) { | ||
return true; | ||
} | ||
|
||
if (item.children) { | ||
item.children = item.children | ||
.map(opt => Object.assign({}, opt)) | ||
.filter(column => filterItems(column, checkedItem)); | ||
return item.children; | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
const flattenTree = tree => { | ||
let result = []; | ||
tree.forEach(item => { | ||
result.push(item); | ||
if (item.children) { | ||
result = result.concat(flattenTree(item.children)); | ||
} | ||
}); | ||
return result; | ||
}; | ||
|
||
const toggleModal = () => { | ||
setSelectedColumns(initialColumns); | ||
setModalOpen(!isModalOpen); | ||
}; | ||
|
||
const isDisabled = () => { | ||
const hasPartialCheck = selectedColumns.map( | ||
item => item.checkProps.checked | ||
); | ||
return hasPartialCheck.every(el => el === false); | ||
}; | ||
|
||
const updateCheckBox = (treeViewItem, checked = true) => { | ||
treeViewItem.checkProps.checked = checked; | ||
if (treeViewItem.children) { | ||
treeViewItem.children.forEach(item => { | ||
item.checkProps.checked = checked; | ||
}); | ||
} | ||
}; | ||
|
||
const onCheck = (evt, treeViewItem) => { | ||
const { checked } = evt.target; | ||
const checkedItemTree = selectedColumns | ||
.map(column => Object.assign({}, column)) | ||
.filter(item => filterItems(item, treeViewItem)); | ||
const flatCheckedItems = flattenTree(checkedItemTree); | ||
|
||
if (checked) { | ||
updateCheckBox(treeViewItem); | ||
setSelectedColumns( | ||
selectedColumns | ||
.concat( | ||
flatCheckedItems.filter( | ||
item => !selectedColumns.some(i => i.key === item.key) | ||
) | ||
) | ||
.filter(item => item.children) | ||
); | ||
} else { | ||
updateCheckBox(treeViewItem, false); | ||
setSelectedColumns( | ||
selectedColumns.filter(item => | ||
flatCheckedItems.some(i => i.key === item.key) | ||
) | ||
); | ||
} | ||
selectedColumns.map(category => areDescendantsChecked(category)); | ||
}; | ||
|
||
const isChecked = dataItem => dataItem.checkProps.checked; | ||
const areDescendantsChecked = dataItem => { | ||
if (dataItem.children) { | ||
if (dataItem.children.every(child => isChecked(child))) { | ||
dataItem.checkProps.checked = true; | ||
} else if (dataItem.children.some(child => isChecked(child))) { | ||
dataItem.checkProps.checked = null; | ||
} else { | ||
dataItem.checkProps.checked = false; | ||
} | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="pf-c-select-input"> | ||
<div className="pf-c-input-group" id="column-selector"> | ||
<Button | ||
id="btn-select-columns" | ||
variant="link" | ||
icon={<ColumnsIcon />} | ||
iconPosition="left" | ||
className="columns-selector" | ||
onClick={() => toggleModal()} | ||
> | ||
{__('Manage columns')} | ||
</Button> | ||
<Modal | ||
variant={ModalVariant.small} | ||
title={__('Manage columns')} | ||
isOpen={isModalOpen} | ||
onClose={toggleModal} | ||
tabIndex={0} | ||
description={__('Select columns to display in the table.')} | ||
position="top" | ||
actions={[ | ||
<Button | ||
key="save" | ||
variant="primary" | ||
isDisabled={isDisabled()} | ||
onClick={() => updateTablePreference()} | ||
> | ||
{__('Save')} | ||
</Button>, | ||
<Button key="cancel" variant="secondary" onClick={toggleModal}> | ||
{__('Cancel')} | ||
</Button>, | ||
]} | ||
> | ||
<TreeView data={selectedColumns} onCheck={onCheck} hasChecks /> | ||
</Modal> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
ColumnSelector.propTypes = { | ||
data: PropTypes.shape({ | ||
url: PropTypes.string, | ||
controller: PropTypes.string, | ||
categories: PropTypes.arrayOf(PropTypes.object), | ||
hasPreference: PropTypes.bool, | ||
}), | ||
}; | ||
|
||
ColumnSelector.defaultProps = { | ||
data: { | ||
url: '', | ||
controller: '', | ||
categories: [], | ||
hasPreference: false, | ||
}, | ||
}; | ||
|
||
export default ColumnSelector; |
59 changes: 59 additions & 0 deletions
59
webpack/assets/javascripts/react_app/components/ColumnSelector/ColumnsSelector.fixtures.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
export const user = { | ||
impersonated_by: true, | ||
id: 4, | ||
login: 'admin', | ||
firstname: 'Admin', | ||
lastname: 'User', | ||
name: 'Admin User', | ||
}; | ||
|
||
export const ColumnSelectorProps = { | ||
data: { | ||
url: `api/users/${user.id}/table_preferences`, | ||
controller: 'hosts', | ||
categories: [{ | ||
name: 'General', | ||
key: 'general', | ||
defaultExpanded: true, | ||
checkProps: { checked: true }, | ||
children: [ | ||
{ | ||
name: 'Name', | ||
key: 'name', | ||
checkProps: { checked: true }, | ||
}, | ||
{ | ||
name: 'Operating system', | ||
key: 'os_title', | ||
checkProps: { checked: true }, | ||
}, | ||
{ | ||
name: 'Model', | ||
key: 'model', | ||
checkProps: { checked: true }, | ||
}, | ||
{ | ||
name: 'Owner', | ||
key: 'owner', | ||
checkProps: { checked: true }, | ||
}, | ||
{ | ||
name: 'Host group', | ||
key: 'hostgroup', | ||
checkProps: { checked: true }, | ||
}, | ||
{ | ||
name: 'Last report', | ||
key: 'last_report', | ||
checkProps: { checked: true }, | ||
}, | ||
{ | ||
name: 'Comment', | ||
key: 'comment', | ||
checkProps: { checked: true }, | ||
}, | ||
], | ||
}], | ||
hasPreference: true, | ||
}, | ||
}; |
14 changes: 14 additions & 0 deletions
14
...k/assets/javascripts/react_app/components/ColumnSelector/__tests__/ColumnSelector.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import React from 'react'; | ||
import ColumnSelector from '../ColumnSelector'; | ||
import { ColumnSelectorProps } from '../ColumnsSelector.fixtures'; | ||
import { testComponentSnapshotsWithFixtures } from '../../../common/testHelpers'; | ||
|
||
const fixtures = { | ||
'should render': ColumnSelectorProps, | ||
}; | ||
describe('ColumnSelector', () => { | ||
const columnSelector = () => ( | ||
<ColumnSelector {...ColumnSelectorProps} /> | ||
); | ||
testComponentSnapshotsWithFixtures(columnSelector, fixtures); | ||
}); |
Oops, something went wrong.