summaryrefslogtreecommitdiffstats
path: root/frontend/components/Header.js
blob: cc4cca73786ee794882ef6b10a6bd980f29753bc (plain)
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
import { Component } from 'react';
import { graphql, compose } from 'react-apollo';
import { CURRENT_USER_QUERY } from '../queries';
import Cart from './Cart';
import Login from './LoginAuth0';
import Search from './Search';
import NProgress from 'nprogress';
import Router from 'next/router';

Router.onRouteChangeStart = url => {
  console.log(`Loading: ${url}`);
  NProgress.start();
};
Router.onRouteChangeComplete = () => NProgress.done();
Router.onRouteChangeError = () => NProgress.done();

class Header extends Component {
  componentDidMount() {
    // This fetches the new data, but doesn't populate the user via props
    // this.props.currentUserQuery.refetch();
    // This fetches the new data, and populates the user via props
    setTimeout(this.props.currentUserQuery.refetch, 1);
  }

  render() {
    const user = this.props.currentUserQuery.user || {};
    const { email = '' } = user;
    return (
      <div>
        <p>
          Signed in as <strong>{email}</strong>
        </p>
        <Login />
        <Cart />
        <Search />
      </div>
    );
  }
}

const userEnhancer = graphql(CURRENT_USER_QUERY, { name: 'currentUserQuery' });
export default compose(userEnhancer)(Header);