|
| 1 | +// Libraries |
| 2 | +import React, {FC, useContext} from 'react' |
| 3 | +import { |
| 4 | + ComponentStatus, |
| 5 | + Form, |
| 6 | + FlexBox, |
| 7 | + Input, |
| 8 | + InputType, |
| 9 | + ComponentSize, |
| 10 | +} from '@influxdata/clockface' |
| 11 | + |
| 12 | +// Types |
| 13 | +import {PipeProp} from 'src/types/flows' |
| 14 | + |
| 15 | +import {PipeContext} from 'src/flows/context/pipe' |
| 16 | + |
| 17 | +import './style.scss' |
| 18 | + |
| 19 | +const Time: FC<PipeProp> = ({Context}) => { |
| 20 | + const {data, update} = useContext(PipeContext) |
| 21 | + |
| 22 | + let startError = '' |
| 23 | + let stopError = '' |
| 24 | + |
| 25 | + if (!data.start) { |
| 26 | + startError = 'Required' |
| 27 | + } else if ( |
| 28 | + !/^-(?:(\d+(y|mo|s|m|w|h){1}))+$/g.test(data.start) && |
| 29 | + !/^((?:(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2}(?:\.\d+)?))(Z|[\+-]\d{2}:\d{2})?)$/g.test( |
| 30 | + data.start |
| 31 | + ) |
| 32 | + ) { |
| 33 | + startError = 'Invalid Time' |
| 34 | + } |
| 35 | + |
| 36 | + if (!data.stop) { |
| 37 | + stopError = 'Required' |
| 38 | + } else if ( |
| 39 | + !/^now\(\)$/g.test(data.stop) && |
| 40 | + !/^-(?:(\d+(y|mo|s|m|w|h){1}))+$/g.test(data.stop) && |
| 41 | + !/^((?:(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2}(?:\.\d+)?))(Z|[\+-]\d{2}:\d{2})?)$/g.test( |
| 42 | + data.stop |
| 43 | + ) |
| 44 | + ) { |
| 45 | + stopError = 'Invalid Time' |
| 46 | + } |
| 47 | + |
| 48 | + const updateStart = evt => { |
| 49 | + update({ |
| 50 | + start: evt.target.value, |
| 51 | + }) |
| 52 | + } |
| 53 | + |
| 54 | + const updateStop = evt => { |
| 55 | + update({ |
| 56 | + stop: evt.target.value, |
| 57 | + }) |
| 58 | + } |
| 59 | + |
| 60 | + return ( |
| 61 | + <Context> |
| 62 | + <FlexBox margin={ComponentSize.Medium}> |
| 63 | + <FlexBox.Child |
| 64 | + basis={200} |
| 65 | + grow={0} |
| 66 | + shrink={0} |
| 67 | + className="flow-panel-time--header" |
| 68 | + > |
| 69 | + <h5>Set a time frame</h5> |
| 70 | + <p> |
| 71 | + <a |
| 72 | + target="_blank" |
| 73 | + href="https://docs.influxdata.com/flux/v0.x/spec/lexical-elements/#duration-literals" |
| 74 | + > |
| 75 | + durations |
| 76 | + </a>{' '} |
| 77 | + and{' '} |
| 78 | + <a |
| 79 | + target="_blank" |
| 80 | + href="https://docs.influxdata.com/flux/v0.x/spec/lexical-elements/#date-and-time-literals" |
| 81 | + > |
| 82 | + dates |
| 83 | + </a>{' '} |
| 84 | + are valid |
| 85 | + </p> |
| 86 | + </FlexBox.Child> |
| 87 | + <FlexBox.Child grow={1} shrink={1} style={{alignSelf: 'start'}}> |
| 88 | + <Form.Element label="Start" required={true} errorMessage={startError}> |
| 89 | + <Input |
| 90 | + name="start" |
| 91 | + type={InputType.Text} |
| 92 | + placeholder="ex: -1h00s" |
| 93 | + value={data.start} |
| 94 | + onChange={updateStart} |
| 95 | + status={ |
| 96 | + startError ? ComponentStatus.Error : ComponentStatus.Default |
| 97 | + } |
| 98 | + size={ComponentSize.Medium} |
| 99 | + /> |
| 100 | + </Form.Element> |
| 101 | + </FlexBox.Child> |
| 102 | + |
| 103 | + <FlexBox.Child grow={1} shrink={1} style={{alignSelf: 'start'}}> |
| 104 | + <Form.Element label="End" required={true} errorMessage={stopError}> |
| 105 | + <Input |
| 106 | + name="end" |
| 107 | + type={InputType.Text} |
| 108 | + placeholder="ex: now()" |
| 109 | + value={data.stop} |
| 110 | + onChange={updateStop} |
| 111 | + status={ |
| 112 | + stopError ? ComponentStatus.Error : ComponentStatus.Default |
| 113 | + } |
| 114 | + size={ComponentSize.Medium} |
| 115 | + /> |
| 116 | + </Form.Element> |
| 117 | + </FlexBox.Child> |
| 118 | + </FlexBox> |
| 119 | + </Context> |
| 120 | + ) |
| 121 | +} |
| 122 | + |
| 123 | +export default Time |
0 commit comments