-
Notifications
You must be signed in to change notification settings - Fork 1k
Fixes #38416 - Hosts bulk action: Assign organization and Assign location #10591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
8 changes: 8 additions & 0 deletions
8
...react_app/components/HostsIndex/BulkActions/assignTaxonomy/BulkAssignTaxonomyConstants.js
This file contains hidden or 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,8 @@ | ||
| export const BULK_ASSIGN_ORGANIZATION_KEY = 'BULK_ASSIGN_ORGANIZATION'; | ||
| export const BULK_ASSIGN_LOCATION_KEY = 'BULK_ASSIGN_LOCATION'; | ||
| export const ORGANIZATION_KEY = 'ORGANIZATION'; | ||
| export const LOCATION_KEY = 'LOCATION'; | ||
| export const MODAL_TYPES = { | ||
| ORGANIZATION: 'ORGANIZATION', | ||
| LOCATION: 'LOCATION', | ||
| }; |
252 changes: 252 additions & 0 deletions
252
...pts/react_app/components/HostsIndex/BulkActions/assignTaxonomy/BulkAssignTaxonomyModal.js
This file contains hidden or 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,252 @@ | ||
| import React, { useState, useEffect } from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import { useDispatch, useSelector } from 'react-redux'; | ||
| import { FormattedMessage } from 'react-intl'; | ||
| import { | ||
| Modal, | ||
| Button, | ||
| MenuToggle, | ||
| SelectOption, | ||
| TextContent, | ||
| Text, | ||
| TreeView, | ||
| } from '@patternfly/react-core'; | ||
| import { addToast } from '../../../ToastsList/slice'; | ||
| import { translate as __ } from '../../../../common/I18n'; | ||
| import { STATUS } from '../../../../constants'; | ||
| import { | ||
| selectAPIStatus, | ||
| selectAPIResponse, | ||
| } from '../../../../redux/API/APISelectors'; | ||
| import { | ||
| bulkAssignOrganization, | ||
| bulkAssignLocation, | ||
| fetchOrganizations, | ||
| fetchLocations, | ||
| } from './actions'; | ||
| import { | ||
| BULK_ASSIGN_ORGANIZATION_KEY, | ||
| BULK_ASSIGN_LOCATION_KEY, | ||
| ORGANIZATION_KEY, | ||
| LOCATION_KEY, | ||
| MODAL_TYPES, | ||
| } from './BulkAssignTaxonomyConstants'; | ||
| import { foremanUrl } from '../../../../common/helpers'; | ||
| import { APIActions } from '../../../../redux/API'; | ||
| import { | ||
| HOSTS_API_PATH, | ||
| API_REQUEST_KEY, | ||
| } from '../../../../routes/Hosts/constants'; | ||
| import TaxonomySelect from './TaxonomySelect'; | ||
|
|
||
| export const BulkAssignOrganizationModal = props => ( | ||
| <BulkAssignTaxonomyModal modalType={MODAL_TYPES.ORGANIZATION} {...props} /> | ||
| ); | ||
| export const BulkAssignLocationModal = props => ( | ||
| <BulkAssignTaxonomyModal modalType={MODAL_TYPES.LOCATION} {...props} /> | ||
| ); | ||
|
|
||
| const BulkAssignTaxonomyModal = ({ | ||
| isOpen, | ||
| closeModal, | ||
| selectAllHostsMode, | ||
| selectedCount, | ||
| fetchBulkParams, | ||
| modalType, | ||
| }) => { | ||
| const org = modalType === MODAL_TYPES.ORGANIZATION; | ||
| const taxType = org ? 'organization' : 'location'; | ||
| const dispatch = useDispatch(); | ||
| const [taxId, setTaxId] = useState(''); | ||
| const [selectOpen, setSelectOpen] = useState(false); | ||
| const [fixRadioChecked, setFixRadioChecked] = useState(true); | ||
| const taxResults = useSelector(state => | ||
| org | ||
| ? selectAPIResponse(state, ORGANIZATION_KEY) | ||
| : selectAPIResponse(state, LOCATION_KEY) | ||
| ); | ||
| const status = useSelector(state => | ||
| org | ||
| ? selectAPIStatus(state, ORGANIZATION_KEY) | ||
| : selectAPIStatus(state, LOCATION_KEY) | ||
| ); | ||
| const hostUpdateStatus = useSelector(state => | ||
| org | ||
| ? selectAPIStatus(state, BULK_ASSIGN_ORGANIZATION_KEY) | ||
| : selectAPIStatus(state, BULK_ASSIGN_LOCATION_KEY) | ||
| ); | ||
| const handleModalClose = () => { | ||
| setTaxId(''); | ||
| setFixRadioChecked(true); | ||
| closeModal(); | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| org ? dispatch(fetchOrganizations()) : dispatch(fetchLocations()); | ||
| }, [dispatch, org]); | ||
|
|
||
| const onToggleClick = () => { | ||
| setSelectOpen(!selectOpen); | ||
| }; | ||
| const toggle = toggleRef => ( | ||
| <MenuToggle | ||
| ref={toggleRef} | ||
| onClick={onToggleClick} | ||
| isExpanded={selectOpen} | ||
| style={{ width: '95%' }} | ||
| > | ||
| {getSelectedLabel(taxId, taxResults)} | ||
| </MenuToggle> | ||
| ); | ||
|
|
||
| const handleSelect = (event, selection) => { | ||
| setTaxId(selection); | ||
| setSelectOpen(false); | ||
| }; | ||
|
|
||
| const getSelectedLabel = (id, taxonomy) => | ||
| taxonomy.results.find(t => t.id === id)?.name; | ||
|
|
||
| const handleError = error => { | ||
| const { | ||
| response: { | ||
| data: { | ||
| error: { message }, | ||
| }, | ||
| }, | ||
| } = error; | ||
| dispatch(addToast({ type: 'danger', message })); | ||
| handleModalClose(); | ||
| }; | ||
|
|
||
| const handleSuccess = response => { | ||
| dispatch( | ||
| addToast({ | ||
| type: 'success', | ||
| message: response.data.message, | ||
| }) | ||
| ); | ||
| dispatch( | ||
| APIActions.get({ | ||
| key: API_REQUEST_KEY, | ||
| url: foremanUrl(HOSTS_API_PATH), | ||
| }) | ||
| ); | ||
| handleModalClose(); | ||
| }; | ||
|
|
||
| const handleSave = () => { | ||
| const requestBody = { | ||
| included: { | ||
| search: fetchBulkParams(), | ||
| }, | ||
| id: taxId, | ||
| mismatch_setting: fixRadioChecked, | ||
| }; | ||
|
|
||
| org | ||
| ? dispatch( | ||
| bulkAssignOrganization(requestBody, handleSuccess, handleError) | ||
| ) | ||
| : dispatch(bulkAssignLocation(requestBody, handleSuccess, handleError)); | ||
| }; | ||
|
|
||
| const translatedTaxType = org ? __('organization') : __('location'); | ||
| const modalText = ( | ||
| <FormattedMessage | ||
| id={`bulk-assign-${taxType}-text-message`} | ||
| defaultMessage={__( | ||
| 'Select {taxonomy} to add hosts to. This change may affect all your selected hosts.' | ||
| )} | ||
| values={{ taxonomy: translatedTaxType }} | ||
| /> | ||
| ); | ||
|
|
||
| const modalActions = [ | ||
| <Button | ||
| key="add" | ||
| ouiaId={`bulk-assign-${taxType}-modal-add-button`} | ||
| variant="primary" | ||
| onClick={handleSave} | ||
| isDisabled={hostUpdateStatus === STATUS.PENDING || taxId === ''} | ||
| isLoading={hostUpdateStatus === STATUS.PENDING} | ||
| > | ||
| {org ? __('Change organization') : __('Change location')} | ||
| </Button>, | ||
| <Button | ||
| key="cancel" | ||
| ouiaId={`bulk-assign-${taxType}-modal-cancel-button`} | ||
| variant="link" | ||
| onClick={handleModalClose} | ||
| > | ||
| {__('Cancel')} | ||
| </Button>, | ||
| ]; | ||
|
|
||
| const selectedTreeViewData = [ | ||
| { | ||
| name: __('Selected hosts'), | ||
| id: 'selected-hosts-tree-view-title', | ||
| customBadgeContent: selectAllHostsMode ? 'All' : selectedCount, | ||
| }, | ||
| ]; | ||
|
|
||
| return ( | ||
| <Modal | ||
| isOpen={isOpen} | ||
| onClose={handleModalClose} | ||
| onEscapePress={handleModalClose} | ||
| title={org ? __('Change organization') : __('Change location')} | ||
| width="50%" | ||
| position="top" | ||
| actions={modalActions} | ||
| id={`bulk-assign-${taxType}-modal`} | ||
| key={`bulk-assign-${taxType}-modal`} | ||
| ouiaId={`bulk-assign-${taxType}-modal`} | ||
| > | ||
| <TextContent> | ||
| <Text ouiaId={`bulk-assign-${taxType}-text`}>{modalText}</Text> | ||
| </TextContent> | ||
| {taxResults && status === STATUS.RESOLVED && ( | ||
| <TaxonomySelect | ||
| headerText={org ? __('Select organization') : __('Select location')} | ||
| taxonomy={taxType} | ||
| isOpen={selectOpen} | ||
| selected={taxId} | ||
| onSelect={handleSelect} | ||
| onOpenChange={isSelectOpen => setSelectOpen(isSelectOpen)} | ||
| toggle={toggle} | ||
| radioChecked={fixRadioChecked} | ||
| setRadioChecked={setFixRadioChecked} | ||
| > | ||
| {taxResults.results?.map(tax => ( | ||
| <SelectOption key={tax.id} value={tax.id}> | ||
| {tax.name} | ||
| </SelectOption> | ||
| ))} | ||
| </TaxonomySelect> | ||
| )} | ||
| <div style={{ width: '70%', maxHeight: '50%', marginLeft: '-1rem' }}> | ||
| <TreeView | ||
| data={selectedTreeViewData} | ||
| aria-label={__('Selected hosts')} | ||
| hasBadges | ||
| /> | ||
| </div> | ||
| </Modal> | ||
| ); | ||
| }; | ||
|
|
||
| BulkAssignTaxonomyModal.propTypes = { | ||
| isOpen: PropTypes.bool, | ||
| closeModal: PropTypes.func, | ||
| selectedCount: PropTypes.number.isRequired, | ||
| selectAllHostsMode: PropTypes.bool.isRequired, | ||
| fetchBulkParams: PropTypes.func.isRequired, | ||
| modalType: PropTypes.string.isRequired, | ||
| }; | ||
|
|
||
| BulkAssignTaxonomyModal.defaultProps = { | ||
| isOpen: false, | ||
| closeModal: () => {}, | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have not seen a good argument why you use
@hosts.countinstead of lettingassign_taxonomyreturn the number of affected rows. I started that discussion in #10538 (comment) and thought you'd use the separation to easily implement that.Please make the change I suggested. Summarizing what I said earlier:
@hosts.countwill always return the number of rows.update_allreturns the number of affected rows.0 <= affected <= number of rows. If you runupdate_alla second time the number of affected is 0.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of showing how many hosts are updated in a popup, we could display how many hosts are selected in the modal itself. I prefer the modal approach.
I need the
ForemanActionsBarContext.Providerchange in #10560 to display the selected hosts in the modal. Any chance to merge that PR first?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still going to insist that you don't use
@hosts.countbut rather use the result ofupdate_all. MakingBulkHostsManagerresponsible for that is IMHO a cleaner abstraction and it saves a query. I don't understand why you resist that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hosts.update_all(organization_id: 1)would always update all of them. That is@hosts.countif the command is successful.Otherwise it would throw an error if it fails.
We don't need to do it as you suggested that is unnecessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In other words,
@hosts.countis the result ofupdate_allhere if it is successful.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not right. It is either
@hosts.countor an error we would catch.