1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-22 17:33:14 +01: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'; 'use strict';
import { altUser } from '../alt'; import { altUser } from '../alt';
import UserFetcher from '../fetchers/user_fetcher';
class UserActions { class UserActions {
@ -9,19 +8,11 @@ class UserActions {
this.generateActions( this.generateActions(
'fetchCurrentUser', 'fetchCurrentUser',
'receiveCurrentUser', 'receiveCurrentUser',
'deleteCurrentUser' 'logoutCurrentUser',
'deleteCurrentUser',
'currentUserFailed'
); );
} }
logoutCurrentUser() {
UserFetcher.logout()
.then(() => {
this.actions.deleteCurrentUser();
})
.catch((err) => {
console.logGlobal(err);
});
}
} }
export default altUser.createActions(UserActions); export default altUser.createActions(UserActions);

View File

@ -1,11 +1,13 @@
'use strict'; 'use strict';
import requests from '../utils/requests'; import requests from '../utils/requests';
import ApiUrls from '../constants/api_urls';
import UserActions from '../actions/user_actions'; import UserActions from '../actions/user_actions';
const UserSource = { const UserSource = {
fetchUser: { fetchCurrentUser: {
remote() { remote() {
return requests.get('user'); return requests.get('user');
}, },
@ -13,12 +15,19 @@ const UserSource = {
local(state) { local(state) {
return state.currentUser && state.currentUser.email ? state : {}; return state.currentUser && state.currentUser.email ? state : {};
}, },
success: UserActions.receiveCurrentUser, success: UserActions.receiveCurrentUser,
error: UserActions.currentUserFailed,
shouldFetch(state) { shouldFetch(state) {
return state.invalidateCache || state.currentUser && !state.currentUser.email; 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() { constructor() {
this.currentUser = {}; this.currentUser = {};
this.invalidateCache = false; this.invalidateCache = false;
this.errorMessage = null;
this.bindActions(UserActions); this.bindActions(UserActions);
this.registerAsync(UserSource); this.registerAsync(UserSource);
@ -19,7 +20,7 @@ class UserStore {
this.invalidateCache = invalidateCache; this.invalidateCache = invalidateCache;
if(!this.getInstance().isLoading()) { if(!this.getInstance().isLoading()) {
this.getInstance().fetchUser(); this.getInstance().fetchCurrentUser();
} }
} }
@ -31,6 +32,11 @@ class UserStore {
onDeleteCurrentUser() { onDeleteCurrentUser() {
this.currentUser = {}; this.currentUser = {};
} }
onCurrentUserFailed(err) {
console.logGlobal(err);
this.errorMessage = err;
}
} }
export default altUser.createStore(UserStore, 'UserStore'); export default altUser.createStore(UserStore, 'UserStore');