|
1 |
| -import'../style.css' |
2 |
| - |
3 |
| -import { ThemeProvider, createGlobalStyle } from "styled-components"; |
4 |
| - |
5 |
| -import { ApolloClient } from "@apollo/client"; |
6 | 1 | import { ApolloProvider } from "@apollo/react-hooks";
|
7 |
| -import Head from "next/head"; |
8 |
| -import Header from '../components/Layouts/Header'; |
9 |
| -import { NormalizedCache } from "apollo-boost"; |
10 |
| -import { Provider } from 'react-redux' |
| 2 | +import { Provider } from "react-redux"; |
| 3 | +//import Home from '!/containers/home'; |
11 | 4 | import React from "react";
|
12 |
| -import { useApollo } from '../lib/apollo' |
13 |
| -import { useStore } from '../lib/redux' |
14 |
| -import withApollo from "../hooks/withApollo"; |
15 |
| -//import App from "next/app"; |
16 |
| -import withRedux from 'next-redux-wrapper'; |
17 |
| -import { wrapper } from '../store/store'; |
18 |
| - |
19 |
| -export interface IThem { |
20 |
| - niceBlack: string; |
21 |
| -} |
| 5 | +import { getDataFromTree } from "react-apollo-graphql"; |
| 6 | +import { initializeApollo } from "../lib/apollo"; |
| 7 | +import { initializeStore } from "../lib/redux"; |
22 | 8 |
|
23 |
| -export interface IThemeWrapper { |
24 |
| - theme: IThem; |
25 |
| -} |
| 9 | +//import { ApolloProvider } from '@apollo/client'; |
26 | 10 |
|
27 |
| -export const theme: IThem = { |
28 |
| - niceBlack: "#001F3F", |
29 |
| -}; |
| 11 | +/** |
| 12 | + * Component to show the home container. |
| 13 | + */ |
| 14 | +class App extends React.Component { |
| 15 | + static async getInitialProps({ Component, ctx }) { |
| 16 | + const pageProps = Component.getInitialProps |
| 17 | + ? await Component.getInitialProps(ctx) |
| 18 | + : {}; |
30 | 19 |
|
31 |
| -const GlobalStyle = createGlobalStyle<IThemeWrapper>` |
32 |
| -body { |
33 |
| - margin: 0 auto; |
34 |
| - color: ${(props) => props.theme.niceBlack} |
35 |
| -} |
| 20 | + //Anything returned here can be access by the client |
| 21 | + // return {pageProps: pageProps}; |
36 | 22 |
|
37 |
| -.overlay { |
38 |
| - position: fixed; |
39 |
| - display: block; |
40 |
| - overflow: auto; |
41 |
| - width: 100%; |
42 |
| - height: 100%; |
43 |
| - top: 0; |
44 |
| - left: 0; |
45 |
| - right: 0; |
46 |
| - bottom: 0; |
47 |
| - background-color: rgba(0,0,0,0.5); |
48 |
| - z-index: 999; |
49 |
| - cursor: pointer; |
50 |
| -} |
| 23 | + let serverState = {}; |
| 24 | + if (!process.browser) { |
| 25 | + const apollo = initializeApollo(); |
| 26 | + const redux = initializeStore(apollo); |
| 27 | + // Run all graphql queries |
| 28 | + const app = ( |
| 29 | + <Provider store={redux}> |
| 30 | + <ApolloProvider client={apollo}> |
| 31 | + <Component {...pageProps} /> |
| 32 | + </ApolloProvider> |
| 33 | + </Provider> |
| 34 | + ); |
| 35 | + await getDataFromTree(app); |
| 36 | + const state = redux.getState(); |
| 37 | + serverState = { |
| 38 | + apollo: { |
| 39 | + // Make sure to only include Apollo's data state |
| 40 | + data: state.apollo.data, |
| 41 | + }, |
| 42 | + }; |
| 43 | + } |
| 44 | + return { |
| 45 | + serverState, |
| 46 | + pageProps , |
| 47 | + }; |
| 48 | + } |
51 | 49 |
|
52 |
| -.content { |
53 |
| - margin: 15% auto; |
54 |
| - background-color: white; |
55 |
| - border-radius: 0.25rem; |
56 |
| - width: 50vw; |
57 |
| - padding: 2rem; |
58 |
| - position: relative; |
59 |
| -} |
| 50 | + // constructor(props) { |
| 51 | + // super(props); |
| 52 | + // this.apollo = initializeApollo(); |
| 53 | + // this.store = initializeStore(this.apollo, props.serverState); |
| 54 | + // } |
60 | 55 |
|
61 |
| -
|
62 |
| -`; |
63 |
| - |
64 |
| -interface IProps { |
65 |
| - apollo: ApolloClient<NormalizedCache>; |
66 |
| -} |
67 |
| - |
68 |
| - |
69 |
| -const App = ({ Component, pageProps }) => { |
70 |
| - //const store = useStore(pageProps.initialReduxState) |
71 |
| - const apolloClient = useApollo(pageProps.initialApolloState) |
72 |
| - // console.log(apolloClient); |
| 56 | + render() { |
| 57 | + const { Component, pageProps, store } = this.props; |
| 58 | + const apollo = initializeApollo(); |
| 59 | + const redux = initializeStore(apollo); |
73 | 60 | return (
|
74 |
| - <React.Fragment> |
75 |
| - <Head> |
76 |
| - <title>GraphQL</title> |
77 |
| - <meta name="viewport" content="width=device-width, initial-scale=1" /> |
78 |
| - </Head> |
79 |
| - {/* adds the apollo provider to provide it's children with the apollo scope. */} |
80 |
| - {/* <Provider store={store}> */} |
81 |
| - <ApolloProvider client={apolloClient}> |
82 |
| - <ThemeProvider theme={theme}> |
83 |
| - <GlobalStyle /> |
84 |
| - <Header></Header> |
85 |
| - <Component {...pageProps} /> |
86 |
| - </ThemeProvider> |
| 61 | + <Provider store={store}> |
| 62 | + <ApolloProvider client={apollo}> |
| 63 | + <Component {...pageProps} /> |
87 | 64 | </ApolloProvider>
|
88 |
| - {/* </Provider> */} |
89 |
| - </React.Fragment> |
| 65 | + </Provider> |
90 | 66 | );
|
91 |
| - |
| 67 | + } |
92 | 68 | }
|
93 |
| -//export default wrapper.withRedux(App) |
94 |
| -export default wrapper.withRedux(App); |
95 |
| -// before exporting our App we wrapp it with our own withApollo provider to have access to the our rehydrated apollo client. |
96 |
| -//export default withApollo(MyApp); |
| 69 | + |
| 70 | +export default App; |
0 commit comments