1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-22 17:33:14 +01:00
onion/js/stores/user_store.js

81 lines
2.1 KiB
JavaScript
Raw Normal View History

'use strict';
import { alt, altUser, altThirdParty } from '../alt';
import EventActions from '../actions/event_actions';
2015-05-20 16:19:40 +02:00
import UserActions from '../actions/user_actions';
import UserSource from '../sources/user_source';
2015-05-20 16:44:45 +02:00
2015-10-29 17:15:26 +01:00
class UserStore {
2015-05-20 16:19:40 +02:00
constructor() {
2015-06-01 18:59:47 +02:00
this.currentUser = {};
this.userMeta = {
hasLoaded: false,
err: null
};
2015-06-03 11:49:39 +02:00
this.bindActions(UserActions);
this.registerAsync(UserSource);
this.exportPublicMethods({
hasLoaded: this.hasLoaded.bind(this)
});
2015-05-20 16:19:40 +02:00
}
onFetchCurrentUser(invalidateCache) {
2016-02-05 10:38:59 +01:00
if (invalidateCache || !this.getInstance().isLoading()) {
this.getInstance().lookupCurrentUser(invalidateCache);
}
2016-02-05 10:38:59 +01:00
// Prevent alt from sending an empty change event when a request is sent
// off to the source
this.preventDefault();
2015-05-20 16:19:40 +02:00
}
onSuccessFetchCurrentUser({ users: [ user = {} ] = [] }) {
this.userMeta.hasLoaded = true;
2015-11-16 17:44:56 +01:00
this.userMeta.err = null;
if (user.email && user.email !== this.currentUser.email) {
EventActions.userDidAuthenticate(user);
}
this.currentUser = user;
}
onLogoutCurrentUser() {
this.getInstance().performLogoutCurrentUser();
2016-02-05 10:38:59 +01:00
// Prevent alt from sending an empty change event when a request is sent
// off to the source
this.preventDefault();
}
onSuccessLogoutCurrentUser() {
EventActions.userDidLogout();
// Reset all stores back to their initial state
// Don't recycle the whitelabel stores since they're not dependent on login
alt.recycle();
altUser.recycle();
altThirdParty.recycle();
// Since we've just logged out, we can set this store's
// hasLoaded flag back to true as there is no current user.
this.userMeta.hasLoaded = true;
2015-06-20 16:43:18 +02:00
}
onErrorCurrentUser(err) {
console.logGlobal(err);
this.userMeta.hasLoaded = true;
this.userMeta.err = err;
}
hasLoaded() {
return this.userMeta.hasLoaded;
}
2015-06-01 18:59:47 +02:00
}
2015-05-20 16:19:40 +02:00
export default altUser.createStore(UserStore, 'UserStore');