-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12011 from influxdata/feat(variables)/edit-variable
Add ability to update a variable
- Loading branch information
Showing
10 changed files
with
281 additions
and
22 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
145 changes: 145 additions & 0 deletions
145
ui/src/organizations/components/UpdateVariableOverlay.tsx
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,145 @@ | ||
// Libraries | ||
import React, {PureComponent, ChangeEvent, FormEvent} from 'react' | ||
import _ from 'lodash' | ||
|
||
// Components | ||
import { | ||
OverlayBody, | ||
OverlayHeading, | ||
OverlayFooter, | ||
ComponentStatus, | ||
OverlayContainer, | ||
Form, | ||
Input, | ||
} from 'src/clockface' | ||
import {Button, ButtonType, ComponentColor} from '@influxdata/clockface' | ||
import FluxEditor from 'src/shared/components/FluxEditor' | ||
|
||
// Types | ||
import {Variable} from '@influxdata/influx' | ||
|
||
interface Props { | ||
variable: Variable | ||
onCloseOverlay: () => void | ||
onUpdateVariable: (variable: Variable) => Promise<void> | ||
} | ||
|
||
interface State { | ||
variable: Variable | ||
script: string | ||
nameErrorMessage: string | ||
nameInputStatus: ComponentStatus | ||
} | ||
|
||
export default class UpdateVariableOverlay extends PureComponent<Props, State> { | ||
constructor(props) { | ||
super(props) | ||
|
||
const {variable} = this.props | ||
const script = _.get(variable, 'arguments.values.query', '') | ||
|
||
this.state = { | ||
variable, | ||
script, | ||
nameInputStatus: ComponentStatus.Default, | ||
nameErrorMessage: '', | ||
} | ||
} | ||
|
||
public render() { | ||
const {onCloseOverlay} = this.props | ||
const {variable, nameInputStatus, nameErrorMessage, script} = this.state | ||
|
||
return ( | ||
<OverlayContainer maxWidth={1000}> | ||
<OverlayHeading | ||
title="Edit Variable" | ||
onDismiss={this.props.onCloseOverlay} | ||
/> | ||
|
||
<Form onSubmit={this.handleSubmit}> | ||
<OverlayBody> | ||
<div className="overlay-flux-editor--spacing"> | ||
<Form.Element label="Name" errorMessage={nameErrorMessage}> | ||
<Input | ||
placeholder="Give your variable a name" | ||
name="name" | ||
autoFocus={true} | ||
value={variable.name} | ||
onChange={this.handleChangeInput} | ||
status={nameInputStatus} | ||
/> | ||
</Form.Element> | ||
</div> | ||
|
||
<Form.Element label="Value"> | ||
<div className="overlay-flux-editor"> | ||
<FluxEditor | ||
script={script} | ||
onChangeScript={this.handleChangeScript} | ||
visibility="visible" | ||
suggestions={[]} | ||
/> | ||
</div> | ||
</Form.Element> | ||
|
||
<OverlayFooter> | ||
<Button | ||
text="Cancel" | ||
color={ComponentColor.Danger} | ||
onClick={onCloseOverlay} | ||
/> | ||
<Button | ||
text="Submit" | ||
type={ButtonType.Submit} | ||
color={ComponentColor.Primary} | ||
status={ | ||
this.isVariableValid | ||
? ComponentStatus.Default | ||
: ComponentStatus.Disabled | ||
} | ||
/> | ||
</OverlayFooter> | ||
</OverlayBody> | ||
</Form> | ||
</OverlayContainer> | ||
) | ||
} | ||
|
||
private get isVariableValid(): boolean { | ||
const {variable, script} = this.state | ||
|
||
return !!variable.name && !!script | ||
} | ||
|
||
private handleSubmit = (e: FormEvent): void => { | ||
e.preventDefault() | ||
|
||
this.props.onUpdateVariable(this.state.variable) | ||
this.props.onCloseOverlay() | ||
} | ||
|
||
private handleChangeScript = (script: string): void => { | ||
this.setState({script}) | ||
} | ||
|
||
private handleChangeInput = (e: ChangeEvent<HTMLInputElement>) => { | ||
const value = e.target.value | ||
const key = e.target.name | ||
const variable = {...this.state.variable, [key]: value} | ||
|
||
if (!value) { | ||
return this.setState({ | ||
variable, | ||
nameInputStatus: ComponentStatus.Error, | ||
nameErrorMessage: `Variable ${key} cannot be empty`, | ||
}) | ||
} | ||
|
||
this.setState({ | ||
variable, | ||
nameInputStatus: ComponentStatus.Valid, | ||
nameErrorMessage: '', | ||
}) | ||
} | ||
} |
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.