Advertisement
Guest User

Untitled

a guest
Aug 13th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.43 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. import { Route, Redirect } from 'react-router';
  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: true,
  34.  
  35. }
  36.  
  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. if (this.setState.user && this.setState.password) {
  86. if (response.statusText == "OK") {
  87. console.log('RESPONSE STATUS OK');
  88.  
  89. user: response.data.user
  90. password: response.data.password
  91.  
  92. console.log("Password" + this.state.password);
  93. console.log("User:" + this.state.user);
  94.  
  95.  
  96. }
  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. this.openHome(response)
  127.  
  128. console.log("Password: " + this.state.password)
  129. console.log("User" + this.state.user)
  130.  
  131. });
  132. }
  133.  
  134. openHome(res) {
  135. this.setState({
  136. user: res.data.user,
  137. password: res.data.password,
  138.  
  139. });
  140. { this.state.activeComponent }
  141. }
  142.  
  143.  
  144. returnToLogin() {
  145.  
  146. }
  147.  
  148. render() {
  149.  
  150. /*if (this.state.Login === 'Login') {
  151. return < Route path="/" exact={true} component={Home} />
  152. }*/
  153.  
  154.  
  155. if (this.state.redirectToHome === true) {
  156. return<Redirect to="/" />
  157. }
  158.  
  159. return (
  160.  
  161. <div className="Login">
  162. <form onSubmit={this.handleSubmit.bind(this)}>
  163. <FormGroup controlId="user" bsSize="large">
  164. <ControlLabel>Username</ControlLabel>
  165. <FormControl
  166. autoFocus
  167. type="user"
  168. value={this.state.user}
  169. onChange={this.handleChange.bind(this)}
  170. />
  171. </FormGroup>
  172. <FormGroup controlId="password" bsSize="large">
  173. <ControlLabel>Password</ControlLabel>
  174. <FormControl
  175. value={this.state.password}
  176. onChange={this.handleChange.bind(this)}
  177. type="password"
  178. />
  179. </FormGroup>
  180. <Button
  181. block
  182. bsSize="large"
  183. disabled={!this.validateForm()}
  184. //onClick={this.handleClick.bind(this)}
  185. type="submit"
  186. >
  187. Login
  188. </Button>
  189. </form>
  190. </div>
  191. );
  192. }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement