Advertisement
Guest User

Untitled

a guest
Aug 17th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.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. import { BrowserRouter as Router, Link } from "react-router-dom";
  9. import { Route, Redirect } from "react-router";
  10. import { createBrowserHistory } from "history";
  11.  
  12. //fetch('http://localhost:52654/Service1.svc/Login', {
  13. // method: 'POST',
  14. // headers: {
  15. // 'Accept': 'application/json',
  16. // 'Content-Type': 'application/json',
  17. // },
  18. // body: JSON.stringify({
  19. // user: ' ' ,
  20. // password: ' ' ,
  21. // })
  22. //})
  23.  
  24. const API = "http://localhost:52654/Service1.svc/Login";
  25.  
  26. export default class Login extends Component {
  27. constructor(props) {
  28. super(props);
  29. // this.openHome = this.openHome.bind(this);
  30. //this.returnToLogin = this.returnToLogin.bind(this);
  31. this.state = {
  32. user: "",
  33. password: "",
  34. isLogged: false,
  35. redirect:''
  36. //activeComponent: <Home changeState={this.changeState.bind(this)} />
  37. };
  38. this.switch2home = this.switch2home.bind(this);
  39.  
  40. /* this.fetchuser = this.fetchuser.bind(this);*/
  41. //this.handleSubmit = this.handleSubmit.bind(this)
  42. //this.onChange = this.onChange.bind(this);
  43. //this.onClick = this.onClick.bind(this);
  44. }
  45.  
  46. /*--*/
  47. // changeState = () => {
  48. // this.setState({ activeComponent: <Home /> })
  49.  
  50. /* fetchuser = () => {
  51. axios.get('http://localhost:52654/Service1.svc/Login/')
  52. .then((response) => {
  53. this.setState({ user: response.data, password: response.data })
  54. .cath((error) => {
  55.  
  56. console.log(error);
  57. });
  58. }
  59.  
  60. }*/
  61.  
  62. validateForm() {
  63. return this.state.user.length > 0 && this.state.password.length > 0;
  64. }
  65.  
  66. handleChange = event => {
  67. this.setState({
  68. [event.target.id]: event.target.value
  69. });
  70. };
  71.  
  72. handleSubmit = event => {
  73. event.preventDefault();
  74. axios({
  75. method: "post",
  76. url: API,
  77. data: {
  78. user: this.state.user,
  79. password: this.state.password
  80. },
  81. withCredentials: false,
  82. responseType: "json",
  83. responseEncoding: "utf8"
  84. }).then(response => {
  85. this.props.history.push("/");
  86.  
  87. if (response.statusText == "OK") {
  88. console.log("RESPONSE STATUS OK");
  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. this.state.isLogged = true;
  96. {
  97. this.openHome;
  98. }
  99. } else {
  100. console.log("Return to login");
  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. openHome(response) {
  134. this.setState({
  135. user: response.data.user,
  136. password: response.data.password
  137. });
  138. // this.props.changeState();
  139. // this.transitionTo('/');
  140. this.switch2home();
  141.  
  142. }
  143. switch2home(){
  144. // console.log(this.refs.home);
  145. localStorage.setItem('logged','true');
  146. this.setState({redirect:<Redirect push to="/" />});
  147. }
  148. returnToLogin() {
  149. this.setState({
  150. isLogged: !this.state.isLogged
  151. });
  152. }
  153. componentWillMount = () => {
  154. localStorage.setItem('logged','false');
  155. }
  156.  
  157.  
  158. render() {
  159. /*if (this.state.Login === 'Login') {
  160. return < Route path="/" exact={true} component={Home} />
  161. }*/
  162.  
  163. /* if (this.props.history.push === true) {
  164. return<Redirect to="/" />
  165. }*/
  166. return (
  167. <div className="Login">
  168. <form onSubmit={this.handleSubmit.bind(this)}>
  169. <FormGroup controlId="user" bsSize="large">
  170. <ControlLabel>Username</ControlLabel>
  171. <FormControl
  172. autoFocus
  173. type="user"
  174. value={this.state.user}
  175. onChange={this.handleChange.bind(this)}
  176. />
  177. </FormGroup>
  178. <FormGroup controlId="password" bsSize="large">
  179. <ControlLabel>Password</ControlLabel>
  180. <FormControl
  181. value={this.state.password}
  182. onChange={this.handleChange.bind(this)}
  183. type="password"
  184. />
  185. </FormGroup>
  186. <Button
  187. block
  188. bsSize="large"
  189. disabled={!this.validateForm()}
  190. //onClick={this.handleClick.bind(this)}
  191. type="submit"
  192. >
  193. Login
  194. </Button>
  195. </form>
  196.  
  197. {this.state.redirect}
  198.  
  199. </div>
  200. );
  201. }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement