-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathExternalSelectorSettings.tsx
More file actions
145 lines (126 loc) · 4.96 KB
/
ExternalSelectorSettings.tsx
File metadata and controls
145 lines (126 loc) · 4.96 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import React from 'react';
import {FormRow} from '@gravity-ui/components';
import {Checkbox, TextInput} from '@gravity-ui/uikit';
import block from 'bem-cn-lite';
import {FieldWrapper} from 'components/FieldWrapper/FieldWrapper';
import {i18n} from 'i18n';
import type {CustomCommands, Spec} from 'immutability-helper';
import update, {Context} from 'immutability-helper';
import {useDispatch, useSelector} from 'react-redux';
import type {StringParams} from 'shared';
import {ControlQA} from 'shared';
import {setSelectorDialogItem} from 'ui/store/actions/controlDialog';
import {selectOpenedItemMeta, selectSelectorDialog} from 'ui/store/selectors/controlDialog';
import {EntryTypeNode} from 'ui/units/dash/modules/constants';
import {EntrySelector} from '../EntrySelector/EntrySelector';
import {ImpactTypeSelect} from '../ImpactTypeSelect/ImpactTypeSelect';
import './ExternalSelectorSettings.scss';
const b = block('external-selector-settings');
const imm = new Context();
type AutoExtendCommand<T = object> = CustomCommands<{$auto: Spec<T>}>;
imm.extend('$auto', (value, object) => {
return object ? update(object, value) : update({}, value);
});
const ExternalSelectorSettings: React.FC<{
navigationPath: string | null;
changeNavigationPath: (newNavigationPath: string) => void;
enableAutoheightDefault?: boolean;
enableGlobalSelectors?: boolean;
rowClassName?: string;
}> = (props) => {
const dispatch = useDispatch();
const {autoHeight, chartId, title, selectorParameters, validation, selectorParametersGroup} =
useSelector(selectSelectorDialog);
const {workbookId} = useSelector(selectOpenedItemMeta);
const handleAutoHeightUpdate = React.useCallback(
(value: boolean) => {
dispatch(
setSelectorDialogItem({
autoHeight: value,
}),
);
},
[dispatch],
);
const handleChartIdChange = React.useCallback(
(value: {entryId: string; name: string; params?: StringParams}) => {
const parsedParams = value.params || {};
const {selectorParameters: mergedSelectorParameters} = imm.update<
{selectorParameters: StringParams},
AutoExtendCommand<StringParams>
>(
{selectorParameters: selectorParameters || {}},
{selectorParameters: {$auto: {$merge: parsedParams}}},
);
dispatch(
setSelectorDialogItem({
chartId: value.entryId,
title: title || value.name,
selectorParameters: mergedSelectorParameters,
}),
);
dispatch(
setSelectorDialogItem({
selectorParametersGroup: (selectorParametersGroup ?? 0) + 1,
}),
);
},
[selectorParameters, dispatch, title, selectorParametersGroup],
);
const handleTitleUpdate = React.useCallback(
(newTitle: string) => {
dispatch(
setSelectorDialogItem({
title: newTitle,
}),
);
},
[dispatch],
);
React.useEffect(() => {
dispatch(setSelectorDialogItem({selectorParametersGroup: 0}));
}, [dispatch]);
return (
<React.Fragment>
<FormRow
label={i18n('dash.control-dialog.edit', 'field_title')}
className={props.rowClassName}
>
<FieldWrapper error={validation.title}>
<TextInput
qa={ControlQA.inputNameControl}
value={title}
onUpdate={handleTitleUpdate}
/>
</FieldWrapper>
</FormRow>
<EntrySelector
className={props.rowClassName}
label={i18n('dash.control-dialog.edit', 'field_source')}
entryId={chartId}
workbookId={workbookId}
errorText={validation.chartId}
isInvalid={Boolean(validation.chartId)}
onChange={handleChartIdChange}
includeClickableType={EntryTypeNode.CONTROL_NODE}
navigationPath={props.navigationPath}
changeNavigationPath={props.changeNavigationPath}
/>
{props.enableGlobalSelectors && <ImpactTypeSelect />}
{!props.enableAutoheightDefault && (
<FormRow
label={i18n('dash.control-dialog.edit', 'field_autoheight')}
className={props.rowClassName}
>
<Checkbox
className={b('checkbox-option')}
checked={autoHeight}
onUpdate={handleAutoHeightUpdate}
size="l"
/>
</FormRow>
)}
</React.Fragment>
);
};
export {ExternalSelectorSettings};