-
Notifications
You must be signed in to change notification settings - Fork 92
/
App.jsx
84 lines (73 loc) · 2.75 KB
/
App.jsx
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
import React from 'react';
import ReactDOM from 'react-dom';
import {Redirect} from "@shopify/app-bridge/actions";
import {authenticatedFetch} from "@shopify/app-bridge-utils"
import {ApolloClient, HttpLink, InMemoryCache} from '@apollo/client';
import {ApolloProvider} from '@apollo/client/react';
import {AppProvider} from "@shopify/polaris";
import translations from "@shopify/polaris/locales/en.json";
import '@shopify/polaris/dist/styles.css';
import PageLayout from "./components/PageLayout";
import ProductsPage from "./components/ProductsPage";
import {Provider, useAppBridge} from '@shopify/app-bridge-react';
import {BrowserRouter, Route, Switch} from "react-router-dom";
import ClientRouter from "./components/ClientRouter";
import AppNavigation from "./components/AppNavigation";
function userLoggedInFetch(app) {
const fetchFunction = authenticatedFetch(app);
return async (uri, options) => {
const response = await fetchFunction(uri, options);
if (response.headers.get("X-Shopify-API-Request-Failure-Reauthorize") === "1") {
const authUrlHeader = response.headers.get("X-Shopify-API-Request-Failure-Reauthorize-Url");
const redirect = Redirect.create(app);
redirect.dispatch(Redirect.Action.APP, authUrlHeader);
return null;
}
return response;
};
}
function AppBridgeApolloProvider({children}) {
const app = useAppBridge();
const client = new ApolloClient({
link: new HttpLink({
credentials: 'same-origin',
fetch: userLoggedInFetch(app),
uri: '/graphql'
}),
cache: new InMemoryCache()
});
return (
<ApolloProvider client={client}>
{children}
</ApolloProvider>
);
}
function ExamplePage() {
return <div>Example Page</div>
}
function App({shop, host, apiKey}) {
const config = {apiKey: apiKey, shopOrigin: shop, host: host, forceRedirect: true};
return (
<BrowserRouter>
<Provider config={config}>
<ClientRouter/>
<AppProvider i18n={translations}>
<AppBridgeApolloProvider>
<AppNavigation/>
<PageLayout>
<Switch>
<Route path="/example" component={ExamplePage}/>
<Route path="/" component={ProductsPage}/>
</Switch>
</PageLayout>
</AppBridgeApolloProvider>
</AppProvider>
</Provider>
</BrowserRouter>
);
}
export default App;
let appElement = document.getElementById('app');
if (appElement) {
ReactDOM.render(<App {...(appElement.dataset)}/>, appElement);
}