mirror of
https://github.com/ascribe/onion.git
synced 2024-12-22 09:23:13 +01:00
Completing prototype for using alt.js's sources instead of fetchers
This commit is contained in:
parent
0157c048ab
commit
7ce7f4d17d
@ -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);
|
||||
|
@ -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
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -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');
|
||||
|
Loading…
Reference in New Issue
Block a user