From 1a54ae09a21183a2fea3ee11ee9f11c9eeabad3e Mon Sep 17 00:00:00 2001 From: Ricardo Hernandez-Montoya Date: Tue, 2 May 2017 17:26:47 +0200 Subject: [PATCH] Allow optional token inclusion in REST API calls --- src/data-managers/rest-data-manager.js | 18 +++++++++--------- src/data-managers/users-data-manager.js | 15 +-------------- src/stores/array-store.js | 11 ++++++----- src/stores/users-store.js | 5 ----- 4 files changed, 16 insertions(+), 33 deletions(-) diff --git a/src/data-managers/rest-data-manager.js b/src/data-managers/rest-data-manager.js index f4fd82f..99dbd04 100644 --- a/src/data-managers/rest-data-manager.js +++ b/src/data-managers/rest-data-manager.js @@ -51,10 +51,10 @@ class RestDataManager { return object; } - load(id) { + load(id, token = null) { if (id != null) { // load single object - RestAPI.get(this.makeURL(this.url + '/' + id)).then(response => { + RestAPI.get(this.makeURL(this.url + '/' + id), token).then(response => { const data = this.filterKeys(response[this.type]); AppDispatcher.dispatch({ @@ -69,7 +69,7 @@ class RestDataManager { }); } else { // load all objects - RestAPI.get(this.makeURL(this.url)).then(response => { + RestAPI.get(this.makeURL(this.url), token).then(response => { const data = response[this.type + 's'].map(element => { return this.filterKeys(element); }); @@ -87,11 +87,11 @@ class RestDataManager { } } - add(object) { + add(object, token = null) { var obj = {}; obj[this.type] = this.filterKeys(object); - RestAPI.post(this.makeURL(this.url), obj).then(response => { + RestAPI.post(this.makeURL(this.url), obj, token).then(response => { AppDispatcher.dispatch({ type: this.type + 's/added', data: response[this.type] @@ -104,8 +104,8 @@ class RestDataManager { }); } - remove(object) { - RestAPI.delete(this.makeURL(this.url + '/' + object._id)).then(response => { + remove(object, token = null) { + RestAPI.delete(this.makeURL(this.url + '/' + object._id), token).then(response => { AppDispatcher.dispatch({ type: this.type + 's/removed', data: response[this.type], @@ -119,11 +119,11 @@ class RestDataManager { }); } - update(object) { + update(object, token = null) { var obj = {}; obj[this.type] = this.filterKeys(object); - RestAPI.put(this.makeURL(this.url + '/' + object._id), obj).then(response => { + RestAPI.put(this.makeURL(this.url + '/' + object._id, token), obj).then(response => { AppDispatcher.dispatch({ type: this.type + 's/edited', data: response[this.type] diff --git a/src/data-managers/users-data-manager.js b/src/data-managers/users-data-manager.js index b20c3c2..bd60534 100644 --- a/src/data-managers/users-data-manager.js +++ b/src/data-managers/users-data-manager.js @@ -55,20 +55,7 @@ class UsersDataManager extends RestDataManager { }); }); } - - getUsers(token) { - RestAPI.get(this.makeURL('/users'), token).then(response => { - AppDispatcher.dispatch({ - type: 'users/loaded', - data: response.users - }); - }).catch(error => { - AppDispatcher.dispatch({ - type: 'users/load-error', - error: error - }); - }); - } + } export default new UsersDataManager(); diff --git a/src/stores/array-store.js b/src/stores/array-store.js index 9ff162e..b7cfbee 100644 --- a/src/stores/array-store.js +++ b/src/stores/array-store.js @@ -69,10 +69,10 @@ class ArrayStore extends ReduceStore { case this.type + '/start-load': if (Array.isArray(action.data)) { action.data.forEach((id) => { - this.dataManager.load(id); + this.dataManager.load(id, action.token); }); } else { - this.dataManager.load(action.data); + this.dataManager.load(action.data, action.token); } return state; @@ -88,18 +88,19 @@ class ArrayStore extends ReduceStore { return state; case this.type + '/start-add': - this.dataManager.add(action.data); + this.dataManager.add(action.data, action.token); return state; case this.type + '/added': return this.updateElements(state, [action.data]); case this.type + '/add-error': + console.log('something happened'); // TODO: Add error message return state; case this.type + '/start-remove': - this.dataManager.remove(action.data); + this.dataManager.remove(action.data, action.token); return state; case this.type + '/removed': @@ -112,7 +113,7 @@ class ArrayStore extends ReduceStore { return state; case this.type + '/start-edit': - this.dataManager.update(action.data); + this.dataManager.update(action.data, action.token); return state; case this.type + '/edited': diff --git a/src/stores/users-store.js b/src/stores/users-store.js index 3eb1595..0b38e69 100644 --- a/src/stores/users-store.js +++ b/src/stores/users-store.js @@ -30,11 +30,6 @@ class UsersStore extends ArrayStore { reduce(state, action) { switch (action.type) { - // Override ArrayStore's start-load to pass token - case 'users/start-load': - UsersDataManager.getUsers(action.token); - return state; - default: return super.reduce(state, action); }