From 676ed9a57625a0b9ede1db9f6d5d8c37ad01fc21 Mon Sep 17 00:00:00 2001 From: mshriver Date: Tue, 4 Feb 2025 08:07:31 -0500 Subject: [PATCH] Convert EditWidgetModal --- frontend/src/components/edit-widget-modal.js | 276 +++++++++---------- frontend/src/components/index.js | 1 - frontend/src/dashboard.js | 3 +- 3 files changed, 134 insertions(+), 146 deletions(-) diff --git a/frontend/src/components/edit-widget-modal.js b/frontend/src/components/edit-widget-modal.js index 03425342..74ee90e3 100644 --- a/frontend/src/components/edit-widget-modal.js +++ b/frontend/src/components/edit-widget-modal.js @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useEffect, useState } from 'react'; import PropTypes from 'prop-types'; import { @@ -20,168 +20,156 @@ import { Settings } from '../settings'; import { linkifyDecorator } from './decorators'; +const EditWidgetModal = (props) => { + const { + onSave, + onClose, + isOpen, + data, + } = props; -export class EditWidgetModal extends React.Component { - static propTypes = { - onSave: PropTypes.func, - onClose: PropTypes.func, - isOpen: PropTypes.bool, - data: PropTypes.object, - }; - - constructor(props) { - super(props); - this.state = { - widgetType: null, - title: props.data.title, - params: props.data.params, - weight: props.data.weight, - isTitleValid: true, - areParamsFilled: true, - componentLoaded: false, - }; - } + const [widgetType, setWidgetType] = useState(null); + const [title, setTitle] = useState(); + const [weight, setWeight] = useState(); + // TODO: move the widget params to their own component to better handle validation? + const [componentLoaded, setComponentLoaded] = useState(false); + const [params, setParams] = useState(); - onNameChange = (name) => { - this.setState({name}); - } + const [isTitleValid, setIsTitleValid] = useState(title !== ''); + const [saveButtonDisabled, setSaveButtonDisabled] = useState(false); - onExpiryDateChange = (expiryStr) => { - this.setState({expiryDate: expiryStr}); - } - - onSave = () => { + const onSaveModal = () => { const updatedWidget = { - title: this.state.title, - params: this.state.params, - weight: parseInt(this.state.weight), + title: title, + params: params, + weight: parseInt(weight) || 0, // 400 if this is null type: 'widget', - widget: this.props.data.widget + widget: data.widget } - this.props.onSave(updatedWidget); - this.setState({ - widgetType: null, - title: '', - params: {}, - weight: 0, - isTitleValid: false, - areParamsFilled: false - }); - } - - onClose = () => { - this.setState({ - title: '', - params: {}, - weight: 0, - isTitleValid: false, - areParamsFilled: false, - }); - this.props.onClose(); - } + onSave(updatedWidget); - onTitleChange = (value) => { - this.setState({title: value, isTitleValid: (value !== '')}); + setTitle(''); + setParams({}); + setWeight(0); + setIsTitleValid(false); + setWidgetType(); } - onWeightChange = (value) => { - this.setState({weight: value}); + const onCloseModal = () => { + setTitle(''); + setParams({}); + setWeight(0); + setIsTitleValid(false); + onClose(); } - onParamChange = (value, event) => { - const params = this.state.params; - let areParamsFilled = true; - if (event) { - params[event.target.name] = value; - } - this.setState({params: params}); - this.state.widgetType.params.forEach(widgetParam => { - if ((widgetParam.required) && (!params[widgetParam.name])) { - areParamsFilled = false; - } - }); - this.setState({areParamsFilled: areParamsFilled}); + useEffect(() => { + setTitle(data.title); + setWeight(data ? data.weight : 0); + setParams(data.params); + }, [data]) + + useEffect(() => { + let validCheck = (title !== '') + setIsTitleValid(validCheck); + if (validCheck) {setSaveButtonDisabled(false)} + else {setSaveButtonDisabled(true)} + }, [title]) + + const onParamChange = (value, event) => { + setParams( + ...params, + params[event.target.name] = (event) ? value : params[event.target.name] + ); } - componentDidMount() { + useEffect(() =>{ HttpClient.get([Settings.serverUrl, 'widget', 'types'], {'type': 'widget'}) - .then(response => HttpClient.handleResponse(response)) - .then(data => { - data.types.forEach(type => { - if (type.id == this.props.data.widget) { - this.setState({widgetType: type}); - this.setState({componentLoaded: true}); - } - }); + .then(response => HttpClient.handleResponse(response)) + .then(data => { + data.types.forEach(type => { + if (type.id == data.widget) { + setWidgetType(type); + setComponentLoaded(true); + } }); - } - - render () { - const { widgetType, componentLoaded } = this.state; - return ( - Save, - - ]} - > -
- - this.onTitleChange(value)} validated={this.state.isTitleValid} isRequired /> - {this.state.isTitleValid !== true && ( - - - } variant="error"> - Please enter a title for this widget - - - - )} - - - this.onWeightChange(value)} /> + }); + }, []) + + return ( + Save, + + ]} + > + + + setTitle(value)} validated={isTitleValid.toString()} isRequired /> + {isTitleValid !== true && ( - - How widgets are ordered on the dashboard + } variant="error"> + Please enter a title for this widget - - {componentLoaded ? widgetType.params.map(param => ( - - - this.onParamChange(value, event)} - isRequired={param.required} - /> - - - - - {param.description} - - - - - - - )): ''} - - - ); - } + )} +
+ + setWeight(value)} /> + + + + How widgets are ordered on the dashboard + + + + + {componentLoaded ? widgetType.params.map(param => ( + + + onParamChange(value, event)} + isRequired={param.required} + /> + + + + + {param.description} + + + + + + + )): ''} + +
+ ); + +} + +EditWidgetModal.propTypes = { + onSave: PropTypes.func, + onClose: PropTypes.func, + isOpen: PropTypes.bool, + data: PropTypes.object, } + +export default EditWidgetModal; diff --git a/frontend/src/components/index.js b/frontend/src/components/index.js index cb9e26b1..a0c03b1a 100644 --- a/frontend/src/components/index.js +++ b/frontend/src/components/index.js @@ -14,4 +14,3 @@ export { TabTitle } from './tabs'; export { TableEmptyState, TableErrorState } from './tablestates'; export { UserDropdown } from './user-dropdown'; export { View } from './view'; -export { EditWidgetModal } from './edit-widget-modal'; diff --git a/frontend/src/dashboard.js b/frontend/src/dashboard.js index 31d0bade..6c68bbc2 100644 --- a/frontend/src/dashboard.js +++ b/frontend/src/dashboard.js @@ -32,7 +32,8 @@ import TimesCircleIcon from '@patternfly/react-icons/dist/esm/icons/times-circle import { HttpClient } from './services/http'; import { KNOWN_WIDGETS } from './constants'; import { Settings } from './settings'; -import { NewDashboardModal, NewWidgetWizard, EditWidgetModal } from './components'; +import { NewDashboardModal, NewWidgetWizard } from './components'; +import EditWidgetModal from './components/edit-widget-modal.js' import DeleteModal from './components/delete-modal.js'; import { GenericAreaWidget,