1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-22 09:23:13 +01:00

Refactor EventActions and UserStore to more cleanly handle user authentication and log out events

This commit is contained in:
Brett Sun 2016-01-08 14:59:45 +01:00
parent 2acf3f4056
commit 19841ce6c4
8 changed files with 49 additions and 53 deletions

View File

@ -8,9 +8,8 @@ class EventActions {
this.generateActions(
'applicationWillBoot',
'applicationDidBoot',
'profileDidLoad',
//'userDidLogin',
//'userDidLogout',
'userDidAuthenticate',
'userDidLogout',
'routeDidChange'
);
}

View File

@ -59,19 +59,6 @@ let Header = React.createClass({
// close the mobile expanded navigation after a click by itself.
// To get rid of this, we set the state of the component ourselves.
history.listen(this.onRouteChange);
if (this.state.currentUser && this.state.currentUser.email) {
EventActions.profileDidLoad.defer(this.state.currentUser);
}
},
componentWillUpdate(nextProps, nextState) {
const { currentUser: { email: curEmail } = {} } = this.state;
const { currentUser: { email: nextEmail } = {} } = nextState;
if (nextEmail && curEmail !== nextEmail) {
EventActions.profileDidLoad.defer(nextState.currentUser);
}
},
componentWillUnmount() {
@ -81,7 +68,7 @@ let Header = React.createClass({
},
getLogo() {
let { whitelabel } = this.state;
const { whitelabel } = this.state;
if (whitelabel.head) {
constructHead(whitelabel.head);
@ -102,7 +89,7 @@ let Header = React.createClass({
);
},
getPoweredBy(){
getPoweredBy() {
return (
<AclProxy
aclObject={this.state.whitelabel}

View File

@ -6,7 +6,6 @@ import { History } from 'react-router';
import AscribeSpinner from './ascribe_spinner';
import UserActions from '../actions/user_actions';
import { alt, altWhitelabel, altUser, altThirdParty } from '../alt';
import { getLangText } from '../utils/lang_utils';
import { setDocumentTitle } from '../utils/dom_utils';
@ -17,12 +16,6 @@ let LogoutContainer = React.createClass({
componentDidMount() {
UserActions.logoutCurrentUser();
alt.flush();
altWhitelabel.flush();
altUser.flush();
altThirdParty.flush();
// kill intercom (with fire)
window.Intercom('shutdown');
},
render() {

View File

@ -32,19 +32,6 @@ let PRApp = React.createClass({
componentDidMount() {
UserStore.listen(this.onChange);
UserActions.fetchCurrentUser();
if (this.state.currentUser && this.state.currentUser.email) {
EventActions.profileDidLoad.defer(this.state.currentUser);
}
},
componentWillUpdate(nextProps, nextState) {
const { currentUser: { email: curEmail } = {} } = this.state;
const { currentUser: { email: nextEmail } = {} } = nextState;
if (nextEmail && curEmail !== nextEmail) {
EventActions.profileDidLoad.defer(nextState.currentUser);
}
},
componentWillUnmount() {

View File

@ -1,8 +1,10 @@
'use strict';
import { altUser } from '../alt';
import UserActions from '../actions/user_actions';
import { alt, altWhitelabel, altUser, altThirdParty } from '../alt';
import EventActions from '../actions/event_actions';
import UserActions from '../actions/user_actions';
import UserSource from '../sources/user_source';
@ -21,7 +23,7 @@ class UserStore {
onFetchCurrentUser(invalidateCache) {
this.userMeta.invalidateCache = invalidateCache;
if(!this.getInstance().isLoading()) {
if (!this.getInstance().isLoading()) {
this.getInstance().lookupCurrentUser();
}
}
@ -29,11 +31,26 @@ class UserStore {
onSuccessFetchCurrentUser({users: [user = {}]}) {
this.userMeta.invalidateCache = false;
this.userMeta.err = null;
if (user.email && user.email !== this.currentUser.email) {
EventActions.userDidAuthenticate(user);
}
this.currentUser = user;
}
onLogoutCurrentUser() {
this.getInstance().performLogoutCurrentUser();
this.getInstance()
.performLogoutCurrentUser()
.then(() => {
EventActions.userDidLogout();
// Reset all stores back to their initial state
alt.recycle();
altWhitelabel.recycle();
altUser.recycle();
altThirdParty.recycle();
});
}
onSuccessLogoutCurrentUser() {

View File

@ -12,25 +12,28 @@ class IntercomHandler {
this.loaded = false;
}
onProfileDidLoad(profile) {
onUserDidAuthenticate(user) {
if (this.loaded) {
return;
}
/* eslint-disable */
Intercom('boot', {
/* eslint-enable */
window.Intercom('boot', {
app_id: 'oboxh5w1',
email: profile.email,
email: user.email,
subdomain: getSubdomain(),
widget: {
activator: '#IntercomDefaultWidget'
}
}
});
console.log('Intercom loaded');
this.loaded = true;
}
onUserDidLogout() {
// kill intercom (with fire)
window.Intercom('shutdown');
this.loaded = false;
}
}
export default altThirdParty.createStore(IntercomHandler, 'IntercomHandler');

View File

@ -17,18 +17,19 @@ class NotificationsHandler {
this.loaded = false;
}
onProfileDidLoad() {
onUserDidAuthenticate() {
if (this.loaded) {
return;
}
let subdomain = getSubdomain();
const subdomain = getSubdomain();
if (subdomain === 'ikonotv') {
NotificationActions.fetchContractAgreementListNotifications().then(
(res) => {
if (res.notifications && res.notifications.length > 0) {
this.loaded = true;
console.log('Contractagreement notifications loaded');
this.loaded = true;
history.pushState(null, '/contract_notifications');
}
}
@ -36,6 +37,10 @@ class NotificationsHandler {
}
this.loaded = true;
}
onUserDidLogout() {
this.loaded = false;
}
}
export default altThirdParty.createStore(NotificationsHandler, 'NotificationsHandler');

View File

@ -12,17 +12,22 @@ class RavenHandler {
this.loaded = false;
}
onProfileDidLoad(profile) {
onUserDidAuthenticate(user) {
if (this.loaded) {
return;
}
Raven.setUserContext({
email: profile.email
email: user.email
});
console.log('Raven loaded');
this.loaded = true;
}
onUserDidLogout() {
Raven.setUserContext();
this.loaded = false;
}
}
export default altThirdParty.createStore(RavenHandler, 'RavenHandler');