Advertisement
Guest User

Untitled

a guest
Nov 25th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. let currentUser = new User();
  2.  
  3. class ClickButton extends Component {
  4. constructor(props) {
  5. super(props);
  6. this.state = {class: "btn", redirect:true};
  7.  
  8. this.press = this.press.bind(this);
  9. }
  10.  
  11. renderRedirect = () => {
  12. this.setState({redirect: true});
  13. }
  14.  
  15. press() {
  16. let className = (this.state.class==="btn")?"btn-on":"btn";
  17. this.setState({class: className});
  18. let my_init = {
  19. method: 'POST',
  20. mode: 'cors',
  21. credentials: 'include',
  22. headers: {
  23. "Access-Control-Allow-Credentials" : "true",
  24. "Content-Type" : "application/json"},
  25. body:JSON.stringify({ username: currentUser.getLogin(),email: currentUser.getEmail(), password: currentUser.getPassword() })
  26. }
  27. fetch('http://localhost:5000/api/users', my_init)
  28. .then((response) =>
  29. {
  30. currentUser.setInfo(JSON.stringify(response));
  31. window.localStorage.setItem("login", currentUser.getLogin());
  32. this.renderRedirect();
  33. })
  34. .catch(() => {
  35. alert("Request failed.");
  36. });
  37.  
  38. }
  39.  
  40. render() {
  41. return !this.state.redirect ? <button onClick={this.press} className={this.state.class}>Войти</button> :
  42. <Redirect to={"/"+currentUser.getLogin()}/>;
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement