Advertisement
Guest User

Untitled

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