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:
parent
6a4c31baef
commit
1a54ae09a2
4 changed files with 16 additions and 33 deletions
|
@ -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]
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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':
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue