mirror of
https://github.com/ascribe/onion.git
synced 2024-12-31 09:07:48 +01:00
Implement cached source for user endpoint
This commit is contained in:
parent
bed067f9bc
commit
469f5108a8
@ -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
25
js/sources/user_source.js
Normal 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;
|
@ -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();
|
||||
}
|
||||
};
|
||||
}
|
@ -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 = {};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user