App.tsx 2.15 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';
markchkhotua's avatar
markchkhotua committed
4

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

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

12
13
14
15
16
// 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
17
18
import Header from './layout/Header';
import Footer from './layout/Footer';
Matija Obreza's avatar
Matija Obreza committed
19
import Snackbar from 'ui/common/snackbar/Snackbar';
Maxym Borodenko's avatar
Maxym Borodenko committed
20
import renderRoutes from 'ui/renderRoutes';
Valeriy Panov's avatar
Valeriy Panov committed
21

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

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

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

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

Maxym Borodenko's avatar
Maxym Borodenko committed
43
    public componentWillReceiveProps(nextProps) {
Maxym Borodenko's avatar
Maxym Borodenko committed
44
45
46
47
        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
48
49
50
51
            }
        }
    }

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

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

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

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