LoginPage.tsx 2.29 KB
Newer Older
1
import * as React from 'react';
2
3
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
4
5
import * as _ from 'lodash';

6
import { log } from 'utilities/debug';
Valeriy Panov's avatar
Valeriy Panov committed
7

8
import { loginRequest, checkTokenRequest } from 'actions/login';
9
10
11
12

import ContentHeader from 'ui/common/heading/ContentHeader';

import LoginForm from './c/LoginForm';
Matija Obreza's avatar
Matija Obreza committed
13
import Grid from '@material-ui/core/Grid';
Matija Obreza's avatar
Matija Obreza committed
14
import Card, { CardHeader, CardContent } from 'ui/common/Card';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
15
import PageLayout from 'ui/layout/PageLayout';
16
17
18
19
20

interface ILoginContainerProps extends React.ClassAttributes<any> {
    login: any;
    loginRequest: (u, p) => Promise<any>;
    checkTokenRequest: (t) => Promise<any>;
Maxym Borodenko's avatar
Maxym Borodenko committed
21
    history: any;
22
23
24
25
26
}

class LoginContainer extends React.Component<ILoginContainerProps, void> {

    private onLogin = (username, password): any => {
Maxym Borodenko's avatar
Maxym Borodenko committed
27
      const { loginRequest, checkTokenRequest, history } = this.props;
Valeriy Panov's avatar
Valeriy Panov committed
28
      log('Trying login', username);
29
30
      return loginRequest(username, password)
          .then(({access_token}) => {
Valeriy Panov's avatar
Valeriy Panov committed
31
            log('Access token', access_token);
32
33
34
            return checkTokenRequest(access_token);
          })
          .then((data) => {
Maxym Borodenko's avatar
Maxym Borodenko committed
35
              history.push('/dashboard');
36
37
38
39
40
41
42
43
44
45
46
              return false;
          }).catch((e) => {
              const data = _.get(e, 'response.data');
              if (data && data.error) {
                  return ({error: data.error, errorDescription: data.error_description});
              }
      });
    }

    public render() {
        return (
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
47
          <PageLayout>
48
            <ContentHeader title="Welcome to Genesys" subTitle="Log in to manage datasets" />
Matija Obreza's avatar
Matija Obreza committed
49
50
51
52
53
54
55
56
57
58
            <Grid container spacing={ 0 } justify="center" className="back-gray p-20">
              <Grid item xs={ 12 } md={ 5 } lg={ 4 } xl={ 3 }>
                <Card>
                  <CardHeader title="Login" />
                  <CardContent>
                    <LoginForm onTryLogin={ this.onLogin } />
                  </CardContent>
                </Card>
              </Grid>
            </Grid>
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
59
          </PageLayout>
60
61
62
63
64
65
66
67
68
69
70
71
72
73
        );
    }
}

const mapStateToProps = (state) => ({
    login: state.login,
});

const mapDispatchToProps = (dispatch) => bindActionCreators({
    loginRequest,
    checkTokenRequest,
}, dispatch);

export default connect(mapStateToProps, mapDispatchToProps)(LoginContainer);