From bfab0be2d906243b29b3dc05dd47dcf8d26e85ef Mon Sep 17 00:00:00 2001 From: Laura Fuentes Grau Date: Tue, 5 Nov 2019 14:56:40 +0100 Subject: [PATCH] wip #210 added query parameter 'param' --- src/common/api/rest-api.js | 1 + src/common/data-managers/rest-data-manager.js | 114 +++++++++++++----- src/user/edit-user.js | 4 +- 3 files changed, 84 insertions(+), 35 deletions(-) diff --git a/src/common/api/rest-api.js b/src/common/api/rest-api.js index 4fb5395..d515079 100644 --- a/src/common/api/rest-api.js +++ b/src/common/api/rest-api.js @@ -73,6 +73,7 @@ class RestAPI { } post(url, body, token) { + console.log(url); return new Promise(function (resolve, reject) { var req = request.post(url).send(body).timeout({ response: 5000 }); // Simple response start timeout (3s) diff --git a/src/common/data-managers/rest-data-manager.js b/src/common/data-managers/rest-data-manager.js index b667ac0..65b904d 100644 --- a/src/common/data-managers/rest-data-manager.js +++ b/src/common/data-managers/rest-data-manager.js @@ -52,7 +52,7 @@ class RestDataManager { return object; } - load(id, token = null) { + load(id, token = null,param = null) { if (id != null) { // load single object RestAPI.get(this.makeURL(this.url + '/' + id), token).then(response => { @@ -96,54 +96,102 @@ class RestDataManager { } } - add(object, token = null) { + add(object, token = null, param = null) { var obj = {}; obj[this.type] = this.filterKeys(object); - RestAPI.post(this.makeURL(this.url), obj, token).then(response => { - AppDispatcher.dispatch({ - type: this.type + 's/added', - data: response[this.type] + if (param === null) { + RestAPI.post(this.makeURL(this.url), obj, token).then(response => { + AppDispatcher.dispatch({ + type: this.type + 's/added', + data: response[this.type] + }); + }).catch(error => { + AppDispatcher.dispatch({ + type: this.type + 's/add-error', + error: error + }); }); - }).catch(error => { - AppDispatcher.dispatch({ - type: this.type + 's/add-error', - error: error + } + else{ + console.log("else was called in add"); + RestAPI.post(this.makeURL(this.url) + "?" + param, obj, token).then(response => { + AppDispatcher.dispatch({ + type: this.type + 's/added', + data: response[this.type] + }); + }).catch(error => { + AppDispatcher.dispatch({ + type: this.type + 's/add-error', + error: error + }); }); - }); + } } - 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], - original: object + remove(object, token = null, param = null) { + if (param === null) { + RestAPI.delete(this.makeURL(this.url + '/' + object.id), token).then(response => { + AppDispatcher.dispatch({ + type: this.type + 's/removed', + data: response[this.type], + original: object + }); + }).catch(error => { + AppDispatcher.dispatch({ + type: this.type + 's/remove-error', + error: error + }); }); - }).catch(error => { - AppDispatcher.dispatch({ - type: this.type + 's/remove-error', - error: error + } + else{ + RestAPI.delete(this.makeURL(this.url + '/' + object.id + '?' + param), token).then(response => { + AppDispatcher.dispatch({ + type: this.type + 's/removed', + data: response[this.type], + original: object + }); + }).catch(error => { + AppDispatcher.dispatch({ + type: this.type + 's/remove-error', + error: error + }); }); - }); + } } - update(object, token = null) { + update(object, token = null, param = null) { var obj = {}; obj[this.type] = this.filterKeys(object); - RestAPI.put(this.makeURL(this.url + '/' + object.id), obj, token).then(response => { - AppDispatcher.dispatch({ - type: this.type + 's/edited', - data: response[this.type] + if(param === null) { + RestAPI.put(this.makeURL(this.url + '/' + object.id), obj, token).then(response => { + AppDispatcher.dispatch({ + type: this.type + 's/edited', + data: response[this.type] + }); + }).catch(error => { + AppDispatcher.dispatch({ + type: this.type + 's/edit-error', + error: error + }); }); - }).catch(error => { - AppDispatcher.dispatch({ - type: this.type + 's/edit-error', - error: error + } + else{ + RestAPI.put(this.makeURL(this.url + '/' + object.id + '?' + param), obj, token).then(response => { + AppDispatcher.dispatch({ + type: this.type + 's/edited', + data: response[this.type] + }); + }).catch(error => { + AppDispatcher.dispatch({ + type: this.type + 's/edit-error', + error: error + }); }); - }); - } + } + } + }; export default RestDataManager; diff --git a/src/user/edit-user.js b/src/user/edit-user.js index 3427452..d60015b 100644 --- a/src/user/edit-user.js +++ b/src/user/edit-user.js @@ -121,8 +121,8 @@ class EditUserDialog extends React.Component { this.handleChange(e)} /> - Old Password - this.handleChange(e)} /> + Admin Password + this.handleChange(e)} /> Password