Advertisement
Guest User

Untitled

a guest
Aug 6th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import LoginForm from './components/login/LoginFormContainer';
  3. import Main from './components/main/MainContainer';
  4. import {
  5. BrowserRouter as Router,
  6. Switch,
  7. Route
  8. } from 'react-router-dom';
  9. import './App.css';
  10.  
  11. class App extends Component {
  12. render() {
  13. return (
  14. <Router>
  15. <Switch>
  16. <Route exact name="login" path="/" component={LoginForm} />
  17. <Route exact name="main" path="/main" component={Main} />
  18. </Switch>
  19. </Router>
  20. );
  21. }
  22. }
  23.  
  24. export default App;
  25.  
  26. import React, { Component } from 'react';
  27. import { Button, Form, Col, Row } from 'react-bootstrap';
  28. import FieldGroup from '../common/FormGroup';
  29. import { Redirect, Link } from 'react-router-dom';
  30. import Logo from '../../img/zonLogo.svg';
  31.  
  32. class LoginForm extends Component {
  33. constructor(props) {
  34. super(props);
  35. this.handleChange = this.handleChange.bind(this);
  36. this.handleLoginClick = this.handleLoginClick.bind(this);
  37. this.state = {
  38. isLoggedIn: false,
  39. username: "",
  40. password: ""
  41. };
  42. }
  43.  
  44. handleLoginClick(e) {
  45. var form = new FormData()
  46. form.append("username", "xxxxx");
  47. form.append("password", "xxxxx");
  48. let self = this;
  49.  
  50. fetch('/login', {
  51. method: 'POST',
  52. body: form
  53. }).then(function (response) {
  54. if (response.status === 200) {
  55. return response.json()
  56. }
  57. }).then(function (data) {
  58. i f (data.user !== undefined && !data.user.isSuspended && !data.user.banned) {
  59. self.setState({ isLoggedIn: true });
  60. }
  61. }).catch(function (error) {
  62. console.log("Call report API error");
  63. });
  64. }
  65.  
  66. render() {
  67.  
  68. if (this.state.isLoggedIn) {
  69. return <Redirect to="main" />;
  70. }
  71. return (
  72. <div >
  73. <Row>
  74. <Col>
  75. <Form onSubmit={this.handleLoginClick}>
  76. <FieldGroup
  77. id="username"
  78. type="text"
  79. placeholder="Username"/>
  80. <FieldGroup
  81. id="password"
  82. type="password"
  83. placeholder="Password"
  84. />
  85. <Button type="submit" block> Log in</Button>
  86. </Form>
  87. </Col>
  88. </Row>
  89. </div >
  90. );
  91. }
  92. }
  93.  
  94. export default LoginForm;
  95.  
  96. handleLoginClick(e) {
  97. self.setState({ isLoggedIn: true });
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement