diff --git a/webpack/assets/javascripts/react_app/components/HostDetails/DetailsCard/index.js b/webpack/assets/javascripts/react_app/components/HostDetails/DetailsCard/index.js index fc1288cd166..1212726115d 100644 --- a/webpack/assets/javascripts/react_app/components/HostDetails/DetailsCard/index.js +++ b/webpack/assets/javascripts/react_app/components/HostDetails/DetailsCard/index.js @@ -24,6 +24,7 @@ import { STATUS } from '../../../constants'; import DefaultLoaderEmptyState from './DefaultLoaderEmptyState'; import PowerStatusDropDown from './PowerStatus/PowerStatusDropDown'; import { foremanUrl } from '../../../common/helpers'; +import { InlineEdit } from '../InlineEdit'; import './styles.scss'; @@ -39,7 +40,10 @@ const DetailsCard = ({ owner_name: ownerName, hostgroup_name: hostgroupName, hostgroup_id: hostgroupId, - permissions: { power_hosts: hasPowerPermission } = {}, + permissions: { + power_hosts: hasPowerPermission, + edit_hosts: editPermission, + } = {}, }, }) => ( @@ -172,7 +176,12 @@ const DetailsCard = ({ emptyState={} status={status} > - {comment} + diff --git a/webpack/assets/javascripts/react_app/components/HostDetails/InlineEdit.js b/webpack/assets/javascripts/react_app/components/HostDetails/InlineEdit.js new file mode 100644 index 00000000000..7eeff9e2ee9 --- /dev/null +++ b/webpack/assets/javascripts/react_app/components/HostDetails/InlineEdit.js @@ -0,0 +1,115 @@ +import React, { useState } from 'react'; +import { useDispatch } from 'react-redux'; +import PropTypes from 'prop-types'; +import { Button, TextInput, Flex, FlexItem } from '@patternfly/react-core'; +import { PencilAltIcon, CheckIcon, TimesIcon } from '@patternfly/react-icons'; + +import { APIActions } from '../../redux/API'; +import { sprintf, translate as __ } from '../../common/I18n'; + +export const InlineEdit = ({ + name, + defaultValue, + hostName, + editPermission, +}) => { + const [value, setValue] = useState(defaultValue); + const [isEditing, setIsEditing] = useState(false); + + const handleInputChange = newValue => { + setValue(newValue); + }; + + const dispatch = useDispatch(); + const handleSave = () => { + setIsEditing(false); + + dispatch( + APIActions.put({ + url: `/api/hosts/${hostName}`, + key: `${hostName}-${name}-EDIT`, + params: { + [name]: value, + }, + successToast: () => sprintf(__('%s saved'), name), + }) + ); + }; + const handleCancel = () => { + setIsEditing(false); + setValue(defaultValue); + }; + return ( + + {isEditing ? ( + <> + + + + + + + + + + + ) : ( + <> + +
{value}
+
+ {editPermission && ( + + + + )} + + )} +
+ ); +}; + +InlineEdit.propTypes = { + name: PropTypes.string.isRequired, + defaultValue: PropTypes.string, + hostName: PropTypes.string.isRequired, + editPermission: PropTypes.bool, +}; + +InlineEdit.defaultProps = { + defaultValue: '', + editPermission: false, +};