From 473849c9930f2e04f4ac39ec216b3ffcf0c977ae Mon Sep 17 00:00:00 2001 From: Ro Date: Wed, 24 Jun 2020 14:14:17 -0700 Subject: [PATCH] moved API JSON response parsing to API request method --- brain/data/Book.js | 2 +- src/com/Base.js | 6 ++---- src/com/controllers/NavIndex.js | 5 ++--- src/com/controllers/PageEditor.js | 6 ++---- src/com/controllers/SettingsIndex.js | 10 +++------- src/libraries/FipamoAPI.js | 15 +++++---------- 6 files changed, 15 insertions(+), 29 deletions(-) diff --git a/brain/data/Book.js b/brain/data/Book.js index 4f390b6..46aa08d 100644 --- a/brain/data/Book.js +++ b/brain/data/Book.js @@ -65,7 +65,7 @@ export default class Book { * Edits single page based on id and task * @parameter body: object that contains all page information * @parameter id: identifier for page being edited - * @parameter task: type of task being performed - listed in DataEvents Class + * @parameter task: type of task being performed - listed in DataEvents Class /src/com/events * @parameter user: object contain user information */ editPage(body, id, task, user) { diff --git a/src/com/Base.js b/src/com/Base.js index 6038743..65a1e60 100644 --- a/src/com/Base.js +++ b/src/com/Base.js @@ -53,8 +53,7 @@ export default class Base { CONTENT_TYPE_JSON, authForm ) - .then(r => { - let response = JSON.parse(r.request['response']); + .then(response => { if (response.type === DataEvent.REQUEST_LAME) { notify.alert(response.message, false); } else { @@ -74,8 +73,7 @@ export default class Base { e.preventDefault(); let setUpForm = data.formDataToJSON(document.getElementById('init-form')); api.request(API_INIT, DataEvent.API_INIT, REQUEST_TYPE_POST, CONTENT_TYPE_JSON, setUpForm) - .then(r => { - let response = JSON.parse(r.request['response']); + .then(response => { if (response.type === DataEvent.API_INIT_LAME) { notify.alert(response.message, false); } else { diff --git a/src/com/controllers/NavIndex.js b/src/com/controllers/NavIndex.js index aa18bb1..9a38176 100644 --- a/src/com/controllers/NavIndex.js +++ b/src/com/controllers/NavIndex.js @@ -29,7 +29,7 @@ export default class NavIndex { CONTENT_TYPE_JSON, data ).then(response => { - let r = JSON.parse(response.request['response']); + let r = response.response; if (r.type == DataEvent.MENU_UPDATED) { notify.alert(r.message, true); } else { @@ -60,8 +60,7 @@ export default class NavIndex { REQUEST_TYPE_POST, CONTENT_TYPE_JSON, data - ).then(response => { - let r = JSON.parse(response.request['response']); + ).then(r => { if (r.type == DataEvent.MENU_UPDATED) { notify.alert(r.message, true); } else { diff --git a/src/com/controllers/PageEditor.js b/src/com/controllers/PageEditor.js index 9a72eb0..9c6980c 100644 --- a/src/com/controllers/PageEditor.js +++ b/src/com/controllers/PageEditor.js @@ -146,8 +146,7 @@ export default class PostEditor { CONTENT_TYPE_FORM, page ) - .then(response => { - let r = JSON.parse(response.request['response']); + .then(r => { if ( r.type === DataEvent.PAGE_ERROR || r.type === DataEvent.API_REQUEST_LAME @@ -252,8 +251,7 @@ export default class PostEditor { imageData.append('post_image', file, file.name); } api.request(url, eventType, REQUEST_TYPE_POST, CONTENT_TYPE_FORM, imageData) - .then(response => { - let r = JSON.parse(response.request['response']); + .then(r => { if (r.type == DataEvent.POST_IMAGE_ADDED) self.editor.notify(EditorEvent.EDITOR_UPLOAD_POST_IMAGE, r.url); }) diff --git a/src/com/controllers/SettingsIndex.js b/src/com/controllers/SettingsIndex.js index 7902869..b94257d 100644 --- a/src/com/controllers/SettingsIndex.js +++ b/src/com/controllers/SettingsIndex.js @@ -37,8 +37,7 @@ export default class SettingsIndex { REQUEST_TYPE_POST, CONTENT_TYPE_JSON, data - ).then(response => { - let r = JSON.parse(response.request['response']); + ).then(r => { if (r.type == DataEvent.SETTINGS_UPDATED) { notify.alert(r.message, true); } else { @@ -171,9 +170,7 @@ export default class SettingsIndex { : imageData.append('background_upload', file, file.name); } api.request(url, eventType, REQUEST_TYPE_POST, CONTENT_TYPE_FORM, imageData) - .then(response => { - //TODO: Move JSON processing to API class - let r = JSON.parse(response.request['response']); + .then(r => { if (r.type == DataEvent.AVATAR_UPLOADED) { notify.alert(r.message, true); document.getElementById('avatar').src = r.url; @@ -197,8 +194,7 @@ export default class SettingsIndex { CONTENT_TYPE_JSON, task ) - .then(response => { - let r = JSON.parse(response.request['response']); + .then(r => { notify.alert(r.message, true); }) .catch(err => { diff --git a/src/libraries/FipamoAPI.js b/src/libraries/FipamoAPI.js index f0139e9..33eb93d 100644 --- a/src/libraries/FipamoAPI.js +++ b/src/libraries/FipamoAPI.js @@ -27,8 +27,7 @@ export default class APIUtils { this.token = null; //checks backend to see if user is logged in //and requests encrypted token for api calls - this.request(API_STATUS).then(r => { - let response = JSON.parse(r.request['response']); + this.request(API_STATUS).then(response => { if (response.type === DataEvent.API_REQUEST_GOOD) { this.token = response.token; } else { @@ -53,15 +52,11 @@ export default class APIUtils { request.open(requestType, requestURL, true); request.onload = () => { if (request.status == 200) { - resolve({ - request, - eventType - }); + let response = JSON.parse(request['response']); + resolve(response); } else { - reject({ - request, - eventType - }); + let error = JSON.parse(request['response']); + reject(error); } }; if (requestType == REQUEST_TYPE_PUT || requestType == REQUEST_TYPE_POST) {