Advertisement
Guest User

Untitled

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