App.tsx 2.22 KB
Newer Older
markchkhotua's avatar
markchkhotua committed
1
import * as React from 'react';
markchkhotua's avatar
markchkhotua committed
2
3
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
Valeriy Panov's avatar
Valeriy Panov committed
4
import {log} from 'utilities/debug';
markchkhotua's avatar
markchkhotua committed
5

Matija Obreza's avatar
Matija Obreza committed
6
import {logoutRequest, loginAppRequest} from 'actions/login';
7
import {setAppMounted} from 'actions/appMounted';
Maxym Borodenko's avatar
Maxym Borodenko committed
8
import {updateHistory} from 'actions/history';
Maxym Borodenko's avatar
Maxym Borodenko committed
9
import { listCrops } from 'actions/crop';
Matija Obreza's avatar
Matija Obreza committed
10

Maxym Borodenko's avatar
Maxym Borodenko committed
11
12
import { withRouter } from 'react-router-dom';

13
14
15
16
17
// FIXME When we start supporting other languages
import TimeAgo from 'javascript-time-ago';
import * as en from 'javascript-time-ago/locale/en';
TimeAgo.locale(en);

Matija Obreza's avatar
Matija Obreza committed
18
19
import Header from './layout/Header';
import Footer from './layout/Footer';
Matija Obreza's avatar
Matija Obreza committed
20
import Snackbar from 'ui/common/snackbar/Snackbar';
Maxym Borodenko's avatar
Maxym Borodenko committed
21
import renderRoutes from 'ui/renderRoutes';
Valeriy Panov's avatar
Valeriy Panov committed
22

markchkhotua's avatar
markchkhotua committed
23
interface IAppProps extends React.ClassAttributes<any> {
24
25
    children: any;
    login: any;
Maxym Borodenko's avatar
Maxym Borodenko committed
26
    route: any;
markchkhotua's avatar
markchkhotua committed
27
28
    location: any;
    logoutRequest: () => Promise<any>;
29
    loginAppRequest: () => Promise<any>;
30
    setAppMounted: () => void;
Maxym Borodenko's avatar
Maxym Borodenko committed
31
    updateHistory: (path: string) => void;
markchkhotua's avatar
markchkhotua committed
32
33
}

markchkhotua's avatar
markchkhotua committed
34
35
class App extends React.Component<IAppProps, any> {

Maxym Borodenko's avatar
Maxym Borodenko committed
36
37
38
    public static needs = [
        () => listCrops(),
    ];
Matija Obreza's avatar
Matija Obreza committed
39

Matija Obreza's avatar
Matija Obreza committed
40
41
42
    public constructor(props: any) {
        super(props);
     }
markchkhotua's avatar
markchkhotua committed
43

Maxym Borodenko's avatar
Maxym Borodenko committed
44
    public componentWillReceiveProps(nextProps) {
Maxym Borodenko's avatar
Maxym Borodenko committed
45
46
47
48
        const {updateHistory} = this.props;
        if (this.props.location !== null && nextProps.location !== null) {
            if (this.props.location !== nextProps.location) {
                updateHistory(this.props.location.pathname);
Maxym Borodenko's avatar
Maxym Borodenko committed
49
50
51
52
            }
        }
    }

markchkhotua's avatar
markchkhotua committed
53
    public render() {
Matija Obreza's avatar
Matija Obreza committed
54
        const { login, logoutRequest, loginAppRequest, route: { routes } } = this.props;
Valeriy Panov's avatar
Valeriy Panov committed
55
        log('Rendering App view');
markchkhotua's avatar
markchkhotua committed
56
        return (
Matija Obreza's avatar
Matija Obreza committed
57
            <div>
Maxym Borodenko's avatar
Maxym Borodenko committed
58
                <Header login={ login } logoutRequest={ logoutRequest } loginAppRequest={ loginAppRequest } />
Maxym Borodenko's avatar
Maxym Borodenko committed
59
                { renderRoutes(routes) }
Matija Obreza's avatar
Matija Obreza committed
60
                <Footer />
Matija Obreza's avatar
Matija Obreza committed
61
                <Snackbar />
Matija Obreza's avatar
Matija Obreza committed
62
            </div>
markchkhotua's avatar
markchkhotua committed
63
64
65
66
        );
    }
}

67
const mapStateToProps = (state) => ({
Matija Obreza's avatar
Matija Obreza committed
68
  // accessToken: state.login.access_token,
69
70
});

markchkhotua's avatar
markchkhotua committed
71
const mapDispatchToProps = (dispatch) => bindActionCreators({
Matija Obreza's avatar
Matija Obreza committed
72
73
74
  logoutRequest,
  loginAppRequest,
  setAppMounted,
Maxym Borodenko's avatar
Maxym Borodenko committed
75
  updateHistory,
markchkhotua's avatar
markchkhotua committed
76
77
}, dispatch);

Maxym Borodenko's avatar
Maxym Borodenko committed
78
export default withRouter(connect(mapStateToProps, mapDispatchToProps)(App));