moved API JSON response parsing to API request method

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

@ -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) {

@ -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 {

@ -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 {

@ -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);
})

@ -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 => {

@ -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) {

Loading…
Cancel
Save