import React, { Component } from 'react'; import { connect } from 'react-redux'; import { userLogin } from '../actions'; class AuthMenu extends Component { constructor (props) { super(props) this.state = { user_authed: false, where_in_auth_menu: "requestUsername", username: "", password: "", email: "" } this.handleInputChange = this.handleInputChange.bind(this) this.handleEmailRequestForAccountCreation = this.handleEmailRequestForAccountCreation.bind(this) this.handlePasswordForLogin = this.handlePasswordForLogin.bind(this) this.handlePasswordForAccountCreation = this.handlePasswordForAccountCreation.bind(this) this.handleLogin = this.handleLogin.bind(this) this.handleCreateAccount = this.handleCreateAccount.bind(this) this.authMenu = this.authMenu.bind(this) } handleInputChange(event) { const target = event.target; const value = target.value; const name = target.name; this.setState({ [name]: value }); } handleEmailRequestForAccountCreation() { this.setState(state => ({ where_in_auth_menu: "requestEmail", auth_menu_visible: true, })); } handlePasswordForLogin() { this.setState(state => ({ where_in_auth_menu: "requestPasswordForLogin", auth_menu_visible: true, })); } handlePasswordForAccountCreation() { this.setState(state => ({ where_in_auth_menu: "requestPasswordForCreation", auth_menu_visible: true, })); } handleLogin() { this.props.userLogin(this.state.username, this.state.password) this.setState(state => ({ auth_menu_visible: false })); } handleCreateAccount() { this.setState(state => ({ where_in_auth_menu: "requestPasswordForCreation", auth_menu_visible: true, })); } authMenu() { if (!this.props.auth_menu_visible) { return null; } const { username, password, email } = this.state; switch(this.state.where_in_auth_menu) { case 'requestUsername': return (
) case 'requestPasswordForLogin': return (
) case 'requestEmail': return (
) case 'requestPasswordForCreation': return (
) default: return null; } } render() { return ( this.authMenu(this.state.auth_menu_visible) ) } } const mapStateToProps = (state) => { return { username: state.username, }; } export default connect( mapStateToProps, { userLogin } )(AuthMenu);