-
Notifications
You must be signed in to change notification settings - Fork 96
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
Fixes #38165 - Add checkboxes actions to job invocation page #944
Draft
kmalyjur
wants to merge
1
commit into
theforeman:master
Choose a base branch
from
kmalyjur:job-inv-table-checkboxes-clean
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+384
−31
Draft
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 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,182 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { | ||
Button, | ||
Dropdown, | ||
DropdownItem, | ||
KebabToggle, | ||
} from '@patternfly/react-core'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { translate as __ } from 'foremanReact/common/I18n'; | ||
import { addToast } from 'foremanReact/components/ToastsList'; | ||
import { APIActions } from 'foremanReact/redux/API'; | ||
import { selectTaskCancelable } from './JobInvocationSelectors'; | ||
import { hasPermission } from './JobInvocationConstants'; | ||
|
||
export const CheckboxesActions = ({ | ||
jobID, | ||
allHostsIds, | ||
bulkParams, | ||
selectedCount, | ||
isAllSelected, | ||
currentPermissions, | ||
permissionsStatus, | ||
}) => { | ||
const [isOpen, setIsOpen] = React.useState(false); | ||
const isTaskCancelable = useSelector(selectTaskCancelable); | ||
const dispatch = useDispatch(); | ||
|
||
const getIdsArray = () => { | ||
if (!bulkParams) { | ||
return isAllSelected ? allHostsIds : []; | ||
} | ||
// bulkParams in format `id ^ (1,2,3)` | ||
const includeIdsMatch = bulkParams.match(/id \^ \(([^)]+)\)/); | ||
if (includeIdsMatch) { | ||
return includeIdsMatch[1].split(',').map(id => id.trim()); | ||
} | ||
// bulkParams in format `id !^ (1,2,3)` | ||
const excludeIdsMatch = bulkParams.match(/id !\^ \(([^)]+)\)/); | ||
if (excludeIdsMatch) { | ||
const excludedIds = excludeIdsMatch[1] | ||
.split(',') | ||
.map(id => Number(id.trim())); | ||
return allHostsIds.filter(id => !excludedIds.includes(id)); | ||
} | ||
return []; | ||
}; | ||
|
||
const onFocus = () => { | ||
const element = document.getElementById('toggle-kebab'); | ||
element.focus(); | ||
}; | ||
const onSelect = () => { | ||
setIsOpen(false); | ||
onFocus(); | ||
}; | ||
const idsArray = getIdsArray(); | ||
console.log('idsArray ', idsArray); | ||
|
||
const getRerunUrl = () => | ||
idsArray?.length | ||
? `/job_invocations/${jobID}/rerun?${idsArray | ||
.map(id => `host_ids[]=${id}`) | ||
.join('&')}` | ||
: null; | ||
|
||
const handleTaskAction = action => { | ||
if (idsArray) { | ||
idsArray.forEach(taskID => { | ||
dispatch( | ||
addToast({ | ||
key: `${action}-job-info-${taskID}`, | ||
type: 'info', | ||
message: __(`Trying to ${action} the task for the host`), | ||
}) | ||
); | ||
|
||
dispatch( | ||
APIActions.post({ | ||
url: `/foreman_tasks/tasks/${taskID}/${action}`, | ||
key: `${action.toUpperCase()}_TASK_${taskID}`, | ||
errorToast: ({ responseError }) => responseError.data.message, | ||
successToast: () => | ||
action === 'cancel' | ||
? __(`Task for the host cancelled successfully`) | ||
: __(`Task for the host aborted successfully`), | ||
}) | ||
); | ||
}); | ||
} | ||
}; | ||
|
||
const RerunButton = () => ( | ||
<Button | ||
ouiaId="template-invocation-new-tab-button" | ||
href={getRerunUrl()} | ||
variant="secondary" | ||
component="a" | ||
isDisabled={ | ||
selectedCount === 0 || | ||
!hasPermission( | ||
currentPermissions, | ||
permissionsStatus, | ||
'create_job_invocations' | ||
) | ||
} | ||
> | ||
{__('Rerun')} | ||
</Button> | ||
); | ||
|
||
const dropdownItems = [ | ||
<DropdownItem | ||
ouiaId="cancel-host-dropdown-item" | ||
onClick={() => handleTaskAction('cancel')} | ||
key="cancel" | ||
component="button" | ||
isDisabled={ | ||
selectedCount === 0 || | ||
!isTaskCancelable || | ||
!hasPermission( | ||
currentPermissions, | ||
permissionsStatus, | ||
'cancel_job_invocations' | ||
) | ||
} | ||
> | ||
{__('Cancel')} | ||
</DropdownItem>, | ||
<DropdownItem | ||
ouiaId="abort-host-dropdown-item" | ||
onClick={() => handleTaskAction('abort')} | ||
key="abort" | ||
component="button" | ||
isDisabled={ | ||
selectedCount === 0 || | ||
!isTaskCancelable || | ||
!hasPermission( | ||
currentPermissions, | ||
permissionsStatus, | ||
'cancel_job_invocations' | ||
) | ||
} | ||
> | ||
{__('Abort')} | ||
</DropdownItem>, | ||
]; | ||
|
||
const ActionsKebab = () => ( | ||
<Dropdown | ||
ouiaId="host-actions-dropdown-kebab" | ||
onSelect={onSelect} | ||
toggle={<KebabToggle id="toggle-kebab" onToggle={setIsOpen} />} | ||
isOpen={isOpen} | ||
isPlain | ||
dropdownItems={dropdownItems} | ||
/> | ||
); | ||
|
||
return ( | ||
<> | ||
<RerunButton /> | ||
<ActionsKebab /> | ||
</> | ||
); | ||
}; | ||
|
||
CheckboxesActions.propTypes = { | ||
jobID: PropTypes.string.isRequired, | ||
allHostsIds: PropTypes.array.isRequired, | ||
bulkParams: PropTypes.string, | ||
selectedCount: PropTypes.number.isRequired, | ||
isAllSelected: PropTypes.bool.isRequired, | ||
currentPermissions: PropTypes.array, | ||
permissionsStatus: PropTypes.string, | ||
}; | ||
|
||
CheckboxesActions.defaultProps = { | ||
bulkParams: undefined, | ||
currentPermissions: [], | ||
permissionsStatus: undefined, | ||
}; |
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
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.
can you please space between rerun and open all in new tabs?

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.
Of course, sorry, I haven't even looked at it after rebasing