File size: 2,544 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// @flow
import * as React from 'react';
import compose from 'recompose/compose';
import { connect } from 'react-redux';
import { Route } from 'react-router-dom';
import getCurrentUserSettings, {
  type GetCurrentUserSettingsType,
} from 'shared/graphql/queries/user/getCurrentUserSettings';
import viewNetworkHandler from 'src/components/viewNetworkHandler';
import { withCurrentUser } from 'src/components/withCurrentUser';
import Head from 'src/components/head';
import { View } from './style';
import Overview from './components/overview';
import Header from 'src/components/settingsViews/header';
import type { ContextRouter } from 'react-router';
import { ErrorView, LoadingView } from 'src/views/viewHelpers';
import { ViewGrid } from 'src/components/layout';
import { setTitlebarProps } from 'src/actions/titlebar';

type Props = {
  data: {
    user: GetCurrentUserSettingsType,
  },
  isLoading: boolean,
  hasError: boolean,
  ...$Exact<ContextRouter>,
};

class UserSettings extends React.Component<Props> {
  componentDidMount() {
    const { dispatch } = this.props;
    return dispatch(
      setTitlebarProps({
        title: 'Settings',
      })
    );
  }

  render() {
    const {
      data: { user },
      match,
      isLoading,
      currentUser,
    } = this.props;

    if (isLoading) {
      return <LoadingView />;
    }

    // the user is logged in but somehow a user wasnt fetched from the server prompt a refresh to reauth the user
    if ((currentUser && !user) || (currentUser && user && !user.id)) {
      return <ErrorView />;
    }

    // user is viewing their own settings, validated on the server
    if (user && user.id && currentUser.id === user.id) {
      const subheading = {
        to: `/users/${user.username}`,
        label: `Return to profile`,
      };

      const avatar = {
        profilePhoto: user.profilePhoto,
        user,
      };

      return (
        <React.Fragment>
          <Head title={'My settings'} />
          <ViewGrid>
            <View data-cy="user-settings">
              <Header
                avatar={avatar}
                subheading={subheading}
                heading={'My Settings'}
              />

              <Route path={`${match.url}`}>
                {() => <Overview user={user} />}
              </Route>
            </View>
          </ViewGrid>
        </React.Fragment>
      );
    }

    return <ErrorView />;
  }
}

export default compose(
  getCurrentUserSettings,
  viewNetworkHandler,
  withCurrentUser,
  connect()
)(UserSettings);