-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
59296d5
commit 3662456
Showing
11 changed files
with
209 additions
and
14 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
packages/graphiql-2-rfc-context/src/api/actions/browserActions.ts
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,18 @@ | ||
export enum BrowserActionTypes { | ||
QueryStringParamsChanged = 'QueryStringParamsChanged', | ||
} | ||
|
||
export type BrowserAction = QueryStringParamsChangedAction; | ||
|
||
export const queryStringParamsChangedAction = ( | ||
parameter: string, | ||
value: string, | ||
) => | ||
({ | ||
type: BrowserActionTypes.QueryStringParamsChanged, | ||
payload: { parameter, value }, | ||
} as const); | ||
|
||
export type QueryStringParamsChangedAction = ReturnType< | ||
typeof queryStringParamsChangedAction | ||
>; |
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
6 changes: 6 additions & 0 deletions
6
packages/graphiql-2-rfc-context/src/api/hooks/useQueryParams.ts
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,6 @@ | ||
import { useBrowserContext } from '../providers/GraphiQLBrowserProvider'; | ||
|
||
export default function useQueryParams(name: string) { | ||
const { queryStringParams } = useBrowserContext(); | ||
return queryStringParams[name]; | ||
} |
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
93 changes: 93 additions & 0 deletions
93
packages/graphiql-2-rfc-context/src/api/providers/GraphiQLBrowserProvider.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,93 @@ | ||
import * as React from 'react'; | ||
import { parseQueryStringURL, updateQueryStringURL } from 'graphiql-toolkit'; | ||
import { BrowserState } from '../types'; | ||
|
||
import { | ||
BrowserAction, | ||
BrowserActionTypes, | ||
queryStringParamsChangedAction, | ||
} from '../actions/browserActions'; | ||
|
||
export type BrowserReducer = React.Reducer<BrowserState, BrowserAction>; | ||
export interface BrowserHandlers { | ||
changeQueryStringParams: (parameter: string, value: string) => void; | ||
dispatch: React.Dispatch<BrowserAction>; | ||
} | ||
|
||
export const initialBrowserState: BrowserState = { | ||
queryStringParams: { | ||
operation: | ||
parseQueryStringURL(window.location.search).operation || | ||
parseQueryStringURL(window.location.search).query, | ||
variables: parseQueryStringURL(window.location.search).variables, | ||
operationName: parseQueryStringURL(window.location.search).operationName, | ||
}, | ||
queryStringParamsURL: '', | ||
}; | ||
|
||
export const initialBrowserContextState: BrowserState & BrowserHandlers = { | ||
changeQueryStringParams: () => null, | ||
dispatch: () => null, | ||
...initialBrowserState, | ||
}; | ||
|
||
export const BrowserContext = React.createContext< | ||
BrowserState & BrowserHandlers | ||
>(initialBrowserContextState); | ||
|
||
export const useBrowserContext = () => React.useContext(BrowserContext); | ||
|
||
const browserReducer: BrowserReducer = (state, action) => { | ||
switch (action.type) { | ||
case BrowserActionTypes.QueryStringParamsChanged: { | ||
const { parameter, value } = action.payload; | ||
return { | ||
...state, | ||
queryStringParams: { | ||
...state.queryStringParams, | ||
[parameter]: value, | ||
}, | ||
queryStringParamsURL: updateQueryStringURL( | ||
state.queryStringParams, | ||
parameter, | ||
value, | ||
), | ||
}; | ||
} | ||
default: { | ||
return state; | ||
} | ||
} | ||
}; | ||
|
||
export type BrowserProviderProps = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
export function BrowserProvider({ children }: BrowserProviderProps) { | ||
const [state, dispatch] = React.useReducer<BrowserReducer>( | ||
browserReducer, | ||
initialBrowserState, | ||
); | ||
|
||
const changeQueryStringParams = React.useCallback( | ||
(parameter: string, value: string) => | ||
dispatch(queryStringParamsChangedAction(parameter, value)), | ||
[dispatch], | ||
); | ||
|
||
React.useEffect(() => { | ||
history.replaceState(null, document.title, state.queryStringParamsURL); | ||
}, [state.queryStringParamsURL]); | ||
|
||
return ( | ||
<BrowserContext.Provider | ||
value={{ | ||
...state, | ||
changeQueryStringParams, | ||
dispatch, | ||
}}> | ||
{children} | ||
</BrowserContext.Provider> | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,56 @@ | ||
/** | ||
* Copyright (c) 2020 GraphQL Contributors. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
export * from './types'; | ||
|
||
// TODO: move the most useful utilities from graphiql to here | ||
|
||
export function parseQueryStringURL(searchURL: string) { | ||
const parameters: any = {}; | ||
searchURL | ||
.substr(1) | ||
.split('&') | ||
.forEach(function (entry) { | ||
const eq = entry.indexOf('='); | ||
if (eq >= 0) { | ||
parameters[decodeURIComponent(entry.slice(0, eq))] = decodeURIComponent( | ||
entry.slice(eq + 1), | ||
); | ||
} | ||
}); | ||
return parameters; | ||
} | ||
|
||
export function updateQueryStringURL( | ||
parameters: any, | ||
parameter: string, | ||
value: string, | ||
) { | ||
parameters[parameter] = value; | ||
let newSearch: string = ''; | ||
if ( | ||
parameters.operationName !== undefined || | ||
parameters.operation !== undefined || | ||
parameters.variables !== undefined | ||
) { | ||
newSearch = | ||
'?' + | ||
Object.keys(parameters) | ||
.filter(function (key) { | ||
return Boolean(parameters[key]); | ||
}) | ||
.map(function (key) { | ||
return ( | ||
encodeURIComponent(key) + | ||
'=' + | ||
encodeURIComponent(parameters?.[key]) | ||
); | ||
}) | ||
.join('&'); | ||
} | ||
return newSearch; | ||
} |