From 0770a1ed61b9d0d3c83bd88c0644de675ffdbcf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Daubensch=C3=BCtz?= Date: Mon, 2 Nov 2015 11:31:02 +0100 Subject: [PATCH] Implement cache invalidation functionality for UserStore & UserSources --- .../ascribe_routes/proxy_routes/auth_proxy_handler.js | 6 +++++- js/components/ascribe_settings/account_settings.js | 2 +- js/components/ascribe_settings/settings_container.js | 4 ++-- js/sources/user_source.js | 4 ++-- js/stores/user_store.js | 7 ++++++- 5 files changed, 16 insertions(+), 7 deletions(-) diff --git a/js/components/ascribe_routes/proxy_routes/auth_proxy_handler.js b/js/components/ascribe_routes/proxy_routes/auth_proxy_handler.js index cdfc129b..aff1eb9c 100644 --- a/js/components/ascribe_routes/proxy_routes/auth_proxy_handler.js +++ b/js/components/ascribe_routes/proxy_routes/auth_proxy_handler.js @@ -47,7 +47,11 @@ export default function AuthProxyHandler({to, when}) { }, componentDidUpdate() { - this.redirectConditionally(); + // Only refresh this component, when UserSources are not loading + // data from the server + if(!UserStore.isLoading()) { + this.redirectConditionally(); + } }, componentWillUnmount() { diff --git a/js/components/ascribe_settings/account_settings.js b/js/components/ascribe_settings/account_settings.js index f337a17b..c650358c 100644 --- a/js/components/ascribe_settings/account_settings.js +++ b/js/components/ascribe_settings/account_settings.js @@ -27,7 +27,7 @@ let AccountSettings = React.createClass({ }, handleSuccess(){ - this.props.loadUser(); + this.props.loadUser(true); let notification = new GlobalNotificationModel(getLangText('Settings succesfully updated'), 'success', 5000); GlobalNotificationActions.appendGlobalNotification(notification); }, diff --git a/js/components/ascribe_settings/settings_container.js b/js/components/ascribe_settings/settings_container.js index 923759fd..5b05e708 100644 --- a/js/components/ascribe_settings/settings_container.js +++ b/js/components/ascribe_settings/settings_container.js @@ -46,8 +46,8 @@ let SettingsContainer = React.createClass({ UserStore.unlisten(this.onChange); }, - loadUser(){ - UserActions.fetchCurrentUser(); + loadUser(invalidateCache){ + UserActions.fetchCurrentUser(invalidateCache); }, onChange(state) { diff --git a/js/sources/user_source.js b/js/sources/user_source.js index 0ac9c6ba..ac73093a 100644 --- a/js/sources/user_source.js +++ b/js/sources/user_source.js @@ -11,13 +11,13 @@ const UserSource = { }, local(state) { - return state.currentUser && state.currentUser.email ? state.currentUser : {}; + return state.currentUser && state.currentUser.email ? state : {}; }, success: UserActions.receiveCurrentUser, shouldFetch(state) { - return state.currentUser && !state.currentUser.email; + return state.invalidateCache || state.currentUser && !state.currentUser.email; } } }; diff --git a/js/stores/user_store.js b/js/stores/user_store.js index 94d7777e..ae19c89c 100644 --- a/js/stores/user_store.js +++ b/js/stores/user_store.js @@ -9,17 +9,22 @@ import UserSource from '../sources/user_source'; class UserStore { constructor() { this.currentUser = {}; + this.invalidateCache = false; + this.bindActions(UserActions); this.registerAsync(UserSource); } - onFetchCurrentUser() { + onFetchCurrentUser(invalidateCache) { + this.invalidateCache = invalidateCache; + if(!this.getInstance().isLoading()) { this.getInstance().fetchUser(); } } onReceiveCurrentUser({users: [user]}) { + this.invalidateCache = false; this.currentUser = user; }