-
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 #36822 - Design new hosts page
Created a HostIndex component Update the TableIndexPage as a part of this action to accept things like select all Co-authored-by: Jeremy Lenz <[email protected]>
- Loading branch information
1 parent
b8e2e05
commit c8aea3e
Showing
23 changed files
with
897 additions
and
27 deletions.
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
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
40 changes: 40 additions & 0 deletions
40
webpack/assets/javascripts/react_app/components/HostsIndex/ActionKebab.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,40 @@ | ||
import React, { useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Dropdown, KebabToggle } from '@patternfly/react-core'; | ||
|
||
/** | ||
* Generate a button or a dropdown of buttons | ||
* @param {String} title The title of the button for the title and text inside the button | ||
* @param {Object} action action to preform when the button is click can be href with data-method or Onclick | ||
* @return {Function} button component or splitbutton component | ||
*/ | ||
export const ActionKebab = ({ items }) => { | ||
const [isOpen, setIsOpen] = useState(false); | ||
if (!items.length) return null; | ||
return ( | ||
<> | ||
{items.length > 0 && ( | ||
<Dropdown | ||
ouiaId="action-buttons-dropdown" | ||
toggle={ | ||
<KebabToggle | ||
aria-label="toggle action dropdown" | ||
onToggle={setIsOpen} | ||
/> | ||
} | ||
isOpen={isOpen} | ||
isPlain | ||
dropdownItems={items} | ||
/> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
ActionKebab.propTypes = { | ||
items: PropTypes.arrayOf(PropTypes.node), | ||
}; | ||
|
||
ActionKebab.defaultProps = { | ||
items: [], | ||
}; |
4 changes: 4 additions & 0 deletions
4
webpack/assets/javascripts/react_app/components/HostsIndex/Selectors.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,4 @@ | ||
import { selectComponentByWeight } from '../common/Slot/SlotSelectors'; | ||
|
||
export const selectKebabItems = () => | ||
selectComponentByWeight('hosts-index-kebab'); |
124 changes: 124 additions & 0 deletions
124
webpack/assets/javascripts/react_app/components/HostsIndex/index.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,124 @@ | ||
import React, { createContext } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { useSelector, shallowEqual } from 'react-redux'; | ||
import { Td } from '@patternfly/react-table'; | ||
import { ToolbarItem } from '@patternfly/react-core'; | ||
import { translate as __ } from '../../common/I18n'; | ||
import TableIndexPage from '../PF4/TableIndexPage/TableIndexPage'; | ||
import { ActionKebab } from './ActionKebab'; | ||
import { HOSTS_API_PATH, API_REQUEST_KEY } from '../../routes/Hosts/constants'; | ||
import { selectKebabItems } from './Selectors'; | ||
import { useAPI } from '../../common/hooks/API/APIHooks'; | ||
import { useBulkSelect } from '../PF4/TableIndexPage/Table/TableHooks'; | ||
import SelectAllCheckbox from '../PF4/TableIndexPage/Table/SelectAllCheckbox'; | ||
import { getPageStats } from '../PF4/TableIndexPage/Table/helpers'; | ||
|
||
export const ForemanHostsIndexActionsBarContext = createContext({}); | ||
|
||
const HostsIndex = () => { | ||
const columns = { | ||
name: { | ||
title: __('Name'), | ||
wrapper: ({ id, name }) => <a href={`hosts/${id}`}>{name}</a>, | ||
isSorted: true, | ||
}, | ||
}; | ||
const defaultParams = { search: '' }; // search || | ||
|
||
const response = useAPI('get', `${HOSTS_API_PATH}?include_permissions=true`, { | ||
key: API_REQUEST_KEY, | ||
params: defaultParams, | ||
}); | ||
|
||
const { | ||
response: { | ||
search: apiSearchQuery, | ||
results, | ||
total, | ||
per_page: perPage, | ||
page, | ||
}, | ||
} = response; | ||
|
||
const { pageRowCount } = getPageStats({ total, page, perPage }); | ||
|
||
const { fetchBulkParams, ...selectAllOptions } = useBulkSelect({ | ||
results, | ||
metadata: { total, page }, | ||
initialSearchQuery: apiSearchQuery || '', | ||
}); | ||
|
||
const { | ||
selectAll, | ||
selectPage, | ||
selectNone, | ||
selectedCount, | ||
selectOne, | ||
areAllRowsOnPageSelected, | ||
areAllRowsSelected, | ||
isSelected, | ||
} = selectAllOptions; | ||
|
||
const selectionToolbar = ( | ||
<ToolbarItem key="selectAll"> | ||
<SelectAllCheckbox | ||
{...{ | ||
selectAll, | ||
selectPage, | ||
selectNone, | ||
selectedCount, | ||
pageRowCount, | ||
}} | ||
totalCount={total} | ||
areAllRowsOnPageSelected={areAllRowsOnPageSelected()} | ||
areAllRowsSelected={areAllRowsSelected()} | ||
/> | ||
</ToolbarItem> | ||
); | ||
|
||
const RowSelectTd = ({ rowData }) => ( | ||
<Td | ||
select={{ | ||
rowIndex: rowData.id, | ||
onSelect: (_event, isSelecting) => { | ||
selectOne(isSelecting, rowData.id); | ||
}, | ||
isSelected: isSelected(rowData.id), | ||
disable: false, | ||
}} | ||
/> | ||
); | ||
|
||
RowSelectTd.propTypes = { | ||
rowData: PropTypes.object.isRequired, | ||
}; | ||
|
||
const actionNode = []; | ||
const registeredItems = useSelector(selectKebabItems, shallowEqual); | ||
const customToolbarItems = ( | ||
<ForemanHostsIndexActionsBarContext.Provider | ||
value={{ ...selectAllOptions, fetchBulkParams }} | ||
> | ||
<ActionKebab items={actionNode.concat(registeredItems)} /> | ||
</ForemanHostsIndexActionsBarContext.Provider> | ||
); | ||
|
||
return ( | ||
<TableIndexPage | ||
apiUrl={HOSTS_API_PATH} | ||
apiOptions={{ key: API_REQUEST_KEY }} | ||
header={__('Hosts')} | ||
controller="hosts" | ||
isDeleteable | ||
columns={columns} | ||
creatable={false} | ||
replacementResponse={response} | ||
customToolbarItems={customToolbarItems} | ||
selectionToolbar={selectionToolbar} | ||
showCheckboxes | ||
rowSelectTd={RowSelectTd} | ||
/> | ||
); | ||
}; | ||
|
||
export default HostsIndex; |
3 changes: 3 additions & 0 deletions
3
...ts/react_app/components/PF4/TableIndexPage/Table/SelectAllCheckbox/SelectAllCheckbox.scss
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,3 @@ | ||
.table-select-all-checkbox { | ||
font-weight: normal; | ||
} |
Oops, something went wrong.