// Copyright 2021 Red Hat, Inc // // Licensed under the Apache License, Version 2.0 (the "License"); you may // not use this file except in compliance with the License. You may obtain // a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations // under the License. import React, { useEffect, useState } from 'react' import PropTypes from 'prop-types' import { connect, useDispatch } from 'react-redux' import { Button, Modal, ModalVariant, Form, FormGroup, TextInput } from '@patternfly/react-core' import { autohold } from '../../api' import { addAutoholdError } from '../../actions/adminActions' import { addNotification } from '../../actions/notifications' import { fetchAutoholds } from '../../actions/autoholds' const AutoholdModal = props => { const dispatch = useDispatch() const { tenant, user, showAutoholdModal, setShowAutoholdModal } = props const [change, setChange] = useState('') const [changeRef, setChangeRef] = useState('') const [project, setProject] = useState('some project') const [job_name, setJob_name] = useState('some job') const [reason, setReason] = useState('-') const [count, setCount] = useState(1) const [nodeHoldExpiration, setNodeHoldExpiration] = useState(86400) // Override defaults if optional parameters were passed useEffect(() => { if (props.change) { setChange(props.change) } if (props.changeRef) { setChangeRef(props.changeRef) } if (props.project) { setProject(props.project) } if (props.jobName) { setJob_name(props.jobName) } if (props.reason) { setReason(props.reason) } else { setReason( user.data ? 'Requested from the web UI by ' + user.data.profile.preferred_username : '-' ) } }, [props.change, props.changeRef, props.project, props.jobName, props.reason, user.data]) function handleConfirm() { let ah_change = change === '' ? null : change let ah_ref = changeRef === '' ? null : changeRef autohold(tenant.apiPrefix, project, job_name, ah_change, ah_ref, reason, parseInt(count), parseInt(nodeHoldExpiration)) .then(() => { /* TODO it looks like there is a delay in the registering of the autohold request by the backend, meaning we sometimes do not get the newly created request after the dispatch. A solution could be to make the autoholds page auto-refreshing like the status page.*/ dispatch(fetchAutoholds(tenant)) dispatch(addNotification( { text: 'Autohold request set successfully.', type: 'success', status: '', url: '', })) }) .catch(error => { dispatch(addAutoholdError(error)) }) setShowAutoholdModal(false) } return ( { setShowAutoholdModal(false) }} actions={[ , ]}>
{ setProject(value) }} /> { setJob_name(value) }} /> { setChange(value) }} /> { setChangeRef(value) }} /> { setReason(value) }} /> { setCount(value) }} /> { setNodeHoldExpiration(value) }} />
) } AutoholdModal.propTypes = { tenant: PropTypes.object, user: PropTypes.object, change: PropTypes.string, changeRef: PropTypes.string, project: PropTypes.string, jobName: PropTypes.string, reason: PropTypes.string, showAutoholdModal: PropTypes.bool, setShowAutoholdModal: PropTypes.func, } export default connect((state) => ({ tenant: state.tenant, user: state.user, }))(AutoholdModal)