diff --git a/ui/src/ifql/components/FuncArg.tsx b/ui/src/ifql/components/FuncArg.tsx new file mode 100644 index 0000000000..ebd626d7d8 --- /dev/null +++ b/ui/src/ifql/components/FuncArg.tsx @@ -0,0 +1,63 @@ +import React, {PureComponent} from 'react' + +import FuncArgInput, {OnChangeArg} from 'src/ifql/components/FuncArgInput' +import * as types from 'src/ifql/constants/argumentTypes' + +interface Props { + funcID: string + argKey: string + value: string + type: string + onChangeArg: OnChangeArg +} + +class FuncArg extends PureComponent { + public render() { + const {argKey, value, type, onChangeArg, funcID} = this.props + + switch (true) { + case this.isInput: { + return ( + + ) + } + case types.BOOL === type: { + // TODO: make boolean arg component + return ( +
+ {argKey} : {value} +
+ ) + } + case types.FUNCTION === type: { + // TODO: make separate function component + return ( +
+ {argKey} : {value} +
+ ) + } + default: { + return ( +
+ {argKey} : {value} +
+ ) + } + } + } + + private get isInput() { + const {type} = this.props + + return type !== types.FUNCTION || types.NIL || types.BOOL || types.INVALID + } +} + +export default FuncArg diff --git a/ui/src/ifql/components/FuncArgInput.tsx b/ui/src/ifql/components/FuncArgInput.tsx new file mode 100644 index 0000000000..088ddaf246 --- /dev/null +++ b/ui/src/ifql/components/FuncArgInput.tsx @@ -0,0 +1,46 @@ +import React, {PureComponent, ChangeEvent} from 'react' + +export type OnChangeArg = (inputArg: InputArg) => void + +export interface InputArg { + funcID: string + key: string + value: string +} + +interface Props { + funcID: string + argKey: string + value: string + type: string + onChangeArg: OnChangeArg +} + +class FuncArgInput extends PureComponent { + public render() { + const {argKey, value, type} = this.props + return ( +
+ + +
+ ) + } + + private handleChange = (e: ChangeEvent) => { + const {funcID, argKey} = this.props + console.log(this.props) + this.props.onChangeArg({funcID, key: argKey, value: e.target.value}) + } +} + +export default FuncArgInput diff --git a/ui/src/ifql/components/FuncArgs.tsx b/ui/src/ifql/components/FuncArgs.tsx index f9339aca6d..aab1ba9a74 100644 --- a/ui/src/ifql/components/FuncArgs.tsx +++ b/ui/src/ifql/components/FuncArgs.tsx @@ -1,4 +1,6 @@ import React, {PureComponent} from 'react' +import FuncArg from 'src/ifql/components/FuncArg' +import {OnChangeArg} from 'src/ifql/components/FuncArgInput' interface Arg { key: string @@ -15,17 +17,27 @@ export interface Func { interface Props { func: Func + onChangeArg: OnChangeArg } export default class FuncArgs extends PureComponent { public render() { + const {func, onChangeArg} = this.props + return (
- {this.props.func.args.map(({key, value}) => ( -
- {key} : {value} -
- ))} + {func.args.map(({key, value, type}) => { + return ( + + ) + })}
) } diff --git a/ui/src/ifql/components/FuncNode.tsx b/ui/src/ifql/components/FuncNode.tsx index 589f1122ff..e145e80aed 100644 --- a/ui/src/ifql/components/FuncNode.tsx +++ b/ui/src/ifql/components/FuncNode.tsx @@ -1,10 +1,12 @@ import React, {PureComponent, MouseEvent} from 'react' import FuncArgs from 'src/ifql/components/FuncArgs' import {Func} from 'src/ifql/components/FuncArgs' +import {OnChangeArg} from 'src/ifql/components/FuncArgInput' interface Props { func: Func onDelete: (id: string) => void + onChangeArg: OnChangeArg } interface State { @@ -20,7 +22,7 @@ export default class FuncNode extends PureComponent { } public render() { - const {func} = this.props + const {func, onChangeArg} = this.props const {isOpen} = this.state return ( @@ -28,7 +30,7 @@ export default class FuncNode extends PureComponent {
{func.name}
- {isOpen && } + {isOpen && }
@@ -40,7 +42,7 @@ export default class FuncNode extends PureComponent { this.props.onDelete(this.props.func.id) } - private handleClick = (e: MouseEvent) => { + private handleClick = (e: MouseEvent): void => { e.stopPropagation() const {isOpen} = this.state diff --git a/ui/src/ifql/components/TimeMachine.tsx b/ui/src/ifql/components/TimeMachine.tsx index 8dd12e6c15..11472bd815 100644 --- a/ui/src/ifql/components/TimeMachine.tsx +++ b/ui/src/ifql/components/TimeMachine.tsx @@ -4,6 +4,7 @@ import FuncNode from 'src/ifql/components/FuncNode' import TimeMachineEditor from 'src/ifql/components/TimeMachineEditor' import {Func} from 'src/ifql/components/FuncArgs' +import {OnChangeArg} from 'src/ifql/components/FuncArgInput' export interface Suggestion { name: string @@ -20,6 +21,7 @@ interface Props { onChangeScript: (script: string) => void onSubmitScript: (script: string) => void onDeleteFuncNode: (id: string) => void + onChangeArg: OnChangeArg } class TimeMachine extends PureComponent { @@ -31,6 +33,7 @@ class TimeMachine extends PureComponent { onChangeScript, onSubmitScript, onDeleteFuncNode, + onChangeArg, } = this.props return ( @@ -42,7 +45,12 @@ class TimeMachine extends PureComponent { />
{funcs.map(f => ( - + ))}
diff --git a/ui/src/ifql/constants/argumentTypes.ts b/ui/src/ifql/constants/argumentTypes.ts new file mode 100644 index 0000000000..6e768fd161 --- /dev/null +++ b/ui/src/ifql/constants/argumentTypes.ts @@ -0,0 +1,13 @@ +export const INVALID = 'invalid' +export const NIL = 'nil' +export const STRING = 'string' +export const INT = 'int' +export const UINT = 'uint' +export const FLOAT = 'float' +export const BOOL = 'bool' +export const TIME = 'time' +export const DURATION = 'duration' +export const REGEXP = 'regexp' +export const ARRAY = 'array' +export const OBJECT = 'object' +export const FUNCTION = 'function' diff --git a/ui/src/ifql/containers/IFQLPage.tsx b/ui/src/ifql/containers/IFQLPage.tsx index 51924c385f..a87708e803 100644 --- a/ui/src/ifql/containers/IFQLPage.tsx +++ b/ui/src/ifql/containers/IFQLPage.tsx @@ -7,6 +7,7 @@ import _ from 'lodash' import TimeMachine, {Suggestion} from 'src/ifql/components/TimeMachine' import Walker from 'src/ifql/ast/walker' import {Func} from 'src/ifql/components/FuncArgs' +import {InputArg} from 'src/ifql/components/FuncArgInput' import {getSuggestions, getAST} from 'src/ifql/apis' @@ -70,6 +71,7 @@ export class IFQLPage extends PureComponent { funcs={this.state.funcs} suggestions={suggestions} onAddNode={this.handleAddNode} + onChangeArg={this.handleChangeArg} onSubmitScript={this.getASTResponse} onChangeScript={this.handleChangeScript} onDeleteFuncNode={this.handleDeleteFuncNode} @@ -80,6 +82,26 @@ export class IFQLPage extends PureComponent { ) } + private handleChangeArg = ({funcID, key, value}: InputArg) => { + const funcs = this.state.funcs.map(f => { + if (f.id !== funcID) { + return f + } + + const args = f.args.map(a => { + if (a.key === key) { + return {...a, value} + } + + return a + }) + + return {...f, args} + }) + + this.setState({funcs}) + } + private handleChangeScript = (script: string): void => { this.setState({script}) } diff --git a/ui/test/ifql/components/TimeMachine.test.tsx b/ui/test/ifql/components/TimeMachine.test.tsx index 79f36471ec..f74727f2bd 100644 --- a/ui/test/ifql/components/TimeMachine.test.tsx +++ b/ui/test/ifql/components/TimeMachine.test.tsx @@ -11,6 +11,7 @@ const setup = () => { onChangeScript: () => {}, onSubmitScript: () => {}, onDeleteFuncNode: () => {}, + onChangeArg: () => {}, } const wrapper = shallow()