I just switched my app's data flow to Redux and I am not really getting it.
My wise component looks like:
const LoginPage = ({ auth, doLogin, doLogout }) => (
<div>
<NavBar/>
<div className="row">
<div style={{display: 'flex', justifyContent: 'center'}} className="col-md-4 col-md-offset-4">
<LoginForm doLogin={doLogin} doLogout={doLogout} auth={auth} />
</div>
</div>
</div>
);
const mapStateToProps = state => ({
auth: state.auth,
});
export default connect(mapStateToProps, { doLogin, doLogout })(LoginPage);
So, from what I understood, I have to mapStateToProps to call the states from the store to map into the props of the component.
Now, on my LoginForm component, which is the component that has the field for login inputs, I pass in doLogin doLogout and auth.
What I am not sure of is, where to use the API call that POSTs the login information.
In my auth.js, I wrote my doLogin as:
export const doLogin = (username, password) => {
//console.log(username, password);
//return {
// type: 'types.AUTH_LOGIN'
//};
var self = this;
axios.post('http://localhost:5000/signin',
{
username: username,
password: password
})
.then(function (response) {
console.log(response);
if(response.data.status === "Successful"){
console.log("Login successfull");
}
else{
console.log("There is no user with the given username and password");
//alert("Username does not exist");
}
}
)
.catch(function (error) {
console.log(error);
});
};
As you see on my comment,
//return {
// type: 'types.AUTH_LOGIN'
//};
I tried to return an object to the action creator. But I am not sure how this works and how my state in the store changes.
I know reducers returns the newly changed state, but how do we make the change?
To summarize, my goal is to change the state isLoggedin to true based upon my axios call used in the action.