-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: groundwork for new elasticpath provider
- Loading branch information
Showing
4 changed files
with
100 additions
and
14 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,66 @@ | ||
import { gateway, Moltin as ElasticPath } from "@moltin/sdk" | ||
import { | ||
QueryClientProvider, | ||
QueryClientProviderProps, | ||
} from "@tanstack/react-query" | ||
import React, { ReactElement } from "react" | ||
|
||
interface ElasticPathContextState { | ||
client: ElasticPath | ||
} | ||
|
||
const ElasticPathContext = React.createContext<ElasticPathContextState | null>( | ||
null, | ||
) | ||
|
||
export const useElasticPath = () => { | ||
const context = React.useContext(ElasticPathContext) | ||
if (!context) { | ||
throw new Error("useElasticPath must be used within a ElasticPathProvider") | ||
} | ||
return context | ||
} | ||
|
||
export type ElasticPathProviderProps = { | ||
queryClientProviderProps: QueryClientProviderProps | ||
children: React.ReactNode | ||
} | ||
|
||
export type ElasticPathProviderPropsWithClient = ElasticPathProviderProps & { | ||
clientId: string | ||
host?: string | ||
} | ||
|
||
export type ElasticPathProviderPropsCustom = ElasticPathProviderProps & { | ||
client: ElasticPath | ||
} | ||
|
||
export function ElasticPathProvider( | ||
props: ElasticPathProviderPropsWithClient, | ||
): ReactElement | ||
export function ElasticPathProvider( | ||
props: ElasticPathProviderPropsCustom, | ||
): ReactElement | ||
export function ElasticPathProvider( | ||
props: ElasticPathProviderPropsWithClient | ElasticPathProviderPropsCustom, | ||
): ReactElement { | ||
const client: ElasticPath = | ||
"client" in props | ||
? props.client | ||
: gateway({ | ||
client_id: props.clientId, | ||
host: props.host, | ||
}) | ||
|
||
return ( | ||
<QueryClientProvider {...props.queryClientProviderProps}> | ||
<ElasticPathContext.Provider | ||
value={{ | ||
client, | ||
}} | ||
> | ||
{props.children} | ||
</ElasticPathContext.Provider> | ||
</QueryClientProvider> | ||
) | ||
} |
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 @@ | ||
export * from "./elasticpath" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.