-
Notifications
You must be signed in to change notification settings - Fork 736
/
Copy pathEndpointPopup.tsx
214 lines (189 loc) · 4.57 KB
/
EndpointPopup.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
import * as React from 'react'
import * as fetch from 'isomorphic-fetch'
import Popup from './Popup'
import { throttle } from 'lodash'
import { Button } from './Button'
import { styled, css } from '../styled'
import { createClient } from 'graphql-ws';
// @ts-ignore
import imageSource from '../assets/logo.png'
export interface Props {
onRequestClose: (endpoint: string) => void
endpoint: string
}
export interface State {
endpoint: string
valid?: boolean
}
export default class EndpointPopup extends React.Component<Props, State> {
checkEndpoint = throttle(() => {
if (this.state.endpoint.match(/^(https?|wss?):\/\/\w+(\.\w+)*(:[0-9]+)?\/?.*$/)) {
if (this.state.endpoint.match(/^(wss?)/)) {
const client = createClient({
url: this.state.endpoint,
retryAttempts:0
});
const unsubscribe = client.subscribe(
{
query: `{
__schema {
queryType {
kind
}
}
}`,
},
{
next: () => {
this.setState({ valid: true })
unsubscribe()
},
error: () => {
this.setState({ valid: false });
unsubscribe()
},
complete: () => {},
},
);
return;
}
fetch(this.state.endpoint, {
method: 'post',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: `{
__schema {
queryType {
kind
}
}
}`,
}),
})
.then(res => {
this.setState({ valid: res.status < 400 })
})
.catch(err => {
this.setState({ valid: false })
})
}
}, 500) as any
constructor(props) {
super(props)
this.state = {
endpoint: props.endpoint,
}
}
componentDidMount() {
this.checkEndpoint()
}
render() {
const { valid } = this.state
return (
<Popup onRequestClose={this.close} darkBg={true}>
<Wrapper>
<LogoWrapper>
<Logo>
<img src={imageSource} alt="" />
<Heading>GraphQL Playground</Heading>
</Logo>
</LogoWrapper>
<Form action="" onSubmit={this.submit}>
<Input
type="text"
placeholder="Enter an endpoint url..."
value={this.state.endpoint}
onChange={this.onChangeEndpoint}
valid={typeof valid === 'boolean' && valid}
invalid={typeof valid === 'boolean' && !valid}
autoFocus={true}
/>
{valid && (
<Button purple={true} onClick={this.close}>
Use Endpoint
</Button>
)}
</Form>
</Wrapper>
</Popup>
)
}
private onChangeEndpoint = e => {
this.setState({ endpoint: e.target.value }, this.checkEndpoint)
}
private submit = e => {
e.preventDefault()
this.close()
}
private close = () => {
if (this.state.valid) {
this.props.onRequestClose(this.state.endpoint)
}
}
}
const Wrapper = styled.div`
box-sizing: border-box;
`
const Form = styled.form`
width: 100%;
display: flex;
flex: 1 1 auto;
.button.button {
padding-right: ${p => p.theme.sizes.small16};
padding-left: ${p => p.theme.sizes.small16};
background: #da1b7f;
&:hover {
background: ${p => p.theme.colours.purple};
}
}
`
interface InputProps {
valid: boolean
invalid: boolean
}
// prettier-ignore
const Input = styled<InputProps, 'input'>('input')`
background: ${p => p.theme.colours.white10};
border-radius: ${p => p.theme.sizes.smallRadius};
padding: ${p => `${p.theme.sizes.small16} ${p.theme.sizes.medium25}`};
font-weight: ${p => p.theme.sizes.fontSemiBold};
color: white;
font-size: 16px;
display: block;
width: 100%;
text-align: center;
flex: 1 1 auto;
display: flex;
transition: 250ms color;
${(p: any) =>
p.valid ? css`
color: ${k => k.theme.colours.green};
`
: p.invalid ? css`
color: ${k => k.theme.colours.red};
`
: ``
}
`
const Heading = styled.h1`
margin-left: 38px;
font-weight: 400;
color: ${p => p.theme.colours.white80};
`
const LogoWrapper = styled.div`
display: flex;
justify-content: center;
align-items: center;
`
const Logo = styled.div`
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 60px;
img {
width: 78px;
height: 78px;
}
`