-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build out a simple login form for the explorer so it can be deployed
- Loading branch information
Showing
5 changed files
with
479 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import React, {useEffect, useMemo, useState} from "react"; | ||
import {GraphiQL} from "graphiql"; | ||
import { createGraphiQLFetcher, Fetcher } from '@graphiql/toolkit'; | ||
import {css} from "@emotion/react"; | ||
import 'graphiql/graphiql.css'; | ||
import {Button, TextInput} from "@guardian/source-react-components"; | ||
|
||
const loginContainer = css` | ||
margin: auto; | ||
width: fit-content; | ||
height: fit-content; | ||
display: flex; | ||
flex-direction: column; | ||
`; | ||
|
||
const loginElement = css` | ||
align-self: center; | ||
` | ||
const inputStyle = css` | ||
max-width: 800px; | ||
width: 800px; | ||
`; | ||
|
||
const buttonContainer = css` | ||
display: flex; | ||
margin-top: 1em; | ||
width: 100%; | ||
flex-direction: row; | ||
justify-content: space-evenly; | ||
`; | ||
|
||
export const LoginForm:React.FC = () => { | ||
const defaultBaseUrl = localStorage.getItem("CapiGQLBase") ?? "https://"; | ||
const defaultApiKey = localStorage.getItem("CapiGQLKey") ?? ""; | ||
const [haveCachedLogin, setHaveCachedLogin] = useState(!!(localStorage.getItem("CapiGQLBase") && localStorage.getItem("CapiGQLKey"))); | ||
|
||
const [baseUrl, setBaseUrl] = useState(defaultBaseUrl); | ||
const [apiKey, setApiKey] = useState(defaultApiKey); | ||
const [readyToGo, setReadyToGo] = useState(false); | ||
const [urlIsValid, setUrlIsValid] = useState(false); | ||
const urlValidator = /^https?:\/\/[\w-:.]+$/; | ||
|
||
useEffect(() => { | ||
setUrlIsValid(urlValidator.test(baseUrl)); | ||
}, [baseUrl]); | ||
|
||
const clearCache = () => { | ||
localStorage.removeItem("CapiGQLBase"); | ||
localStorage.removeItem("CapiGQLKey"); | ||
setBaseUrl("https://"); | ||
setApiKey(""); | ||
setHaveCachedLogin(false); | ||
} | ||
|
||
const fetcher = useMemo<Fetcher|null>(()=>{ | ||
if(readyToGo) { | ||
localStorage.setItem("CapiGQLBase", baseUrl); | ||
localStorage.setItem("CapiGQLKey", apiKey); | ||
|
||
return createGraphiQLFetcher({url: `${baseUrl}/query`, headers: {'X-Api-Key': apiKey}}); | ||
} else { | ||
return null; | ||
} | ||
}, [readyToGo]); | ||
|
||
|
||
return fetcher && readyToGo ? | ||
<GraphiQL fetcher={fetcher} /> : | ||
<div css={loginContainer}> | ||
<div css={loginElement}> | ||
<h1>Please log in</h1> | ||
</div> | ||
<div css={loginElement}> | ||
<TextInput label="CAPI GraphQL base URL" | ||
css={inputStyle} | ||
value={baseUrl} | ||
error={urlIsValid ? undefined : "Please input only a base URL with no trailing slash, i.e. https://my.server.name"} | ||
onChange={(evt)=>setBaseUrl(evt.target.value)}/> | ||
</div> | ||
<div css={loginElement}> | ||
<TextInput label="API key" | ||
css={inputStyle} | ||
value={apiKey} | ||
type="password" | ||
onChange={(evt)=>setApiKey(evt.target.value)} | ||
/> | ||
</div> | ||
<div css={buttonContainer}> | ||
<div> | ||
<Button disabled={!urlIsValid || apiKey==""} | ||
onClick={()=>setReadyToGo(true)}> | ||
Connect | ||
</Button> | ||
</div> | ||
<div> | ||
<Button disabled={!haveCachedLogin} | ||
priority="secondary" | ||
onClick={clearCache}> | ||
Clear</Button> | ||
</div> | ||
</div> | ||
</div> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,13 @@ | ||
import { createGraphiQLFetcher } from '@graphiql/toolkit'; | ||
import { GraphiQL } from 'graphiql'; | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
|
||
import 'graphiql/graphiql.css'; | ||
|
||
const fetcher = createGraphiQLFetcher({ url: "/query", headers: {'X-Consumer-Username': ":internal"} }); | ||
import {LoginForm} from "./LoginForm"; | ||
|
||
const rootElem = document.createElement('div'); | ||
rootElem.setAttribute("style","height: 100vh"); | ||
|
||
document.body.append(rootElem) | ||
ReactDOM.render( | ||
<GraphiQL fetcher={fetcher} />, | ||
<LoginForm />, | ||
rootElem, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.