moved API JSON response parsing to API request method

pull/20/head
Ro 4 years ago
parent 192998bdb4
commit 473849c993

@ -65,7 +65,7 @@ export default class Book {
* Edits single page based on id and task * Edits single page based on id and task
* @parameter body: object that contains all page information * @parameter body: object that contains all page information
* @parameter id: identifier for page being edited * @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 * @parameter user: object contain user information
*/ */
editPage(body, id, task, user) { editPage(body, id, task, user) {

@ -53,8 +53,7 @@ export default class Base {
CONTENT_TYPE_JSON, CONTENT_TYPE_JSON,
authForm authForm
) )
.then(r => { .then(response => {
let response = JSON.parse(r.request['response']);
if (response.type === DataEvent.REQUEST_LAME) { if (response.type === DataEvent.REQUEST_LAME) {
notify.alert(response.message, false); notify.alert(response.message, false);
} else { } else {
@ -74,8 +73,7 @@ export default class Base {
e.preventDefault(); e.preventDefault();
let setUpForm = data.formDataToJSON(document.getElementById('init-form')); let setUpForm = data.formDataToJSON(document.getElementById('init-form'));
api.request(API_INIT, DataEvent.API_INIT, REQUEST_TYPE_POST, CONTENT_TYPE_JSON, setUpForm) api.request(API_INIT, DataEvent.API_INIT, REQUEST_TYPE_POST, CONTENT_TYPE_JSON, setUpForm)
.then(r => { .then(response => {
let response = JSON.parse(r.request['response']);
if (response.type === DataEvent.API_INIT_LAME) { if (response.type === DataEvent.API_INIT_LAME) {
notify.alert(response.message, false); notify.alert(response.message, false);
} else { } else {

@ -29,7 +29,7 @@ export default class NavIndex {
CONTENT_TYPE_JSON, CONTENT_TYPE_JSON,
data data
).then(response => { ).then(response => {
let r = JSON.parse(response.request['response']); let r = response.response;
if (r.type == DataEvent.MENU_UPDATED) { if (r.type == DataEvent.MENU_UPDATED) {
notify.alert(r.message, true); notify.alert(r.message, true);
} else { } else {
@ -60,8 +60,7 @@ export default class NavIndex {
REQUEST_TYPE_POST, REQUEST_TYPE_POST,
CONTENT_TYPE_JSON, CONTENT_TYPE_JSON,
data data
).then(response => { ).then(r => {
let r = JSON.parse(response.request['response']);
if (r.type == DataEvent.MENU_UPDATED) { if (r.type == DataEvent.MENU_UPDATED) {
notify.alert(r.message, true); notify.alert(r.message, true);
} else { } else {

@ -146,8 +146,7 @@ export default class PostEditor {
CONTENT_TYPE_FORM, CONTENT_TYPE_FORM,
page page
) )
.then(response => { .then(r => {
let r = JSON.parse(response.request['response']);
if ( if (
r.type === DataEvent.PAGE_ERROR || r.type === DataEvent.PAGE_ERROR ||
r.type === DataEvent.API_REQUEST_LAME r.type === DataEvent.API_REQUEST_LAME
@ -252,8 +251,7 @@ export default class PostEditor {
imageData.append('post_image', file, file.name); imageData.append('post_image', file, file.name);
} }
api.request(url, eventType, REQUEST_TYPE_POST, CONTENT_TYPE_FORM, imageData) api.request(url, eventType, REQUEST_TYPE_POST, CONTENT_TYPE_FORM, imageData)
.then(response => { .then(r => {
let r = JSON.parse(response.request['response']);
if (r.type == DataEvent.POST_IMAGE_ADDED) if (r.type == DataEvent.POST_IMAGE_ADDED)
self.editor.notify(EditorEvent.EDITOR_UPLOAD_POST_IMAGE, r.url); self.editor.notify(EditorEvent.EDITOR_UPLOAD_POST_IMAGE, r.url);
}) })

@ -37,8 +37,7 @@ export default class SettingsIndex {
REQUEST_TYPE_POST, REQUEST_TYPE_POST,
CONTENT_TYPE_JSON, CONTENT_TYPE_JSON,
data data
).then(response => { ).then(r => {
let r = JSON.parse(response.request['response']);
if (r.type == DataEvent.SETTINGS_UPDATED) { if (r.type == DataEvent.SETTINGS_UPDATED) {
notify.alert(r.message, true); notify.alert(r.message, true);
} else { } else {
@ -171,9 +170,7 @@ export default class SettingsIndex {
: imageData.append('background_upload', file, file.name); : imageData.append('background_upload', file, file.name);
} }
api.request(url, eventType, REQUEST_TYPE_POST, CONTENT_TYPE_FORM, imageData) api.request(url, eventType, REQUEST_TYPE_POST, CONTENT_TYPE_FORM, imageData)
.then(response => { .then(r => {
//TODO: Move JSON processing to API class
let r = JSON.parse(response.request['response']);
if (r.type == DataEvent.AVATAR_UPLOADED) { if (r.type == DataEvent.AVATAR_UPLOADED) {
notify.alert(r.message, true); notify.alert(r.message, true);
document.getElementById('avatar').src = r.url; document.getElementById('avatar').src = r.url;
@ -197,8 +194,7 @@ export default class SettingsIndex {
CONTENT_TYPE_JSON, CONTENT_TYPE_JSON,
task task
) )
.then(response => { .then(r => {
let r = JSON.parse(response.request['response']);
notify.alert(r.message, true); notify.alert(r.message, true);
}) })
.catch(err => { .catch(err => {

@ -27,8 +27,7 @@ export default class APIUtils {
this.token = null; this.token = null;
//checks backend to see if user is logged in //checks backend to see if user is logged in
//and requests encrypted token for api calls //and requests encrypted token for api calls
this.request(API_STATUS).then(r => { this.request(API_STATUS).then(response => {
let response = JSON.parse(r.request['response']);
if (response.type === DataEvent.API_REQUEST_GOOD) { if (response.type === DataEvent.API_REQUEST_GOOD) {
this.token = response.token; this.token = response.token;
} else { } else {
@ -53,15 +52,11 @@ export default class APIUtils {
request.open(requestType, requestURL, true); request.open(requestType, requestURL, true);
request.onload = () => { request.onload = () => {
if (request.status == 200) { if (request.status == 200) {
resolve({ let response = JSON.parse(request['response']);
request, resolve(response);
eventType
});
} else { } else {
reject({ let error = JSON.parse(request['response']);
request, reject(error);
eventType
});
} }
}; };
if (requestType == REQUEST_TYPE_PUT || requestType == REQUEST_TYPE_POST) { if (requestType == REQUEST_TYPE_PUT || requestType == REQUEST_TYPE_POST) {

Loading…
Cancel
Save