1
0
mirror of https://github.com/ascribe/onion.git synced 2024-06-28 08:37:59 +02:00

Completing prototype for using alt.js's sources instead of fetchers

This commit is contained in:
Tim Daubenschütz 2015-11-02 15:47:57 +01:00
parent 0157c048ab
commit 7ce7f4d17d
3 changed files with 22 additions and 16 deletions

View File

@ -1,7 +1,6 @@
'use strict';
import { altUser } from '../alt';
import UserFetcher from '../fetchers/user_fetcher';
class UserActions {
@ -9,19 +8,11 @@ class UserActions {
this.generateActions(
'fetchCurrentUser',
'receiveCurrentUser',
'deleteCurrentUser'
'logoutCurrentUser',
'deleteCurrentUser',
'currentUserFailed'
);
}
logoutCurrentUser() {
UserFetcher.logout()
.then(() => {
this.actions.deleteCurrentUser();
})
.catch((err) => {
console.logGlobal(err);
});
}
}
export default altUser.createActions(UserActions);

View File

@ -1,11 +1,13 @@
'use strict';
import requests from '../utils/requests';
import ApiUrls from '../constants/api_urls';
import UserActions from '../actions/user_actions';
const UserSource = {
fetchUser: {
fetchCurrentUser: {
remote() {
return requests.get('user');
},
@ -13,12 +15,19 @@ const UserSource = {
local(state) {
return state.currentUser && state.currentUser.email ? state : {};
},
success: UserActions.receiveCurrentUser,
error: UserActions.currentUserFailed,
shouldFetch(state) {
return state.invalidateCache || state.currentUser && !state.currentUser.email;
}
},
logoutCurrentUser: {
remote() {
return requests.get(ApiUrls.users_logout);
},
success: UserActions.deleteCurrentUser,
error: UserActions.currentUserFailed
}
};

View File

@ -10,6 +10,7 @@ class UserStore {
constructor() {
this.currentUser = {};
this.invalidateCache = false;
this.errorMessage = null;
this.bindActions(UserActions);
this.registerAsync(UserSource);
@ -19,7 +20,7 @@ class UserStore {
this.invalidateCache = invalidateCache;
if(!this.getInstance().isLoading()) {
this.getInstance().fetchUser();
this.getInstance().fetchCurrentUser();
}
}
@ -31,6 +32,11 @@ class UserStore {
onDeleteCurrentUser() {
this.currentUser = {};
}
onCurrentUserFailed(err) {
console.logGlobal(err);
this.errorMessage = err;
}
}
export default altUser.createStore(UserStore, 'UserStore');