Advertisement
Guest User

Untitled

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