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

Implement cached source for user endpoint

This commit is contained in:
Tim Daubenschütz 2015-10-30 16:57:03 +01:00
parent bed067f9bc
commit 469f5108a8
4 changed files with 40 additions and 39 deletions

View File

@ -3,41 +3,16 @@
import { altUser } from '../alt';
import UserFetcher from '../fetchers/user_fetcher';
import UserStore from '../stores/user_store';
class UserActions {
constructor() {
this.generateActions(
'updateCurrentUser',
'fetchCurrentUser',
'receiveCurrentUser',
'deleteCurrentUser'
);
}
fetchCurrentUser() {
UserFetcher.fetchOne()
.then((res) => {
this.actions.updateCurrentUser(res.users[0]);
})
.catch((err) => {
console.logGlobal(err);
this.actions.updateCurrentUser({});
});
}
/*fetchCurrentUser() {
if(UserStore.getState().currentUser && !UserStore.getState().currentUser.email) {
UserFetcher.fetchOne()
.then((res) => {
this.actions.updateCurrentUser(res.users[0]);
})
.catch((err) => {
console.logGlobal(err);
this.actions.updateCurrentUser({});
});
}
}*/
logoutCurrentUser() {
UserFetcher.logout()
.then(() => {

25
js/sources/user_source.js Normal file
View File

@ -0,0 +1,25 @@
'use strict';
import requests from '../utils/requests';
import UserActions from '../actions/user_actions';
const UserSource = {
fetchUser: {
remote() {
return requests.get('user');
},
local(state) {
return state.currentUser && state.currentUser.email ? state.currentUser : {};
},
success: UserActions.receiveCurrentUser,
shouldFetch(state) {
return state.currentUser && !state.currentUser.email;
}
}
};
export default UserSource;

View File

@ -12,10 +12,4 @@ export default class SessionPersistentStore extends AscribeStorage {
this[key] = value;
super.setItem(key, value);
}
}
SessionPersistentStore.config = {
getState() {
return new AscribeStorage('sessionStorage', this.displayName).toObject();
}
};
}

View File

@ -3,22 +3,29 @@
import { altUser } from '../alt';
import UserActions from '../actions/user_actions';
import SessionPersistentStore from './session_persistent_store';
import UserSource from '../sources/user_source';
// import AscribeStorage from '../models/ascribe_storage';
// import { sessionStorageAvailable } from '../utils/feature_detection_utils';
class UserStore extends SessionPersistentStore {
class UserStore {
constructor() {
super('UserStore');
this.currentUser = {};
this.bindActions(UserActions);
this.registerAsync(UserSource);
}
onUpdateCurrentUser(user) {
this.setItem('currentUser', user);
onFetchCurrentUser() {
if(!this.getInstance().isLoading()) {
this.getInstance().fetchUser();
}
}
onReceiveCurrentUser({users: [user]}) {
this.currentUser = user;
}
onDeleteCurrentUser() {
this.currentUser = {};
}