Advertisement
Guest User

Untitled

a guest
Aug 13th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import { Button, FormGroup, FormControl, ControlLabel } from "react-bootstrap";
  3. import "./Login.css";
  4. //import { BrowserRouter as Router, Route } from "react-router-dom";
  5. //import { Link } from "react-router-dom";
  6. import axios from 'axios';
  7. //import Home from "./Home";
  8.  
  9.  
  10.  
  11.  
  12. const API = 'http://localhost:56641/Service1.svc/Login';
  13.  
  14. export default class Login extends Component {
  15. constructor(props) {
  16. super(props);
  17. // this.openHome = this.openHome.bind(this);
  18. //this.returnToLogin = this.returnToLogin.bind(this);
  19. this.state = {
  20. user: "",
  21. password: "",
  22. Login: false
  23. }
  24.  
  25. /* this.fetchuser = this.fetchuser.bind(this);*/
  26. //this.handleSubmit = this.handleSubmit.bind(this)
  27. //this.onChange = this.onChange.bind(this);
  28. //this.onClick = this.onClick.bind(this);
  29. }
  30.  
  31. /*--*/
  32.  
  33. /* fetchuser = () => {
  34. axios.get('http://localhost:52654/Service1.svc/Login/')
  35. .then((response) => {
  36. this.setState({ user: response.data, password: response.data })
  37. .cath((error) => {
  38.  
  39. console.log(error);
  40. });
  41. }
  42.  
  43. }*/
  44.  
  45.  
  46.  
  47.  
  48. validateForm() {
  49. return this.state.user.length > 0 && this.state.password.length > 0;
  50. }
  51.  
  52. handleChange = event => {
  53. this.setState({
  54. [event.target.id]: event.target.value
  55. });
  56.  
  57. };
  58.  
  59. handleSubmit = event => {
  60. event.preventDefault();
  61. axios({
  62. method: 'post',
  63. url: API,
  64. data: {
  65. user: this.state.user,
  66. password: this.state.password
  67. },
  68. withCredentials: false,
  69. responseType: 'json',
  70. responseEncoding: 'utf8'
  71. }).then(response => {
  72.  
  73.  
  74. if (response.statusText == "OK") {
  75. console.log('RESPONSE STATUS OK');
  76.  
  77. user: response.data.user
  78. password: response.data.password
  79.  
  80. console.log("Password" + this.state.password);
  81. console.log("User:" + this.state.user);
  82.  
  83. } else {
  84. console.log("Return to login");
  85. }
  86. });
  87.  
  88. };
  89.  
  90. handleClick() {
  91. //axios.post(API, {
  92. // user: this.state.user,
  93. // password: this.state.password
  94. //}).then(function (response) {
  95. // console.log(response);
  96. //}).catch(function (error) {
  97. // console.log(error.response);
  98. //});
  99.  
  100. axios({
  101. method: 'post',
  102. url: API,
  103. data: {
  104. user: this.state.user,
  105. password: this.state.password
  106. },
  107. withCredentials: false,
  108. responseType: 'json',
  109. responseEncoding: 'utf8'
  110. }).then(response => {
  111. this.openHome(response)
  112.  
  113. console.log("Password: " + this.state.password)
  114. console.log("User" + this.state.user)
  115.  
  116. });
  117. }
  118.  
  119. openHome(res) {
  120. this.setState({
  121. user: res.data.user,
  122. password: res.data.password
  123. });
  124.  
  125.  
  126.  
  127. }
  128.  
  129.  
  130. returnToLogin() {
  131.  
  132. }
  133.  
  134. render() {
  135.  
  136. /*if (this.state.Login === 'Login') {
  137. return < Route path="/" exact={true} component={Home} />
  138. }*/
  139.  
  140.  
  141. return (
  142.  
  143. <div className="Login">
  144. <form onSubmit={this.handleSubmit.bind(this)}>
  145. <FormGroup controlId="user" bsSize="large">
  146. <ControlLabel>Username</ControlLabel>
  147. <FormControl
  148. autoFocus
  149. type="user"
  150. value={this.state.user}
  151. onChange={this.handleChange.bind(this)}
  152. />
  153. </FormGroup>
  154. <FormGroup controlId="password" bsSize="large">
  155. <ControlLabel>Password</ControlLabel>
  156. <FormControl
  157. value={this.state.password}
  158. onChange={this.handleChange.bind(this)}
  159. type="password"
  160. />
  161. </FormGroup>
  162. <Button
  163. block
  164. bsSize="large"
  165. disabled={!this.validateForm()}
  166. //onClick={this.handleClick.bind(this)}
  167. type="submit"
  168. >
  169. Login
  170. </Button>
  171. </form>
  172. </div>
  173. );
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement