2015-06-05 11:06:36 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-07 09:31:38 +02:00
|
|
|
import { altUser } from '../alt';
|
2016-01-15 13:55:58 +01:00
|
|
|
|
2015-06-03 11:49:39 +02:00
|
|
|
import UserActions from '../actions/user_actions';
|
2015-05-20 16:19:40 +02:00
|
|
|
|
2015-10-30 16:57:03 +01:00
|
|
|
import UserSource from '../sources/user_source';
|
2015-05-20 16:44:45 +02:00
|
|
|
|
2015-10-29 17:15:26 +01:00
|
|
|
|
2015-10-30 16:57:03 +01:00
|
|
|
class UserStore {
|
2015-05-20 16:19:40 +02:00
|
|
|
constructor() {
|
2015-06-01 18:59:47 +02:00
|
|
|
this.currentUser = {};
|
2015-11-03 10:07:44 +01:00
|
|
|
this.userMeta = {
|
|
|
|
err: null
|
|
|
|
};
|
2015-11-02 11:31:02 +01:00
|
|
|
|
2015-06-03 11:49:39 +02:00
|
|
|
this.bindActions(UserActions);
|
2015-10-30 16:57:03 +01:00
|
|
|
this.registerAsync(UserSource);
|
2015-05-20 16:19:40 +02:00
|
|
|
}
|
|
|
|
|
2015-11-02 11:31:02 +01:00
|
|
|
onFetchCurrentUser(invalidateCache) {
|
2016-01-15 13:55:58 +01:00
|
|
|
if (invalidateCache || !this.getInstance().isLoading()) {
|
|
|
|
this.getInstance().lookupCurrentUser(invalidateCache);
|
2015-10-30 16:57:03 +01:00
|
|
|
}
|
2016-01-15 13:56:56 +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
|
|
|
}
|
2015-10-30 16:57:03 +01:00
|
|
|
|
2016-01-15 13:55:58 +01:00
|
|
|
onSuccessFetchCurrentUser({ users: [ user = {} ] }) {
|
2015-11-16 17:44:56 +01:00
|
|
|
this.userMeta.err = null;
|
2015-10-30 16:57:03 +01:00
|
|
|
this.currentUser = user;
|
|
|
|
}
|
|
|
|
|
2015-11-02 16:32:55 +01:00
|
|
|
onLogoutCurrentUser() {
|
|
|
|
this.getInstance().performLogoutCurrentUser();
|
2016-01-15 13:56:56 +01:00
|
|
|
|
|
|
|
// Prevent alt from sending an empty change event when a request is sent
|
|
|
|
// off to the source
|
|
|
|
this.preventDefault();
|
2015-11-02 16:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
onSuccessLogoutCurrentUser() {
|
2016-01-15 13:55:58 +01:00
|
|
|
this.userMeta.err = null;
|
2015-06-20 16:43:18 +02:00
|
|
|
this.currentUser = {};
|
|
|
|
}
|
2015-11-02 15:47:57 +01:00
|
|
|
|
2015-11-16 17:16:13 +01:00
|
|
|
onErrorCurrentUser(err) {
|
2015-11-02 15:47:57 +01:00
|
|
|
console.logGlobal(err);
|
2015-11-03 10:07:44 +01:00
|
|
|
this.userMeta.err = err;
|
2015-11-02 15:47:57 +01:00
|
|
|
}
|
2015-06-01 18:59:47 +02:00
|
|
|
}
|
2015-05-20 16:19:40 +02:00
|
|
|
|
2015-10-07 09:31:38 +02:00
|
|
|
export default altUser.createStore(UserStore, 'UserStore');
|