Advertisement
Guest User

Untitled

a guest
Aug 11th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 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.  
  84. if (response.statusText == "OK") {
  85. console.log('RESPONSE STATUS OK');
  86. this.setState({
  87. user: response.data.user,
  88. password: response.data.password
  89. });
  90. console.log("Password" + this.state.password);
  91. console.log("User:" + this.state.user);
  92.  
  93. } else {
  94. console.log("Return to login");
  95. }
  96. });
  97.  
  98. };
  99.  
  100. handleClick() {
  101. //axios.post(API, {
  102. // user: this.state.user,
  103. // password: this.state.password
  104. //}).then(function (response) {
  105. // console.log(response);
  106. //}).catch(function (error) {
  107. // console.log(error.response);
  108. //});
  109.  
  110. axios({
  111. method: 'post',
  112. url: API,
  113. data: {
  114. user: this.state.user,
  115. password: this.state.password
  116. },
  117. withCredentials: false,
  118. responseType: 'json',
  119. responseEncoding: 'utf8'
  120. }).then(function (response) {
  121.  
  122.  
  123.  
  124. console.log(response.data); /* response.data*/
  125. });
  126. }
  127.  
  128. openHome(res) {
  129. this.setState({
  130. user: res.data.user,
  131. password: res.data.password
  132. });
  133.  
  134.  
  135. console.log("Password: " + this.state.password)
  136. console.log("User" +this.state.user)
  137. }
  138.  
  139.  
  140. returnToLogin() {
  141.  
  142. }
  143.  
  144. render() {
  145.  
  146. /*if (this.state.Login === 'Login') {
  147. return < Route path="/" exact={true} component={Home} />
  148. }*/
  149.  
  150.  
  151. return (
  152.  
  153. <div className="Login">
  154. <form onSubmit={this.handleSubmit.bind(this)}>
  155. <FormGroup controlId="user" bsSize="large">
  156. <ControlLabel>Username</ControlLabel>
  157. <FormControl
  158. autoFocus
  159. type="user"
  160. value={this.state.user}
  161. onChange={this.handleChange.bind(this)}
  162. />
  163. </FormGroup>
  164. <FormGroup controlId="password" bsSize="large">
  165. <ControlLabel>Password</ControlLabel>
  166. <FormControl
  167. value={this.state.password}
  168. onChange={this.handleChange.bind(this)}
  169. type="password"
  170. />
  171. </FormGroup>
  172. <Button
  173. block
  174. bsSize="large"
  175. disabled={!this.validateForm()}
  176. //onClick={this.handleClick.bind(this)}
  177. type="submit"
  178. >
  179. Login
  180. </Button>
  181. </form>
  182. </div>
  183. );
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement