From 7ce7f4d17d62270566dfb39016fbdc19536c19b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Daubensch=C3=BCtz?= Date: Mon, 2 Nov 2015 15:47:57 +0100 Subject: [PATCH] Completing prototype for using alt.js's sources instead of fetchers --- js/actions/user_actions.js | 15 +++------------ js/sources/user_source.js | 15 ++++++++++++--- js/stores/user_store.js | 8 +++++++- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/js/actions/user_actions.js b/js/actions/user_actions.js index f9666cdb..8c127e63 100644 --- a/js/actions/user_actions.js +++ b/js/actions/user_actions.js @@ -1,7 +1,6 @@ 'use strict'; import { altUser } from '../alt'; -import UserFetcher from '../fetchers/user_fetcher'; class UserActions { @@ -9,19 +8,11 @@ class UserActions { this.generateActions( 'fetchCurrentUser', 'receiveCurrentUser', - 'deleteCurrentUser' + 'logoutCurrentUser', + 'deleteCurrentUser', + 'currentUserFailed' ); } - - logoutCurrentUser() { - UserFetcher.logout() - .then(() => { - this.actions.deleteCurrentUser(); - }) - .catch((err) => { - console.logGlobal(err); - }); - } } export default altUser.createActions(UserActions); diff --git a/js/sources/user_source.js b/js/sources/user_source.js index ac73093a..28b9448d 100644 --- a/js/sources/user_source.js +++ b/js/sources/user_source.js @@ -1,11 +1,13 @@ 'use strict'; import requests from '../utils/requests'; +import ApiUrls from '../constants/api_urls'; + import UserActions from '../actions/user_actions'; const UserSource = { - fetchUser: { + fetchCurrentUser: { remote() { return requests.get('user'); }, @@ -13,12 +15,19 @@ const UserSource = { local(state) { return state.currentUser && state.currentUser.email ? state : {}; }, - success: UserActions.receiveCurrentUser, - + error: UserActions.currentUserFailed, shouldFetch(state) { return state.invalidateCache || state.currentUser && !state.currentUser.email; } + }, + + logoutCurrentUser: { + remote() { + return requests.get(ApiUrls.users_logout); + }, + success: UserActions.deleteCurrentUser, + error: UserActions.currentUserFailed } }; diff --git a/js/stores/user_store.js b/js/stores/user_store.js index ae19c89c..3bd69255 100644 --- a/js/stores/user_store.js +++ b/js/stores/user_store.js @@ -10,6 +10,7 @@ class UserStore { constructor() { this.currentUser = {}; this.invalidateCache = false; + this.errorMessage = null; this.bindActions(UserActions); this.registerAsync(UserSource); @@ -19,7 +20,7 @@ class UserStore { this.invalidateCache = invalidateCache; if(!this.getInstance().isLoading()) { - this.getInstance().fetchUser(); + this.getInstance().fetchCurrentUser(); } } @@ -31,6 +32,11 @@ class UserStore { onDeleteCurrentUser() { this.currentUser = {}; } + + onCurrentUserFailed(err) { + console.logGlobal(err); + this.errorMessage = err; + } } export default altUser.createStore(UserStore, 'UserStore');