Last active
January 22, 2016 21:32
-
-
Save anyong/46fa22162e06a0a892af to your computer and use it in GitHub Desktop.
React Redux Router
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 LOGIN_REQUEST = 'LOGIN_REQUEST'; | |
export const LOGIN_SUCCESS = 'LOGIN_SUCCESS'; | |
export const LOGIN_FAILURE = 'LOGIN_FAILURE'; | |
export const LOGOUT = '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 React from 'react'; | |
import { Router, | |
Route } from 'react-router'; | |
import { history } from 'react-router/lib/BrowserHistory'; | |
import { createStore, | |
combineReducers, | |
applyMiddleware } from 'redux'; | |
import { Provider } from 'react-redux'; | |
import promiseMiddleware from 'redux-promise'; | |
import * as stores from './stores'; | |
import Master from './views/Master'; | |
const reducer = combineReducers(stores); | |
const createStoreWithMiddleware = applyMiddleware(promiseMiddleware)(createStore); | |
const store = createStoreWithMiddleware(reducer); | |
React.render(( | |
<Provider store={store}> | |
{() => | |
<Router history={history}> | |
<Route component={Master} path="admin"/> | |
</Router> | |
} | |
</Provider> | |
), document.getElementById('react-mount')); |
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 { assign } from 'lodash/object'; | |
import { LOGIN_REQUEST, LOGIN_SUCCESS, LOGIN_FAILURE, LOGOUT } from '../constants/ActionTypes'; | |
const initialState = { | |
id: null, | |
loggedIn: false, | |
loggingIn: false, | |
error: false | |
}; | |
export default function auth(state = initialState, action = {}) { | |
switch (action.type) { | |
case LOGIN_REQUEST: | |
return assign({}, initialState, {loggingIn: true}); | |
case LOGIN_SUCCESS: | |
return assign({}, initialState, action.payload, {loggedIn: true}); | |
case LOGIN_FAILURE: | |
return assign({}, initialState, {error: true}); | |
case LOGOUT: | |
return initialState; | |
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 Promise from 'bluebird'; | |
import * as types from '../constants/ActionTypes'; | |
export function loginRequest() { | |
return { | |
type: types.LOGIN_REQUEST | |
} | |
} | |
export function login(credentials) { | |
return Promise | |
.try(function() { | |
var { user, pass } = credentials; | |
if (user === 'john' && pass === 'password') { | |
return { | |
type: types.LOGIN_SUCCESS, | |
payload: { | |
user, | |
loggedIn: true, | |
id: 1 | |
} | |
}; | |
} else { | |
return { | |
type: types.LOGIN_FAILURE | |
} | |
} | |
}); | |
} | |
export function logout() { | |
return { | |
type: types.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 React, { PropTypes } from 'react'; | |
import { bindActionCreators } from 'redux'; | |
import { connect } from 'react-redux'; | |
import * as AuthActions from '../actions/AuthActions'; | |
@connect(store => ({user: store.auth})) | |
class Login extends React.Component { | |
constructor () { | |
super(); | |
this._login = this._login.bind(this); | |
} | |
_login () { | |
var user = this.refs.username.getDOMNode().value; | |
var pass = this.refs.password.getDOMNode().value; | |
var authActions = bindActionCreators(AuthActions, this.props.dispatch); | |
authActions.loginRequest(); | |
authActions.login({user, pass}); | |
} | |
render () { | |
return ( | |
<div> | |
<span>Usernamse: </span> | |
<input type='text' ref='username'/> | |
<span>Password: </span> | |
<input type='password' ref='password'/> | |
<button onClick={this._login}>Login</button> | |
<p>Logging in: {JSON.stringify(this.props.user)}</p> | |
</div> | |
); | |
} | |
} | |
export default Login; |
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, { PropTypes } from 'react'; | |
import { bindActionCreators } from 'redux'; | |
import { connect } from 'react-redux'; | |
import * as AuthActions from '../actions/AuthActions'; | |
@connect(store => ({user: store.auth})) | |
class Logout extends React.Component { | |
constructor () { | |
super(); | |
this._logout = this._logout.bind(this); | |
} | |
_logout () { | |
var authActions = bindActionCreators(AuthActions, this.props.dispatch); | |
authActions.logout(); | |
} | |
render () { | |
return ( | |
<button onClick={this._logout}>Logout</button> | |
); | |
} | |
} | |
export default 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 React from 'react'; | |
import Login from '../components/Login'; | |
import Logout from '../components/Logout'; | |
import { connect } from 'react-redux'; | |
@connect(store => ({user: store.auth})) | |
class Master extends React.Component { | |
render () { | |
var loggedIn = !!this.props.user.id; | |
if (!loggedIn) { | |
return ( | |
<div> | |
<Login/> | |
</div> | |
) | |
} | |
return ( | |
<div> | |
<p>Welcome {this.props.user.user}!</p> | |
<Logout/> | |
</div> | |
); | |
} | |
} | |
export default Master; |
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 { default as auth } from './auth'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment