forked from appsmithorg/appsmith
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomGraphQLActionsConfigControl.tsx
More file actions
184 lines (173 loc) · 6.54 KB
/
CustomGraphQLActionsConfigControl.tsx
File metadata and controls
184 lines (173 loc) · 6.54 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import React from "react";
import type { ControlType } from "constants/PropertyControlConstants";
import FormControl from "pages/Editor/FormControl";
import { TabPanel, TabsList, Tab } from "@appsmith/ads";
import BaseControl, { type ControlProps } from "../BaseControl";
import { GRAPHQL_HTTP_METHOD_OPTIONS } from "PluginActionEditor/constants/GraphQLEditorConstants";
import { API_EDITOR_TAB_TITLES } from "ee/constants/messages";
import { createMessage } from "ee/constants/messages";
import styled from "styled-components";
import { useSelector } from "react-redux";
import { getFormData } from "selectors/formSelectors";
import {
CodeEditorBorder,
EditorModes,
EditorSize,
EditorTheme,
TabBehaviour,
} from "components/editorComponents/CodeEditor/EditorConfig";
import DynamicTextField from "components/editorComponents/form/fields/DynamicTextField";
import {
Section,
Zone,
} from "PluginActionEditor/components/PluginActionForm/components/ActionForm";
import { AutocompleteDataType } from "utils/autocomplete/AutocompleteDataType";
import FormLabel from "components/editorComponents/FormLabel";
import {
CustomActionFormLayout,
CUSTOM_ACTION_TABS,
TabbedWrapper,
useSyncParamsToPath,
} from "./common";
const GraphQLQueryContainer = styled.div`
&&&& .CodeMirror {
height: auto;
min-height: 150px;
}
`;
const StyledFormLabel = styled(FormLabel)`
&& {
margin-bottom: var(--ads-v2-spaces-2);
padding: 0;
}
`;
const EXPECTED_VARIABLE = {
type: "object",
example:
'{\n "name":"{{ inputName.property }}",\n "preference":"{{ dropdownName.property }}"\n}',
autocompleteDataType: AutocompleteDataType.OBJECT,
};
const TabbedControls = (props: ControlProps) => {
// Use the hook to sync params with path
useSyncParamsToPath(props.formName, props.configProperty);
const formValues = useSelector((state) => getFormData(state, props.formName));
const values = formValues?.values || {};
const actionName = values.name || "";
return (
<TabbedWrapper defaultValue={CUSTOM_ACTION_TABS.HEADERS}>
<TabsList>
{Object.values(CUSTOM_ACTION_TABS).map((tab) => {
let tabLabel: string = tab;
if (tab === CUSTOM_ACTION_TABS.HEADERS) {
tabLabel = createMessage(API_EDITOR_TAB_TITLES.HEADERS);
} else if (tab === CUSTOM_ACTION_TABS.PARAMS) {
tabLabel = createMessage(API_EDITOR_TAB_TITLES.PARAMS);
} else if (tab === CUSTOM_ACTION_TABS.BODY) {
tabLabel = createMessage(API_EDITOR_TAB_TITLES.BODY);
}
return (
<Tab data-testid={`t--graphql-editor-${tab}`} key={tab} value={tab}>
{tabLabel}
</Tab>
);
})}
</TabsList>
<TabPanel value={CUSTOM_ACTION_TABS.HEADERS}>
<FormControl
config={{
controlType: "KEYVALUE_ARRAY",
configProperty: `${props.configProperty}.headers`,
formName: props.formName,
id: `${props.configProperty}.headers`,
isValid: true,
// @ts-expect-error FormControl component has incomplete TypeScript definitions for some valid properties
showHeader: true,
}}
formName={props.formName}
/>
</TabPanel>
<TabPanel value={CUSTOM_ACTION_TABS.PARAMS}>
<FormControl
config={{
controlType: "KEYVALUE_ARRAY",
configProperty: `${props.configProperty}.params`,
formName: props.formName,
id: `${props.configProperty}.params`,
// @ts-expect-error FormControl component has incomplete TypeScript definitions for some valid properties
showHeader: true,
isValid: true,
}}
formName={props.formName}
/>
</TabPanel>
<TabPanel value={CUSTOM_ACTION_TABS.BODY}>
<GraphQLQueryContainer>
<Section isFullWidth withoutPadding>
<Zone layout="single_column">
<div className="t--graphql-query-editor">
<StyledFormLabel>Query</StyledFormLabel>
<DynamicTextField
border={CodeEditorBorder.ALL_SIDE}
dataTreePath={`${actionName}.config.${props.configProperty}.body`}
evaluatedPopUpLabel={"Query"}
mode={EditorModes.GRAPHQL_WITH_BINDING}
name={`${props.configProperty}.body`}
placeholder={`{{\n\tquery {\n\t\tname: inputName.property\n\t}\n}}`}
showLineNumbers
size={EditorSize.EXTENDED}
tabBehaviour={TabBehaviour.INDENT}
theme={EditorTheme.LIGHT}
/>
</div>
</Zone>
<Zone layout="single_column">
<div className="t--graphql-variable-editor">
<StyledFormLabel>Query variables</StyledFormLabel>
<DynamicTextField
border={CodeEditorBorder.ALL_SIDE}
dataTreePath={`${actionName}.config.${props.configProperty}.variables`}
evaluatedPopUpLabel={"Query variables"}
expected={EXPECTED_VARIABLE}
height="100%"
mode={EditorModes.JSON_WITH_BINDING}
name={`${props.configProperty}.variables`}
placeholder={`${EXPECTED_VARIABLE.example}\n\n\\\\Take widget inputs using {{ }}`}
showLightningMenu={false}
showLineNumbers
size={EditorSize.EXTENDED}
tabBehaviour={TabBehaviour.INDENT}
theme={EditorTheme.LIGHT}
/>
</div>
</Zone>
</Section>
</GraphQLQueryContainer>
</TabPanel>
</TabbedWrapper>
);
};
/**
* This component is used to configure the custom GraphQL actions for the external integration.
* It allows the user to add or update details for the custom GraphQL action like method type, path, headers, params, query, and variables.
*/
export class CustomGraphQLActionsControl extends BaseControl<ControlProps> {
getControlType(): ControlType {
return "CUSTOM_GRAPHQL_ACTIONS_CONFIG_FORM";
}
render() {
const { props } = this;
return (
<CustomActionFormLayout
configProperty={props.configProperty}
formName={props.formName}
methodOptions={GRAPHQL_HTTP_METHOD_OPTIONS.map((method) => ({
label: method.value,
value: method.value,
}))}
pathPlaceholder="/graphql"
>
<TabbedControls {...props} />
</CustomActionFormLayout>
);
}
}