Last active
December 14, 2018 13:37
-
-
Save santaklouse/9c428d7a8f1881b10593463db8e75df1 to your computer and use it in GitHub Desktop.
ReactJS code example
This file contains hidden or 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 { userConstants } from '../constants'; | |
| let user = JSON.parse(localStorage.getItem('user')); | |
| const initialState = user ? { loggedIn: true, user } : {}; | |
| export function authentication(state = initialState, action) { | |
| switch (action.type) { | |
| case userConstants.LOGIN_REQUEST: | |
| return { | |
| loggingIn: true, | |
| user: action.user | |
| }; | |
| case userConstants.LOGIN_SUCCESS: | |
| return { | |
| loggedIn: true, | |
| user: action.user | |
| }; | |
| case userConstants.LOGIN_FAILURE: | |
| return {}; | |
| case userConstants.LOGOUT: | |
| return {}; | |
| default: | |
| return state | |
| } | |
| } |
This file contains hidden or 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 from 'react'; | |
| import { connect } from 'react-redux'; | |
| import { userActions } from '../actions'; | |
| class LoginPage extends React.Component { | |
| constructor(props) { | |
| super(props); | |
| this.props.dispatch(userActions.logout()); | |
| this.state = { | |
| email: '', | |
| password: '', | |
| remember: false, | |
| submitted: false | |
| }; | |
| this.handleChange = this.handleChange.bind(this); | |
| this.handleSubmit = this.handleSubmit.bind(this); | |
| } | |
| handleChange(e) { | |
| const { name, value } = e.target; | |
| this.setState({ [name]: value }); | |
| } | |
| handleSubmit(e) { | |
| e.preventDefault(); | |
| this.setState({ submitted: true }); | |
| const { email, password, remember } = this.state; | |
| const { dispatch } = this.props; | |
| if (email && password) { | |
| dispatch(userActions.login(email, password, remember)); | |
| } | |
| } | |
| render() { | |
| const { loggingIn } = this.props; | |
| const { email, password, submitted } = this.state; | |
| return ( | |
| <div className="p-4"> | |
| <div className="bg-white pt-5 pb-1 login-form-wrapper mt-5"> | |
| <div className="login-image"> | |
| <img src="../../images/logo.png" alt="logo"/> | |
| </div> | |
| <h2 className="text-center font-weight-bold">Face IT</h2> | |
| <form name="form" onSubmit={this.handleSubmit}> | |
| <div style={{width: '80%', margin: '20px auto', textAlign: 'left'}} className={'form-group' + (submitted && !email ? ' has-error' : '')}> | |
| <label htmlFor="email">Email</label> | |
| <input type="email" className="form-control" name="email" value={email} onChange={this.handleChange} /> | |
| {submitted && !email && | |
| <div className="help-block">Email is required</div> | |
| } | |
| </div> | |
| <div style={{width: '80%', margin: '0 auto', textAlign: 'left'}} className={'form-group' + (submitted && !password ? ' has-error' : '')}> | |
| <label htmlFor="password">Password</label> | |
| <input type="password" className="form-control" name="password" value={password} onChange={this.handleChange} /> | |
| {submitted && !password && | |
| <div className="help-block">Password is required</div> | |
| } | |
| </div> | |
| <div className="form-check" style={{width: '80%', margin: '20px auto 0', textAlign: 'left'}}> | |
| <input type="checkbox" className="form-check-input" id="exampleCheck1" /> | |
| <label className="form-check-label" htmlFor="exampleCheck1">Check me out</label> | |
| </div> | |
| <div className="form-group" style={{width: '80%', margin: '30px auto', textAlign: 'left'}}> | |
| <button className="btn btn-success btn-block btn-lg"> | |
| Login | |
| {loggingIn && | |
| <img src="data:image/gif;base64,R0lGODlhEAAQAPIAAP///wAAAMLCwkJCQgAAAGJiYoKCgpKSkiH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCgAAACwAAAAAEAAQAAADMwi63P4wyklrE2MIOggZnAdOmGYJRbExwroUmcG2LmDEwnHQLVsYOd2mBzkYDAdKa+dIAAAh+QQJCgAAACwAAAAAEAAQAAADNAi63P5OjCEgG4QMu7DmikRxQlFUYDEZIGBMRVsaqHwctXXf7WEYB4Ag1xjihkMZsiUkKhIAIfkECQoAAAAsAAAAABAAEAAAAzYIujIjK8pByJDMlFYvBoVjHA70GU7xSUJhmKtwHPAKzLO9HMaoKwJZ7Rf8AYPDDzKpZBqfvwQAIfkECQoAAAAsAAAAABAAEAAAAzMIumIlK8oyhpHsnFZfhYumCYUhDAQxRIdhHBGqRoKw0R8DYlJd8z0fMDgsGo/IpHI5TAAAIfkECQoAAAAsAAAAABAAEAAAAzIIunInK0rnZBTwGPNMgQwmdsNgXGJUlIWEuR5oWUIpz8pAEAMe6TwfwyYsGo/IpFKSAAAh+QQJCgAAACwAAAAAEAAQAAADMwi6IMKQORfjdOe82p4wGccc4CEuQradylesojEMBgsUc2G7sDX3lQGBMLAJibufbSlKAAAh+QQJCgAAACwAAAAAEAAQAAADMgi63P7wCRHZnFVdmgHu2nFwlWCI3WGc3TSWhUFGxTAUkGCbtgENBMJAEJsxgMLWzpEAACH5BAkKAAAALAAAAAAQABAAAAMyCLrc/jDKSatlQtScKdceCAjDII7HcQ4EMTCpyrCuUBjCYRgHVtqlAiB1YhiCnlsRkAAAOwAAAAAAAAAAAA==" /> | |
| } | |
| </button> | |
| </div> | |
| </form> | |
| </div> | |
| </div> | |
| ); | |
| } | |
| } | |
| function mapStateToProps(state) { | |
| const { loggingIn } = state.authentication; | |
| return { | |
| loggingIn | |
| }; | |
| } | |
| const connectedLoginPage = connect(mapStateToProps)(LoginPage); | |
| export { connectedLoginPage as LoginPage }; |
This file contains hidden or 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 { userConstants } from '../constants'; | |
| import { userService } from '../services'; | |
| import { history } from '../helpers'; | |
| //--------------------------------------------------------------------- | |
| /** | |
| * login | |
| * | |
| * @param email | |
| * @param password | |
| * @param remember | |
| * @returns {Function} | |
| */ | |
| function login(email, password, remember) { | |
| return dispatch => { | |
| dispatch(request({ email })); | |
| userService.login(email, password, remember) | |
| .then( | |
| user => { | |
| dispatch(success(user)); | |
| history.push('/'); | |
| }, | |
| error => dispatch(failure(error.toString())) | |
| ); | |
| }; | |
| function request(user) { return { type: userConstants.LOGIN_REQUEST, user } } | |
| function success(user) { return { type: userConstants.LOGIN_SUCCESS, user } } | |
| function failure(error) { return { type: userConstants.LOGIN_FAILURE, error } } | |
| } | |
| //--------------------------------------------------------------------- | |
| /** | |
| * logout | |
| * | |
| * @returns {{type: string}} | |
| */ | |
| function logout() { | |
| userService.logout(); | |
| return { type: userConstants.LOGOUT }; | |
| } | |
| export const userActions = { | |
| login, | |
| logout | |
| }; |
This file contains hidden or 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
| export const userConstants = { | |
| LOGIN_REQUEST: 'USERS_LOGIN_REQUEST', | |
| LOGIN_SUCCESS: 'USERS_LOGIN_SUCCESS', | |
| LOGIN_FAILURE: 'USERS_LOGIN_FAILURE', | |
| LOGOUT: 'USERS_LOGOUT', | |
| }; |
This file contains hidden or 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 { host } from '../config-host'; | |
| //--------------------------------------------------------------------- | |
| /** | |
| * login user | |
| * | |
| * @param email | |
| * @param passwd | |
| * @param remember | |
| * @returns {Promise<Response | never>} | |
| */ | |
| function login(email, passwd, remember) { | |
| return fetch(`${host.local}/auth/login`, { | |
| method: 'POST', | |
| headers: { | |
| "accept": "application/json", | |
| "Content-Type": "application/json" | |
| }, | |
| body: JSON.stringify({ email, passwd }) | |
| }) | |
| .then((response) => { | |
| return response.json() | |
| }) | |
| .then(res => { | |
| if (res.data.token) { | |
| localStorage.setItem('user', JSON.stringify(res.data)); | |
| } | |
| return res; | |
| }); | |
| } | |
| //--------------------------------------------------------------------- | |
| /** | |
| * logout | |
| * | |
| * remove user from local storage to log user out | |
| */ | |
| function logout() { | |
| localStorage.removeItem('user'); | |
| } | |
| export const userService = { | |
| login, | |
| logout | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment