Skip to content

Commit

Permalink
Fixes #36887 - Add Schedule a Job to new host overview
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylenz committed Nov 2, 2023
1 parent 527dcda commit 046bbb6
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 17 deletions.
6 changes: 4 additions & 2 deletions webpack/react_app/components/FeaturesDropdown/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { foremanUrl } from 'foremanReact/common/helpers';
import { sprintf, translate as __ } from 'foremanReact/common/I18n';
import { post } from 'foremanReact/redux/API';

export const runFeature = (hostId, feature, label) => dispatch => {
export const runFeature = (hostId, feature, label, hostSearch) => dispatch => {
const url = foremanUrl(
`/job_invocations?feature=${feature}&host_ids%5B%5D=${hostId}`
hostId
? `/job_invocations?feature=${feature}&host_ids%5B%5D=${hostId}`
: `/job_invocations?feature=${feature}&search=${hostSearch}`
);
const redirectUrl = 'job_invocations/new';

Expand Down
3 changes: 0 additions & 3 deletions webpack/react_app/components/FeaturesDropdown/constant.js

This file was deleted.

5 changes: 5 additions & 0 deletions webpack/react_app/components/FeaturesDropdown/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const REX_FEATURES_HOST_URL = host =>
`/api/v2/hosts/${host}/available_remote_execution_features`;
export const ALL_REX_FEATURES_URL = '/api/v2/remote_execution_features';
export const NEW_JOB_PAGE = '/job_invocations/new?host_ids%5B%5D';
export const ALL_HOSTS_NEW_JOB_PAGE = '/job_invocations/new?search';
41 changes: 29 additions & 12 deletions webpack/react_app/components/FeaturesDropdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,48 @@ import { translate as __ } from 'foremanReact/common/I18n';
import { foremanUrl } from 'foremanReact/common/helpers';
import { STATUS } from 'foremanReact/constants';

import { REX_FEATURES_API, NEW_JOB_PAGE } from './constant';
import {
REX_FEATURES_HOST_URL,
ALL_REX_FEATURES_URL,
NEW_JOB_PAGE,
ALL_HOSTS_NEW_JOB_PAGE,
} from './constants';
import { runFeature } from './actions';
import './index.scss';

const FeaturesDropdown = ({ hostId }) => {
const FeaturesDropdown = ({ hostId, hostSearch, selectedCount }) => {
const [isOpen, setIsOpen] = useState(false);
const { response, status } = useAPI(
'get',
foremanUrl(REX_FEATURES_API(hostId))
);
const rexFeaturesUrl = hostId
? REX_FEATURES_HOST_URL(hostId)
: ALL_REX_FEATURES_URL;
const { response, status } = useAPI('get', foremanUrl(rexFeaturesUrl));
const dispatch = useDispatch();
// eslint-disable-next-line camelcase
const canRunJob = response?.permissions?.can_run_job;
if (!canRunJob) {
if (!!hostId && !canRunJob) {
return null;
}
// eslint-disable-next-line camelcase
const features = response?.remote_execution_features;

const features = hostId
? response?.remote_execution_features // eslint-disable-line camelcase
: response?.results;
const dropdownItems = features
?.filter(feature => feature.host_action_button)
?.map(({ name, label, id, description }) => (
<DropdownItem
onClick={() => dispatch(runFeature(hostId, label, name))}
onClick={() => dispatch(runFeature(hostId, label, name, hostSearch))}
key={id}
description={description}
>
{name}
</DropdownItem>
));
const newJobPageUrl = hostId
? `${NEW_JOB_PAGE}=${hostId}`
: `${ALL_HOSTS_NEW_JOB_PAGE}=${hostSearch}`;
const scheduleJob = [
<DropdownToggleAction
onClick={() => dispatch(push(`${NEW_JOB_PAGE}=${hostId}`))}
onClick={() => dispatch(push(newJobPageUrl))}
key="schedule-job-action"
>
{__('Schedule a job')}
Expand All @@ -54,15 +65,17 @@ const FeaturesDropdown = ({ hostId }) => {
return (
<Dropdown
ouiaId="schedule-a-job-dropdown"
id="schedule-a-job-dropdown"
alignments={{ default: 'right' }}
onSelect={() => setIsOpen(false)}
toggle={
<DropdownToggle
ouiaId="schedule-a-job-dropdown-toggle"
id="schedule-a-job-dropdown-toggle"
splitButtonItems={scheduleJob}
toggleVariant="secondary"
onToggle={() => setIsOpen(prev => !prev)}
isDisabled={status === STATUS.PENDING}
isDisabled={status === STATUS.PENDING || selectedCount === 0}
splitButtonVariant="action"
/>
}
Expand All @@ -74,9 +87,13 @@ const FeaturesDropdown = ({ hostId }) => {

FeaturesDropdown.propTypes = {
hostId: PropTypes.number,
hostSearch: PropTypes.string,
selectedCount: PropTypes.number,
};
FeaturesDropdown.defaultProps = {
hostId: undefined,
hostSearch: undefined,
selectedCount: 0,
};

export default FeaturesDropdown;
11 changes: 11 additions & 0 deletions webpack/react_app/components/FeaturesDropdown/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#schedule-a-job-dropdown ul.pf-c-dropdown__menu {
padding-left: 0;
li {
display: unset;
a {
font-size: 16px;
color: var(--pf-c-dropdown__menu-item--Color);
font-weight: 300;
}
}
}
6 changes: 6 additions & 0 deletions webpack/react_app/extend/Fills.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ const fills = [
component: props => <FeaturesDropdown {...props} />,
weight: 1000,
},
{
slot: '_all-hosts-schedule-a-job',
name: '_all-hosts-schedule-a-job',
component: props => <FeaturesDropdown {...props} />,
weight: 1000,
},
];

const registerFills = () => {
Expand Down

0 comments on commit 046bbb6

Please sign in to comment.