1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/web/ synced 2025-03-30 00:00:13 +01:00

Allow optional token inclusion in REST API calls

This commit is contained in:
Ricardo Hernandez-Montoya 2017-05-02 17:26:47 +02:00
parent 6a4c31baef
commit 1a54ae09a2
4 changed files with 16 additions and 33 deletions

View file

@ -51,10 +51,10 @@ class RestDataManager {
return object; return object;
} }
load(id) { load(id, token = null) {
if (id != null) { if (id != null) {
// load single object // 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]); const data = this.filterKeys(response[this.type]);
AppDispatcher.dispatch({ AppDispatcher.dispatch({
@ -69,7 +69,7 @@ class RestDataManager {
}); });
} else { } else {
// load all objects // 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 => { const data = response[this.type + 's'].map(element => {
return this.filterKeys(element); return this.filterKeys(element);
}); });
@ -87,11 +87,11 @@ class RestDataManager {
} }
} }
add(object) { add(object, token = null) {
var obj = {}; var obj = {};
obj[this.type] = this.filterKeys(object); 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({ AppDispatcher.dispatch({
type: this.type + 's/added', type: this.type + 's/added',
data: response[this.type] data: response[this.type]
@ -104,8 +104,8 @@ class RestDataManager {
}); });
} }
remove(object) { remove(object, token = null) {
RestAPI.delete(this.makeURL(this.url + '/' + object._id)).then(response => { RestAPI.delete(this.makeURL(this.url + '/' + object._id), token).then(response => {
AppDispatcher.dispatch({ AppDispatcher.dispatch({
type: this.type + 's/removed', type: this.type + 's/removed',
data: response[this.type], data: response[this.type],
@ -119,11 +119,11 @@ class RestDataManager {
}); });
} }
update(object) { update(object, token = null) {
var obj = {}; var obj = {};
obj[this.type] = this.filterKeys(object); 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({ AppDispatcher.dispatch({
type: this.type + 's/edited', type: this.type + 's/edited',
data: response[this.type] data: response[this.type]

View file

@ -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(); export default new UsersDataManager();

View file

@ -69,10 +69,10 @@ class ArrayStore extends ReduceStore {
case this.type + '/start-load': case this.type + '/start-load':
if (Array.isArray(action.data)) { if (Array.isArray(action.data)) {
action.data.forEach((id) => { action.data.forEach((id) => {
this.dataManager.load(id); this.dataManager.load(id, action.token);
}); });
} else { } else {
this.dataManager.load(action.data); this.dataManager.load(action.data, action.token);
} }
return state; return state;
@ -88,18 +88,19 @@ class ArrayStore extends ReduceStore {
return state; return state;
case this.type + '/start-add': case this.type + '/start-add':
this.dataManager.add(action.data); this.dataManager.add(action.data, action.token);
return state; return state;
case this.type + '/added': case this.type + '/added':
return this.updateElements(state, [action.data]); return this.updateElements(state, [action.data]);
case this.type + '/add-error': case this.type + '/add-error':
console.log('something happened');
// TODO: Add error message // TODO: Add error message
return state; return state;
case this.type + '/start-remove': case this.type + '/start-remove':
this.dataManager.remove(action.data); this.dataManager.remove(action.data, action.token);
return state; return state;
case this.type + '/removed': case this.type + '/removed':
@ -112,7 +113,7 @@ class ArrayStore extends ReduceStore {
return state; return state;
case this.type + '/start-edit': case this.type + '/start-edit':
this.dataManager.update(action.data); this.dataManager.update(action.data, action.token);
return state; return state;
case this.type + '/edited': case this.type + '/edited':

View file

@ -30,11 +30,6 @@ class UsersStore extends ArrayStore {
reduce(state, action) { reduce(state, action) {
switch (action.type) { switch (action.type) {
// Override ArrayStore's start-load to pass token
case 'users/start-load':
UsersDataManager.getUsers(action.token);
return state;
default: default:
return super.reduce(state, action); return super.reduce(state, action);
} }