diff --git a/js/actions/user_actions.js b/js/actions/user_actions.js index 59fdfa25..f9666cdb 100644 --- a/js/actions/user_actions.js +++ b/js/actions/user_actions.js @@ -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(() => { diff --git a/js/sources/user_source.js b/js/sources/user_source.js new file mode 100644 index 00000000..0ac9c6ba --- /dev/null +++ b/js/sources/user_source.js @@ -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; \ No newline at end of file diff --git a/js/stores/session_persistent_store.js b/js/stores/session_persistent_store.js index cd2fa1ca..0036740a 100644 --- a/js/stores/session_persistent_store.js +++ b/js/stores/session_persistent_store.js @@ -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(); - } -}; \ No newline at end of file +} \ No newline at end of file diff --git a/js/stores/user_store.js b/js/stores/user_store.js index dc55f0c2..cdbb40ea 100644 --- a/js/stores/user_store.js +++ b/js/stores/user_store.js @@ -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 = {}; }