Advertisement
Guest User

Untitled

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