Advertisement
Guest User

Untitled

a guest
Mar 5th, 2017
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {PropTypes, Component} from "react";
  2. import {bindActionCreators} from "redux";
  3. import {connect} from "react-redux";
  4. import PageComponent from "./PageComponent";
  5. import * as pageActions from "../actions/PageActions";
  6. import RegForm from "../forms/RegForm";
  7. import axios from "axios";
  8. import cookie from "react-cookie";
  9.  
  10. class About extends Component {
  11.  
  12.     // componentDidMount() {
  13.     //
  14.     //     var instance = axios.create({
  15.     //         baseURL: 'http://localhost:9000',
  16.     //         headers: {
  17.     //             'Authorization': 'Bearer ' + cookie.load("token"),
  18.     //             'Content-Type': 'application/json'
  19.     //         }
  20.     //     });
  21.     //     instance.get('/all').then(response => {
  22.     //         console.log(response.data);
  23.     //     })
  24.     //         .catch(function (error) {
  25.     //             console.log(error);
  26.     //         });
  27.     // }
  28.  
  29.     handleSubmit = (values) => {
  30.         console.log(values);
  31.  
  32.         var instance = axios.create({
  33.             baseURL: 'http://localhost:9000',
  34.             auth: {
  35.                 username: 'job4j',
  36.                 password: 'password'
  37.             }
  38.         });
  39.  
  40.         var username = values.email;
  41.         var password = values.password;
  42.  
  43.         const data = new FormData();
  44.         data.append('grant_type', 'password');
  45.         data.append('username', username);
  46.         data.append('password', password);
  47.  
  48.         instance.post('/oauth/token', data).then(response => {
  49.             console.log(response.data.access_token);
  50.             cookie.save('token', response.data.access_token, {path: '/'});
  51.  
  52.             const {setAuth} = this.props.actions;
  53.             setAuth(true);
  54.  
  55.         }).catch(error => {
  56.             console.log(error);
  57.         });
  58.     };
  59.  
  60.     render() {
  61.         const {page, user} = this.props;
  62.         const {setName} = this.props.actions;
  63.  
  64.         return <div>
  65.             <PageComponent name={page.name} setName={setName}/>
  66.             <br/>
  67.             { user.isAuth ? null : <RegForm onSubmit={this.handleSubmit}/> }
  68.         </div>
  69.     }
  70. }
  71.  
  72. function mapStateToProps(state) {
  73.     return {
  74.         page: state.page,
  75.         user: state.user
  76.     }
  77. }
  78.  
  79. function mapDispatchToProps(dispatch) {
  80.     return {
  81.         actions: bindActionCreators(pageActions, dispatch),
  82.     }
  83. }
  84.  
  85. export default connect(mapStateToProps, mapDispatchToProps)(About)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement