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 auth from 'utils/auth';
import { updateUser } from './UserActions';
import { updateTeam } from './TeamActions';
export const SLACK_AUTH_PENDING = 'SLACK_AUTH_PENDING';
export const SLACK_AUTH_SUCCESS = 'SLACK_AUTH_SUCCESS';
export const SLACK_AUTH_FAILURE = 'SLACK_AUTH_FAILURE';
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');
export function slackAuthAsync(code) {
@ -23,12 +20,11 @@ export function slackAuthAsync(code) {
})
.then(data => {
auth.setToken(data.data.accessToken);
dispatch(updateUser(data.data.user));
dispatch(updateTeam(data.data.team));
dispatch(slackAuthSuccess(data.data.user, data.data.team));
dispatch(replace('/dashboard'));
})
// .catch((err) => {
// dispatch(push('/error'));
// })
.catch((err) => {
dispatch(push('/error'));
})
};
};

View File

@ -35,7 +35,32 @@ const store = createStore(reducer, applyMiddleware(
routerMiddlewareWithHistory,
loggerMiddleware,
), 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) {
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) => {
switch (action.type) {
case UPDATE_TEAM: {
return {
...action.team,
};
}
case SLACK_AUTH_SUCCESS: {
return {
...action.team,
};
}
default:
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) => {
switch (action.type) {
case UPDATE_USER: {
case SLACK_AUTH_SUCCESS: {
return {
...action.user,
};

View File

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