Fixed persistent loading of user and team

This commit is contained in:
Jori Lallo
2016-05-02 21:58:39 -07:00
parent aa8dbe5626
commit f785e37fab
5 changed files with 40 additions and 33 deletions

View File

@@ -3,15 +3,12 @@ import { replace } from 'react-router-redux';
import { client } from 'utils/ApiClient'; import { client } from 'utils/ApiClient';
import auth from 'utils/auth'; import auth from 'utils/auth';
import { updateUser } from './UserActions';
import { updateTeam } from './TeamActions';
export const SLACK_AUTH_PENDING = 'SLACK_AUTH_PENDING'; export const SLACK_AUTH_PENDING = 'SLACK_AUTH_PENDING';
export const SLACK_AUTH_SUCCESS = 'SLACK_AUTH_SUCCESS'; export const SLACK_AUTH_SUCCESS = 'SLACK_AUTH_SUCCESS';
export const SLACK_AUTH_FAILURE = 'SLACK_AUTH_FAILURE'; export const SLACK_AUTH_FAILURE = 'SLACK_AUTH_FAILURE';
const slackAuthPending = makeActionCreator(SLACK_AUTH_PENDING); const slackAuthPending = makeActionCreator(SLACK_AUTH_PENDING);
const slackAuthSuccess = makeActionCreator(SLACK_AUTH_SUCCESS, 'user'); const slackAuthSuccess = makeActionCreator(SLACK_AUTH_SUCCESS, 'user', 'team');
const slackAuthFailure = makeActionCreator(SLACK_AUTH_FAILURE, 'error'); const slackAuthFailure = makeActionCreator(SLACK_AUTH_FAILURE, 'error');
export function slackAuthAsync(code) { export function slackAuthAsync(code) {
@@ -23,12 +20,11 @@ export function slackAuthAsync(code) {
}) })
.then(data => { .then(data => {
auth.setToken(data.data.accessToken); auth.setToken(data.data.accessToken);
dispatch(updateUser(data.data.user)); dispatch(slackAuthSuccess(data.data.user, data.data.team));
dispatch(updateTeam(data.data.team));
dispatch(replace('/dashboard')); dispatch(replace('/dashboard'));
}) })
// .catch((err) => { .catch((err) => {
// dispatch(push('/error')); dispatch(push('/error'));
// }) })
}; };
}; };

View File

@@ -35,7 +35,32 @@ const store = createStore(reducer, applyMiddleware(
routerMiddlewareWithHistory, routerMiddlewareWithHistory,
loggerMiddleware, loggerMiddleware,
), autoRehydrate()); ), autoRehydrate());
persistStore(store);
persistStore(store, {
whitelist: [
'user',
'team',
]
}, () => {
render((
<Provider store={store}>
<Router history={History}>
<Route path="/">
<IndexRoute component={Home} />
<Route path="/dashboard" component={Dashboard
} onEnter={ requireAuth } />
<Route path="/atlas/:id" component={Dashboard} onEnter={ requireAuth } />
<Route path="/atlas/:id/new" component={Dashboard} onEnter={ requireAuth } />
<Route path="/editor" component={Dashboard} />
<Route path="/auth/slack" component={SlackAuth} />
</Route>
</Router>
</Provider>
), document.getElementById('root'));
});
function requireAuth(nextState, replace) { function requireAuth(nextState, replace) {
if (!auth.loggedIn()) { if (!auth.loggedIn()) {
@@ -46,17 +71,3 @@ function requireAuth(nextState, replace) {
} }
} }
render((
<Provider store={store}>
<Router history={History}>
<Route path="/">
<IndexRoute component={Home} />
<Route path="/dashboard" component={Dashboard} onEnter={ requireAuth } />
<Route path="/editor" component={App} />
<Route path="/auth/slack" component={SlackAuth} />
</Route>
</Router>
</Provider>
), document.getElementById('root'));

View File

@@ -1,12 +1,12 @@
import { UPDATE_TEAM } from 'actions/TeamActions'; import { SLACK_AUTH_SUCCESS } from 'actions/SlackAuthAction';
const team = (state = null, action) => { const team = (state = null, action) => {
switch (action.type) { switch (action.type) {
case UPDATE_TEAM: { case SLACK_AUTH_SUCCESS: {
return { return {
...action.team, ...action.team,
}; };
} }
default: default:
return state; return state;
} }

View File

@@ -1,8 +1,8 @@
import { UPDATE_USER } from 'actions/UserActions'; import { SLACK_AUTH_SUCCESS } from 'actions/SlackAuthAction';
const user = (state = null, action) => { const user = (state = null, action) => {
switch (action.type) { switch (action.type) {
case UPDATE_USER: { case SLACK_AUTH_SUCCESS: {
return { return {
...action.user, ...action.user,
}; };

View File

@@ -20,7 +20,7 @@ class SlackAuth extends React.Component {
render() { render() {
return ( return (
<div>Loading...</div> <div></div>
); );
} }
} }