-
-
Save VinSpee/4146c0da0b95e261a72c to your computer and use it in GitHub Desktop.
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 from 'react-router'; | |
import { createStore, combineReducers, applyMiddleware } from 'redux'; | |
import { Provider } from 'react-redux'; | |
import promiseMiddleware from 'redux-promise'; | |
import * as stores from './stores'; | |
import routes from './views/routes'; | |
const reducer = combineReducers(stores); | |
const createStoreWithMiddleware = applyMiddleware(promiseMiddleware)(createStore); | |
const store = createStoreWithMiddleware(reducer); | |
Router.run(routes, Router.HistoryLocation, function (Root, state) { | |
React.render(( | |
<Provider store={store}> | |
{() => <Root/>} | |
</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() { | |
console.log(credentials); | |
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 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
import React from 'react'; | |
import { Route } from 'react-router'; | |
import Master from './Master'; | |
var routes = ( | |
<Route handler={Master} path="/admin/?"/> | |
); | |
export default routes; |
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