-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from andela/ft-users-login-#164046210
#164046210: users can log in
- Loading branch information
Showing
22 changed files
with
779 additions
and
33 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
"react" | ||
], | ||
"rules": { | ||
} | ||
}, | ||
"parser": "babel-eslint" | ||
} | ||
|
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import React from 'react'; | ||
import LoginForm from '../containers/LoginForm'; | ||
|
||
const Login = () => <div className="main-container">Login</div>; | ||
const Login = () => <div className="main-container"><LoginForm /></div>; | ||
export default Login; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
import React from 'react'; | ||
import { | ||
Form, Button, Alert, | ||
} from 'react-bootstrap'; | ||
import PropTypes from 'prop-types'; | ||
import { connect } from 'react-redux'; | ||
import loginAction from '../store/actions/authActions/LoginAction'; | ||
import validateLoginForm from '../utils/validation'; | ||
import store from '../store/store'; | ||
import { REGISTER, IS_LOADING } from '../store/actions/actionTypes'; | ||
import ButtonSpinner from '../components/ButtonSpinner'; | ||
|
||
export class LoginForm extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
email: '', | ||
password: '', | ||
formErrors: {}, | ||
touched: false, | ||
show: true, | ||
}; | ||
} | ||
|
||
onAlertClose = () => { | ||
this.setState({ | ||
show: false, | ||
}); | ||
} | ||
|
||
handleChange = (event) => { | ||
this.setState({ [event.target.name]: event.target.value, touched: true }); | ||
}; | ||
|
||
handleSubmit = (event) => { | ||
const { email, password } = this.state; | ||
const { LoginUser } = this.props; | ||
event.preventDefault(); | ||
const formErrors = validateLoginForm(email, password); | ||
if (formErrors.length === 0) { | ||
this.setState({ | ||
formErrors: [], | ||
}); | ||
const data = { email, password }; | ||
store.dispatch({ type: IS_LOADING, payload: true }); | ||
LoginUser(data); | ||
} else { | ||
this.setState({ | ||
formErrors, | ||
touched: false, | ||
}); | ||
} | ||
}; | ||
|
||
swapModal = () => { | ||
store.dispatch({ type: REGISTER }); | ||
}; | ||
|
||
render() { | ||
const { loginUser } = this.props; | ||
const { errors, isLoading } = loginUser; | ||
const { formErrors, touched, show } = this.state; | ||
return ( | ||
<div> | ||
<Form className="main-container" onSubmit={this.handleSubmit}> | ||
<Form.Group name="email"> | ||
{errors && ( | ||
<Alert | ||
className="close-login-alert" | ||
dismissible | ||
variant="danger" | ||
show={show} | ||
onClick={this.onAlertClose} | ||
> | ||
<p> | ||
<small>{errors}</small> | ||
</p> | ||
</Alert> | ||
)} | ||
<Form.Label htmlFor="email">Email address</Form.Label> | ||
<div className="text-danger email-warning"> | ||
{formErrors[0] === 'email' && !touched && formErrors[1]} | ||
</div> | ||
<p className="text-danger email-warning">{errors.email}</p> | ||
<Form.Control | ||
className={ | ||
formErrors[0] === 'email' ? 'red-form-border' : 'email-input' | ||
} | ||
name="email" | ||
type="email" | ||
placeholder="Enter your email" | ||
onChange={this.handleChange} | ||
/> | ||
</Form.Group> | ||
<Form.Group name="password"> | ||
<Form.Label htmlFor="password">Password</Form.Label> | ||
<div className="text-danger password-warning"> | ||
{formErrors[0] === 'password' && !touched && formErrors[1]} | ||
</div> | ||
<p className="text-danger password-warning">{errors.password}</p> | ||
<Form.Control | ||
className={ | ||
formErrors[0] === 'password' | ||
? 'red-form-border' | ||
: 'password-input' | ||
} | ||
name="password" | ||
type="password" | ||
placeholder="Password" | ||
onChange={this.handleChange} | ||
/> | ||
</Form.Group> | ||
<Button className="btn-one" type="submit" disabled={isLoading}> | ||
{ | ||
isLoading && <ButtonSpinner className="btn-disabled btn-one" /> | ||
} | ||
{ isLoading ? 'Loading...' : 'Sign In' } | ||
</Button> | ||
<p className="swap-modal-text"> | ||
Don't have an account? | ||
| ||
<span | ||
role="button" | ||
tabIndex={0} | ||
className="swap-modal-span" | ||
onClick={this.swapModal} | ||
onKeyPress={this.swapModal} | ||
> | ||
Click here | ||
</span> | ||
| ||
to register. | ||
</p> | ||
</Form> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
LoginForm.propTypes = { | ||
loginUser: PropTypes.shape({ | ||
logged_in: PropTypes.bool, | ||
errors: PropTypes.oneOfType([PropTypes.string, PropTypes.array]), | ||
isLoading: PropTypes.bool, | ||
}).isRequired, | ||
LoginUser: PropTypes.func.isRequired, | ||
}; | ||
|
||
export function mapDispatchToProps(dispatch) { | ||
return { | ||
LoginUser: payload => dispatch(loginAction(payload)), | ||
}; | ||
} | ||
|
||
export const mapStateToProps = (state) => { | ||
const { loginUser } = state; | ||
return { | ||
loginUser, | ||
}; | ||
}; | ||
|
||
export default connect(mapStateToProps, mapDispatchToProps)(LoginForm); |
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
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
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
Oops, something went wrong.