File size: 1,512 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// @flow
import * as React from 'react';
import { connect } from 'react-redux';
import compose from 'recompose/compose';
import { withRouter, type History, type Location } from 'react-router';
import { getCurrentUser } from 'shared/graphql/queries/user/getUser';
import type { GetUserType } from 'shared/graphql/queries/user/getUser';
import editUserMutation from 'shared/graphql/mutations/user/editUser';
import NewUserOnboarding from 'src/views/newUserOnboarding';

type Props = {
  history: History,
  location: Location,
  editUser: Function,
  children: (authed: boolean) => React$Element<*>,
  data: {
    user: GetUserType,
    loading: boolean,
  },
};

class AuthViewHandler extends React.Component<Props> {
  componentDidUpdate(prev: Props) {
    const {
      data: { user },
      editUser,
      history,
      location,
    } = this.props;

    if (!prev.data.user && user) {
      if (!user.timezone) {
        const timezone = new Date().getTimezoneOffset() * -1;
        try {
          editUser({ timezone });
        } catch (err) {}
      }

      if (location.pathname === '/home') history.replace('/');
    }
  }

  render() {
    const {
      children,
      data: { user, loading },
    } = this.props;

    if (user && !user.username) return <NewUserOnboarding />;
    if (user && user.id) return children(true);
    if (loading) return null;
    return children(false);
  }
}

export default compose(
  getCurrentUser,
  editUserMutation,
  withRouter,
  connect()
)(AuthViewHandler);