-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: GraphQL client settings: apiUrl, apiKey
- Loading branch information
1 parent
88626d3
commit 9b1fd12
Showing
15 changed files
with
170 additions
and
35 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
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,28 +1,78 @@ | ||
import React, { useState, createContext } from 'react' | ||
|
||
export const AppContext = createContext<AppState | any>({}) | ||
export interface IAppContext { | ||
appState: AppState | ||
setDefaultDid: (defaultDid: string) => void | ||
setApiKey: (apiKey: string) => void | ||
setApiUrl: (apiUrl: string) => void | ||
} | ||
|
||
export const AppContext = createContext<IAppContext>({ | ||
appState: { | ||
defaultDid: '', | ||
apiUrl: '', | ||
apiKey: '' | ||
}, | ||
setDefaultDid: (defaultDid: string) => {}, | ||
setApiKey: (apiKey: string) => {}, | ||
setApiUrl: (apiUrl: string) => {} | ||
}) | ||
|
||
interface AppState { | ||
defaultDid: string | ||
apiUrl: string | ||
apiKey: string | ||
} | ||
|
||
export const AppProvider = (props: any) => { | ||
const defaultDid = localStorage.getItem('defaultId') || '' | ||
const apiUrl = localStorage.getItem('apiUrl') || 'http://localhost:4000/' | ||
const apiKey = localStorage.getItem('apiKey') || 'hardcoded-example-token' | ||
|
||
const [appState, setGlobalState] = useState<AppState>({ | ||
defaultDid: defaultDid, | ||
defaultDid, | ||
apiKey, | ||
apiUrl | ||
}) | ||
|
||
const setDefaultDid = (did: string) => { | ||
localStorage.setItem('defaultId', did) | ||
const setDefaultDid = (defaultDid: string) => { | ||
localStorage.setItem('defaultId', defaultDid) | ||
|
||
const newState = { | ||
...appState, | ||
defaultDid, | ||
} | ||
|
||
setGlobalState(newState) | ||
} | ||
|
||
const setApiUrl = (apiUrl: string) => { | ||
localStorage.setItem('apiUrl', apiUrl) | ||
|
||
const newState = { | ||
...appState, | ||
defaultDid: did, | ||
apiUrl, | ||
} | ||
|
||
setGlobalState(newState) | ||
} | ||
|
||
return <AppContext.Provider value={[appState, setDefaultDid]}>{props.children}</AppContext.Provider> | ||
const setApiKey = (apiKey: string) => { | ||
localStorage.setItem('apiKey', apiKey) | ||
|
||
const newState = { | ||
...appState, | ||
apiKey, | ||
} | ||
|
||
setGlobalState(newState) | ||
} | ||
|
||
|
||
return <AppContext.Provider value={{ | ||
appState, | ||
setDefaultDid, | ||
setApiKey, | ||
setApiUrl | ||
}}>{props.children}</AppContext.Provider> | ||
} |
29 changes: 29 additions & 0 deletions
29
examples/react-graphql/client/src/context/GraphQLProvider.tsx
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,29 @@ | ||
import React, { useContext } from 'react' | ||
import { ApolloProvider } from 'react-apollo' | ||
import ApolloClient from 'apollo-boost' | ||
import { AppContext } from './AppProvider' | ||
|
||
|
||
export const GraphQLProvider = (props: any) => { | ||
|
||
const { appState } = useContext(AppContext) | ||
|
||
const client = new ApolloClient({ | ||
uri: appState.apiUrl, | ||
|
||
// Authorization is out of scope for this example, | ||
// but this is where you could add your auth logic | ||
request: operation => { | ||
const token = appState.apiKey | ||
operation.setContext({ | ||
headers: { | ||
Authorization: token ? `Bearer ${token}` : '', | ||
}, | ||
}) | ||
}, | ||
}) | ||
|
||
|
||
|
||
return <ApolloProvider client={client}>{props.children}</ApolloProvider> | ||
} |
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
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
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
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
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
65 changes: 65 additions & 0 deletions
65
examples/react-graphql/client/src/views/Settings/Settings.tsx
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,65 @@ | ||
import React, { useContext, useState } from 'react' | ||
import { Box, Heading, Input, Field, Button, Flex, Form } from 'rimble-ui' | ||
import Page from '../../layout/Page' | ||
import Avatar from '../../components/Avatar/Avatar' | ||
import * as Types from '../../types' | ||
import Panel from '../../components/Panel/Panel' | ||
import { AppContext } from '../../context/AppProvider' | ||
|
||
const Component = () => { | ||
const { appState, setApiKey, setApiUrl } = useContext(AppContext) | ||
const [ newApiKey, setNewApiKey ] = useState(appState.apiKey) | ||
const [ newApiUrl, setNewApiUrl ] = useState(appState.apiUrl) | ||
|
||
const handleSave = () => { | ||
setApiKey(newApiKey) | ||
setApiUrl(newApiUrl) | ||
} | ||
|
||
return ( | ||
<Page title={'Settings'}> | ||
|
||
<Box p={3}> | ||
<Panel heading={'API details'}> | ||
|
||
|
||
<Flex mx={-3} flexWrap={'wrap'}> | ||
<Box width={1} px={3}> | ||
|
||
<Field label="API URL"> | ||
<Input | ||
border={0} | ||
type="text" | ||
required | ||
backgroundColor={'#313131'} | ||
value={newApiUrl} | ||
onChange={(e: any) => setNewApiUrl(e.target.value)} | ||
/> | ||
</Field> | ||
</Box> | ||
</Flex> | ||
|
||
<Flex mx={-3} flexWrap={'wrap'}> | ||
<Box width={1} px={3}> | ||
|
||
<Field label="API Key"> | ||
<Input | ||
border={0} | ||
type="text" | ||
required | ||
backgroundColor={'#313131'} | ||
value={newApiKey} | ||
onChange={(e: any) => setNewApiKey(e.target.value)} | ||
/> | ||
</Field> | ||
</Box> | ||
</Flex> | ||
|
||
</Panel> | ||
<Button onClick={handleSave}>Save</Button> | ||
</Box> | ||
</Page> | ||
) | ||
} | ||
|
||
export default Component |