1
0
Fork 0
This repository has been archived on 2022-09-24. You can view files and clone it, but cannot push or open issues or pull requests.
sudoscientist-js-frontend/src/components/AuthMenu.js

173 lines
4.9 KiB
JavaScript
Raw Normal View History

2019-08-27 06:17:32 +00:00
import React, { Component } from 'react';
2019-09-16 02:02:15 +00:00
2019-09-15 00:29:55 +00:00
import { connect } from 'react-redux';
import { userLogin } from '../actions';
2019-08-27 06:17:32 +00:00
class AuthMenu extends Component {
constructor (props) {
super(props)
2019-08-28 01:01:42 +00:00
2019-08-27 06:17:32 +00:00
this.state = {
2019-09-15 00:29:55 +00:00
user_authed: false,
2019-08-28 01:42:16 +00:00
where_in_auth_menu: "requestUsername",
2019-08-28 01:49:25 +00:00
username: "",
password: "",
email: ""
2019-08-27 06:17:32 +00:00
}
2019-08-28 01:01:42 +00:00
2019-09-07 02:18:14 +00:00
this.handleInputChange = this.handleInputChange.bind(this)
2019-09-15 00:03:41 +00:00
this.handleEmailRequestForAccountCreation = this.handleEmailRequestForAccountCreation.bind(this)
2019-08-28 01:42:16 +00:00
this.handlePasswordForLogin = this.handlePasswordForLogin.bind(this)
2019-09-15 00:03:41 +00:00
this.handlePasswordForAccountCreation = this.handlePasswordForAccountCreation.bind(this)
this.handleLogin = this.handleLogin.bind(this)
this.handleCreateAccount = this.handleCreateAccount.bind(this)
2019-08-28 00:53:20 +00:00
this.authMenu = this.authMenu.bind(this)
2019-08-28 01:01:42 +00:00
2019-08-27 06:17:32 +00:00
}
2019-08-28 01:42:16 +00:00
2019-09-07 02:18:14 +00:00
handleInputChange(event) {
const target = event.target;
const value = target.value;
const name = target.name;
this.setState({
[name]: value
});
}
2019-09-15 00:03:41 +00:00
handleEmailRequestForAccountCreation() {
this.setState(state => ({
where_in_auth_menu: "requestEmail",
auth_menu_visible: true,
}));
}
2019-08-28 01:42:16 +00:00
handlePasswordForLogin() {
2019-08-27 06:17:32 +00:00
this.setState(state => ({
2019-08-28 01:42:16 +00:00
where_in_auth_menu: "requestPasswordForLogin",
2019-08-27 06:17:32 +00:00
auth_menu_visible: true,
}));
}
2019-09-15 00:03:41 +00:00
handlePasswordForAccountCreation() {
2019-08-27 06:17:32 +00:00
this.setState(state => ({
2019-09-15 00:03:41 +00:00
where_in_auth_menu: "requestPasswordForCreation",
2019-08-27 06:17:32 +00:00
auth_menu_visible: true,
}));
}
2019-08-28 01:42:16 +00:00
2019-09-15 00:03:41 +00:00
handleLogin() {
2019-09-15 00:29:55 +00:00
this.props.userLogin(this.state.username, this.state.password)
2019-09-16 00:35:05 +00:00
this.setState(state => ({
2019-09-16 02:02:15 +00:00
auth_menu_visible: false,
2019-09-16 00:35:05 +00:00
}));
2019-08-27 06:17:32 +00:00
}
2019-09-15 00:03:41 +00:00
handleCreateAccount() {
this.setState(state => ({
where_in_auth_menu: "requestPasswordForCreation",
auth_menu_visible: true,
}));
}
2019-08-28 01:42:16 +00:00
2019-08-28 00:53:20 +00:00
authMenu() {
2019-08-28 01:17:11 +00:00
if (!this.props.auth_menu_visible) {
2019-08-28 00:53:20 +00:00
return null;
}
2019-09-15 00:03:41 +00:00
const { username, password, email } = this.state;
2019-08-28 00:53:20 +00:00
switch(this.state.where_in_auth_menu) {
2019-08-28 01:42:16 +00:00
case 'requestUsername':
2019-08-28 00:53:20 +00:00
return (
<div className="ui menu dropdown" style={{display: "inline"}}>
<div className="ui left icon input">
2019-09-07 02:18:14 +00:00
<input
type="text"
placeholder="Username"
name="username"
2019-09-15 00:03:41 +00:00
value={username}
2019-09-07 02:18:14 +00:00
onChange={this.handleInputChange}
2019-09-07 02:38:28 +00:00
></input>
2019-08-28 00:53:20 +00:00
<i className="users icon"></i>
</div>
<div className="fluid ui buttons">
2019-09-15 00:03:41 +00:00
<button onClick={this.handleEmailRequestForAccountCreation} className="blue ui button">Sign Up</button>
2019-08-28 00:53:20 +00:00
<div className="or"></div>
2019-08-28 01:42:16 +00:00
<button onClick={this.handlePasswordForLogin} className="ui teal button">Sign In</button>
</div>
</div>
)
case 'requestPasswordForLogin':
return (
<div className="ui menu dropdown" style={{display: "inline"}}>
<div className="ui left icon input">
2019-09-07 02:18:14 +00:00
<input
2019-09-15 00:03:41 +00:00
type={"password"}
2019-09-07 02:18:14 +00:00
placeholder="Password"
2019-09-07 02:38:28 +00:00
name="password"
2019-09-15 00:03:41 +00:00
value={password}
2019-09-07 02:18:14 +00:00
onChange={this.handleInputChange}
2019-09-07 02:38:28 +00:00
></input>
2019-08-28 01:42:16 +00:00
<i className="lock icon"></i>
</div>
2019-09-15 00:03:41 +00:00
<button onClick={this.handleLogin} className="fluid ui positive button">Log In</button>
2019-08-28 01:42:16 +00:00
</div>
)
case 'requestEmail':
return (
<div className="ui menu dropdown" style={{display: "inline"}}>
<div className="ui left icon input">
2019-09-07 02:18:14 +00:00
<input
type="text"
placeholder="email@address.tld"
2019-09-15 00:03:41 +00:00
name="email"
value={email}
2019-09-07 02:18:14 +00:00
onChange={this.handleInputChange}
2019-09-07 02:38:28 +00:00
></input>
2019-08-28 01:42:16 +00:00
<i className="mail icon"></i>
2019-08-28 00:53:20 +00:00
</div>
2019-09-15 00:03:41 +00:00
<button onClick={this.handlePasswordForAccountCreation} className="fluid ui teal button">Next</button>
2019-08-28 00:53:20 +00:00
</div>
)
2019-08-28 01:42:16 +00:00
case 'requestPasswordForCreation':
2019-08-28 00:53:20 +00:00
return (
<div className="ui menu dropdown" style={{display: "inline"}}>
<div className="ui left icon input">
2019-09-15 00:03:41 +00:00
<input
type={"password"}
placeholder="Password"
name="password"
value={password}
onChange={this.handleInputChange}
2019-09-07 02:38:28 +00:00
></input>
2019-08-28 01:17:11 +00:00
<i className="lock icon"></i>
2019-08-28 00:53:20 +00:00
</div>
2019-09-15 00:03:41 +00:00
<button onClick={this.handleCreateAccount} className="fluid ui positive button">Create Account!</button>
2019-08-28 00:53:20 +00:00
</div>
)
default:
return null;
}
}
2019-08-27 06:17:32 +00:00
render() {
return (
2019-08-28 00:53:20 +00:00
this.authMenu(this.state.auth_menu_visible)
2019-08-27 06:17:32 +00:00
)
}
}
2019-09-15 00:29:55 +00:00
const mapStateToProps = (state) => {
return {
username: state.username,
};
}
export default connect(
mapStateToProps,
{ userLogin }
)(AuthMenu);