-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathGraphQL.tsx
228 lines (210 loc) · 8.36 KB
/
GraphQL.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import React, { useContext } from 'react';
import styles from './GraphQL.module.scss';
import style from '../ReactTestComponent/Render/Render.module.scss';
import styled from '../ReactTestComponent/Render/Prop.module.scss';
import GraphQLAssertion from './GraphQLAssertion';
import { Assertion, GraphQLObj, Header, Action, EventTarget } from '../../utils/graphQLTypes';
import {
deleteGraphQL,
updateGraphQL,
addHeader,
deleteHeader,
togglePost,
updatePost,
addAssertion,
} from '../../context/actions/graphQLTestCaseActions';
import { GlobalContext } from '../../context/reducers/globalReducer';
const closeIcon = require('../../assets/images/close.png');
const minusIcon = require('../../assets/images/minus-box-outline.png');
interface GraphQLProps {
graphQL: GraphQLObj;
index: number;
dispatchToGraphQLTestCase: (action: Action) => void;
}
const GraphQL = ({ graphQL, index, dispatchToGraphQLTestCase }: GraphQLProps) => {
const [ {theme} ] = useContext<any>(GlobalContext)
const handleChangeGraphQLFields = ({ target }: EventTarget, field: string) => {
let updatedGraphQL = { ...graphQL};
field === 'headerName' || field === 'headerValue'
? (updatedGraphQL.headers[Number(target.id)][field] = target.value)
: (updatedGraphQL[field] = target.value);
dispatchToGraphQLTestCase(updateGraphQL(updatedGraphQL));
if (target.value === 'query') dispatchToGraphQLTestCase(togglePost(index));
else if (target.type === 'select-one' && graphQL.post)
dispatchToGraphQLTestCase(togglePost(index));
if (target.value === 'mutation') dispatchToGraphQLTestCase(togglePost(index));
else if (target.type === 'select-one' && graphQL.post)
dispatchToGraphQLTestCase(togglePost(index));
if (target.value === 'subscription') dispatchToGraphQLTestCase(togglePost(index));
else if (target.type === 'select-one' && graphQL.post)
dispatchToGraphQLTestCase(togglePost(index));
};
const handleClickDeleteGraphQL = () => {
// delete endpoint returns action object {type: 'DELETE_ENDPOINT, id: endpoint.id}
dispatchToGraphQLTestCase(deleteGraphQL(graphQL.id));
};
const handleClickAddHeader = () => {
dispatchToGraphQLTestCase(addHeader(index));
};
const handleClickDeleteHeader = (i: number) => {
dispatchToGraphQLTestCase(deleteHeader(index, i));
};
const updatePostData = ({ target }: EventTarget) => {
dispatchToGraphQLTestCase(updatePost(target.value, index));
target.style.height = 'inherit';
target.style.height = `${Math.max(Math.min(target.scrollHeight, 200), 102)}px`;
};
const addAssertionHandleClick = () => {
dispatchToGraphQLTestCase(addAssertion(index));
};
return (
<div style={{ maxWidth: '650px' }}>
<div
id={styles[`graphQLmodal${theme}`]}
>
<img
src={closeIcon}
id={styles.close}
alt='close'
onClick={handleClickDeleteGraphQL}
/>
<div id={styles.header}>
<h3>GraphQL</h3>
</div>
<div id={styles.groupFlexbox}>
<div id={styles.serverInput} style={{ width: '100%' }}>
<label htmlFor='test-statement'>Test</label>
<div
style={{
display: 'flex',
justifyContent: 'spaceBetween',
}}
>
<div id={styles.labelInputTest}>
<input
type='text'
id={styles.testStatement}
value={graphQL.testName}
onChange={(e) => handleChangeGraphQLFields(e, 'testName')}
/>
</div>{' '}
<button
id={styles.assertionButton}
style={{ marginTop: '9px' }}
onClick={handleClickAddHeader}
>
<i className='fas fa-plus'></i> Add Header
</button>
</div>
</div>
</div>
<div id={styles.groupFlexbox}>
<div id={styles.dropdownWrapper}>
<label htmlFor='method'>Query, Subscription, or Mutation</label>
<div id={styles.dropdownFlex}>
<select
id='method'
value={graphQL.method}
onChange={(e) => handleChangeGraphQLFields(e, 'method')}
>
<option value='' />
<option value='query'>Query</option>
<option value='mutation'>Mutation</option>
<option value='subscription'>Subscription</option>
</select>
</div>
</div>
<div id={styles.alignRight}>
<label htmlFor='route'>Route</label>
<div id={styles.inputFlexBox}>
<input
type='text'
name='route'
value={graphQL.route}
placeholder='eg. /route'
onChange={(e) => handleChangeGraphQLFields(e, 'route')}
/>
</div>
</div>
</div>{' '}
{graphQL.assertions.map((assertion: Assertion, i: number) => {
return <GraphQLAssertion assertion={assertion} index={index} id={i} />;
})}{' '}
{graphQL.post && (
<div id={style.RenderContainer} style={{ margin: '10px 0 0 0' }}>
<label htmlFor='Header' id={styles.labelInputPost}>
Data To Send
</label>
<textarea
value={graphQL.postData}
onChange={(e) => updatePostData(e)}
style={{
display: 'block',
width: '90%',
overflow: 'scroll',
border: '1px solid rgb(205, 205, 205)',
margin: '10px auto',
height: '100px',
resize: 'none',
overflowX: 'hidden',
padding: '10px',
}}
placeholder={'Insert query string here... { }'}
/>
</div>
)}
{graphQL.headers.length > 0 && (
<div id={styles[`addheadermodal${theme}`]} style={{ margin: '10px 0 0 0' }}>
<div className={'props'}>
<div>
<div id={style.renderProp} style={{ width: '56.5%', paddingBottom: '3px' }}>
<label htmlFor='Header' id={style.headerLabel}>
Header
</label>
<label htmlFor='Value' id={style.headerLabel}>
Value
</label>
</div>
<hr />
{graphQL.headers.map((header: Header, i) => {
return (
<div id={styled.renderPropsFlexBox}>
<input
type='text'
id={i.toString()}
onChange={(e) => handleChangeGraphQLFields(e, 'headerName')}
value={header.headerName}
/>
<input
type='text'
id={i.toString()}
onChange={(e) => handleChangeGraphQLFields(e, 'headerValue')}
value={header.headerValue}
/>
<img
src={minusIcon}
alt='delete'
onClick={() => handleClickDeleteHeader(i)}
/>
</div>
);
})}
</div>
</div>
</div>
)}{' '}
<div className={styles.buttonsContainer}>
<button
// id={id}
onClick={addAssertionHandleClick}
id={styles.assertionButton}
>
<i className='fas fa-plus'></i>
Assertion
</button>
</div>
</div>
</div>
);
};
export default GraphQL;