Last active
November 27, 2018 10:21
-
-
Save kvnam/14f70ef3ab912b5645f9e2ec925d4081 to your computer and use it in GitHub Desktop.
ReactPress Auth Container component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
import { Switch, Route, Redirect } from "react-router-dom"; | |
import { connect } from "react-redux"; | |
import * as actionMethods from "../../store/actions/index.actions"; | |
import SignUp from "../../components/Auth/SignUp"; | |
import SignIn from "../../components/Auth/SignIn"; | |
import SignOut from "../../components/Auth/Signout"; | |
class Auth extends Component{ | |
state = { | |
user : { | |
first_name: "", | |
last_name: "", | |
email: "", | |
password: "", | |
username: "", | |
locale: "en_US", | |
description: "", | |
name: "" | |
}, | |
alertText : null | |
}; | |
onUserSignin = () => { | |
//TODO: Validate inputs | |
if(this.state.user.username === "" || this.state.user.password === ""){ | |
//TODO: ADD ERROR DIALOG | |
}else{ | |
//Submit the form | |
let userDets = { | |
username: this.state.user.username, | |
password: this.state.user.password | |
}; | |
this.props.onUserSigninSubmit(userDets); | |
} | |
} | |
onInputClicked = (event, forField, forForm) => { | |
let tempUser = this.state.user; | |
tempUser[forField] = event.target.value; | |
this.setState({user: tempUser}); | |
} | |
render(){ | |
return ( | |
<React.Fragment> | |
{this.props.token ? <Redirect to="/dashboard" /> : null} | |
<Switch> | |
<Route path="/auth/signin" render={() => <SignIn | |
uid={this.state.user.username} | |
pwd={this.state.user.password} | |
onUserClick={this.onUserSignin} | |
inputChanged={this.onInputClicked} />} /> | |
<Route path="/auth/signup" render={() => <SignUp user={this.state.user} | |
inputChanged={this.onInputClicked} | |
onFormSubmit={this.onUserSignUp}/>} /> | |
<Route path="/auth/signout" render={() => <SignOut />} /> | |
</Switch> | |
</React.Fragment> | |
); | |
} | |
}; | |
const mapStateToProps = (state) => { | |
return { | |
userInfo: state.usersRed.userInfo, | |
token: state.usersRed.token | |
}; | |
} | |
const mapDispatchToProps = (dispatch) => { | |
return { | |
onUserSigninSubmit: (user) => {dispatch(actionMethods.userSignin(user))}, | |
}; | |
}; | |
export default connect(mapStateToProps, mapDispatchToProps)(Auth); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment