-
Notifications
You must be signed in to change notification settings - Fork 67
/
Controls.tsx
95 lines (88 loc) 路 3.12 KB
/
Controls.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import {Form, Radio, Typography} from 'antd';
import {toUpper} from 'lodash';
import {useEffect, useMemo} from 'react';
import Test from 'models/Test.model';
import {CliCommandFormat, CliCommandOption, TCliCommandConfig} from 'services/CliCommand.service';
import * as S from './CliCommand.styled';
import SwitchControl from './SwitchControl';
import {defaultOptions} from './hooks/useCliCommand';
interface IOptionsMetadataParams {
isEnvironmentSelected: boolean;
}
interface IOptionsMetadata {
label: string;
help?: string;
disabled?: boolean;
}
function getOptionsMetadata({
isEnvironmentSelected,
}: IOptionsMetadataParams): Record<CliCommandOption, IOptionsMetadata> {
return {
[CliCommandOption.UseId]: {label: 'Use test ID instead of file'},
[CliCommandOption.SkipResultWait]: {label: 'Skip waiting for test to complete'},
[CliCommandOption.UseHostname]: {label: 'Specify Tracetest server hostname'},
[CliCommandOption.UseCurrentEnvironment]: {
label: 'Use selected environment',
help: !isEnvironmentSelected ? 'This option is only available when an environment is selected' : undefined,
disabled: !isEnvironmentSelected,
},
[CliCommandOption.GeneratesJUnit]: {label: 'Generate JUnit report'},
[CliCommandOption.useDocker]: {label: 'Run CLI via Docker image'},
};
}
interface IProps {
onChange(cmdConfig: TCliCommandConfig): void;
test: Test;
environmentId?: string;
fileName: string;
}
const Controls = ({onChange, test, environmentId, fileName}: IProps) => {
const [form] = Form.useForm<TCliCommandConfig>();
const options = Form.useWatch('options', form);
const format = Form.useWatch('format', form);
const optionsMetadata = useMemo(() => getOptionsMetadata({isEnvironmentSelected: !!environmentId}), [environmentId]);
useEffect(() => {
onChange({
options: options ?? defaultOptions,
format: format ?? CliCommandFormat.Pretty,
test,
environmentId,
fileName,
});
}, [environmentId, fileName, format, onChange, options, test]);
return (
<Form<TCliCommandConfig>
form={form}
autoComplete="off"
initialValues={{
options: defaultOptions,
format: CliCommandFormat.Pretty,
}}
layout="horizontal"
name="CLI_COMMAND"
>
<S.ControlsContainer>
<S.OptionsContainer>
{Object.entries(optionsMetadata).map(([name, data]) => (
<Form.Item name={['options', name]} noStyle>
<SwitchControl id={name} text={data.label} key={name} disabled={data.disabled} help={data.help} />
</Form.Item>
))}
</S.OptionsContainer>
<S.FormatContainer>
<Typography.Paragraph>Output Format:</Typography.Paragraph>
<Form.Item name="format" noStyle>
<Radio.Group>
{Object.values(CliCommandFormat).map(cmdFormat => (
<Radio key={cmdFormat} value={cmdFormat}>
{toUpper(cmdFormat)}
</Radio>
))}
</Radio.Group>
</Form.Item>
</S.FormatContainer>
</S.ControlsContainer>
</Form>
);
};
export default Controls;