Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let currentUser = new User();
- class ClickButton extends Component {
- constructor(props) {
- super(props);
- this.state = {class: "btn", redirect:true};
- this.press = this.press.bind(this);
- }
- renderRedirect = () => {
- this.setState({redirect: true});
- }
- press() {
- let className = (this.state.class==="btn")?"btn-on":"btn";
- this.setState({class: className});
- let my_init = {
- method: 'POST',
- mode: 'cors',
- credentials: 'include',
- headers: {
- "Access-Control-Allow-Credentials" : "true",
- "Content-Type" : "application/json"},
- body:JSON.stringify({ username: currentUser.getLogin(),email: currentUser.getEmail(), password: currentUser.getPassword() })
- }
- fetch('http://localhost:5000/api/users', my_init)
- .then((response) =>
- {
- currentUser.setInfo(JSON.stringify(response));
- window.localStorage.setItem("login", currentUser.getLogin());
- this.renderRedirect();
- })
- .catch(() => {
- alert("Request failed.");
- });
- }
- render() {
- return !this.state.redirect ? <button onClick={this.press} className={this.state.class}>Войти</button> :
- <Redirect to={"/"+currentUser.getLogin()}/>;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement