1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-22 17:33:14 +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( this.generateActions(
'applicationWillBoot', 'applicationWillBoot',
'applicationDidBoot', 'applicationDidBoot',
'profileDidLoad', 'userDidAuthenticate',
//'userDidLogin', 'userDidLogout',
//'userDidLogout',
'routeDidChange' 'routeDidChange'
); );
} }

View File

@ -59,19 +59,6 @@ let Header = React.createClass({
// close the mobile expanded navigation after a click by itself. // close the mobile expanded navigation after a click by itself.
// To get rid of this, we set the state of the component ourselves. // To get rid of this, we set the state of the component ourselves.
history.listen(this.onRouteChange); 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() { componentWillUnmount() {
@ -81,7 +68,7 @@ let Header = React.createClass({
}, },
getLogo() { getLogo() {
let { whitelabel } = this.state; const { whitelabel } = this.state;
if (whitelabel.head) { if (whitelabel.head) {
constructHead(whitelabel.head); constructHead(whitelabel.head);

View File

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

View File

@ -32,19 +32,6 @@ let PRApp = React.createClass({
componentDidMount() { componentDidMount() {
UserStore.listen(this.onChange); UserStore.listen(this.onChange);
UserActions.fetchCurrentUser(); 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() { componentWillUnmount() {

View File

@ -1,8 +1,10 @@
'use strict'; 'use strict';
import { altUser } from '../alt'; import { alt, altWhitelabel, altUser, altThirdParty } from '../alt';
import UserActions from '../actions/user_actions';
import EventActions from '../actions/event_actions';
import UserActions from '../actions/user_actions';
import UserSource from '../sources/user_source'; import UserSource from '../sources/user_source';
@ -29,11 +31,26 @@ class UserStore {
onSuccessFetchCurrentUser({users: [user = {}]}) { onSuccessFetchCurrentUser({users: [user = {}]}) {
this.userMeta.invalidateCache = false; this.userMeta.invalidateCache = false;
this.userMeta.err = null; this.userMeta.err = null;
if (user.email && user.email !== this.currentUser.email) {
EventActions.userDidAuthenticate(user);
}
this.currentUser = user; this.currentUser = user;
} }
onLogoutCurrentUser() { 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() { onSuccessLogoutCurrentUser() {

View File

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

View File

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

View File

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