request promise way too large, switching back to custom request method
This commit is contained in:
parent
7897a8bca9
commit
018bfc410b
4 changed files with 71 additions and 35 deletions
|
@ -1,10 +1,8 @@
|
|||
import DataUtils from './utils/DataUtils';
|
||||
import Manager from './controllers/DashManager';
|
||||
import request from 'request-promise';
|
||||
import * as DataEvent from '../com/events/DataEvent';
|
||||
//import DBUtils from '../../../../../brain/utils/tools/DBUtils';
|
||||
const utils = new DataUtils();
|
||||
const settings = require('../../site/settings.json');
|
||||
import DataUtils, { REQUEST_TYPE_POST, CONTENT_TYPE_JSON } from './utils/DataUtils';
|
||||
import * as DataEvent from './events/DataEvent';
|
||||
import DashManager from './controllers/DashManager';
|
||||
|
||||
const data = new DataUtils();
|
||||
|
||||
export default class Base {
|
||||
//--------------------------
|
||||
|
@ -23,7 +21,7 @@ export default class Base {
|
|||
.getElementById('login-btn')
|
||||
.addEventListener('click', e => this.handleLogin(e));
|
||||
} else {
|
||||
let manager = new Manager();
|
||||
//let dm = new DashManager();
|
||||
}
|
||||
}
|
||||
//--------------------------
|
||||
|
@ -32,23 +30,32 @@ export default class Base {
|
|||
handleLogin(e) {
|
||||
e.stopPropagation();
|
||||
e.preventDefault();
|
||||
let authForm = utils.formDataToJSON(document.getElementById('login'));
|
||||
|
||||
request({
|
||||
method: 'POST',
|
||||
uri: settings.base_url + '/api/v1/auth/login',
|
||||
body: authForm,
|
||||
json: true
|
||||
})
|
||||
.then(res => {
|
||||
if (res.type === DataEvent.REQUEST_LAME) {
|
||||
e.target.innerHTML = res.message;
|
||||
let authForm = data.formDataToJSON(document.getElementById('login'));
|
||||
data.request(
|
||||
'/api/v1/auth/login',
|
||||
DataEvent.AUTH_STATUS,
|
||||
REQUEST_TYPE_POST,
|
||||
CONTENT_TYPE_JSON,
|
||||
authForm
|
||||
)
|
||||
.then(r => {
|
||||
let response = JSON.parse(r.request['response']);
|
||||
if (response.type === DataEvent.REQUEST_LAME) {
|
||||
e.target.innerHTML = response.message;
|
||||
setTimeout(() => {
|
||||
e.target.innerHTML = 'TRY IT AGAIN, HOMIE';
|
||||
}, 1500);
|
||||
//console.log('NOPE', response.message);
|
||||
//self.dashManager = new DashManager();
|
||||
} else {
|
||||
e.target.innerHTML = response.message;
|
||||
setTimeout(() => {
|
||||
window.location = '/@/dashboard';
|
||||
}, 500);
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
//console.log('ERROR', err);
|
||||
//console.log(err);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@ import PostActions from '../actions/PostActions';
|
|||
import * as EditorEvent from '../../../src/com/events/EditorEvent';
|
||||
import TinyDatePicker from 'tiny-date-picker';
|
||||
import TextEditor from '../../../src/com/ui/TextEditor';
|
||||
import DBUtils, { FINAL_KEY } from '../../../src/com/utils/DBUtils';
|
||||
export default class PostEditor {
|
||||
//TODO - FIX POST FEATURE URLS IN DB
|
||||
|
||||
|
@ -14,22 +13,11 @@ export default class PostEditor {
|
|||
//--------------------------
|
||||
constructor() {
|
||||
let self = this;
|
||||
this.dataUtils = new DataUtils();
|
||||
this.urlPieces = document.URL.split('/');
|
||||
this.dbUtils = new DBUtils();
|
||||
this.post = [];
|
||||
this.postID = null;
|
||||
if (document.getElementById('post-edit-index').getAttribute('data-index')) {
|
||||
this.postID = document.getElementById('post-edit-index').getAttribute('data-index');
|
||||
this.dbUtils
|
||||
.getPost(this.postID)
|
||||
.then(body => {
|
||||
self.post = body.post;
|
||||
this.start();
|
||||
})
|
||||
.catch();
|
||||
} else {
|
||||
this.start();
|
||||
}
|
||||
if (document.getElementById('edit-post-text')) {
|
||||
this.editor = new TextEditor(
|
||||
|
@ -71,6 +59,8 @@ export default class PostEditor {
|
|||
return self.dateUtils.getDate('origin', date);
|
||||
}
|
||||
});
|
||||
|
||||
this.start();
|
||||
}
|
||||
}
|
||||
//--------------------------
|
||||
|
|
|
@ -15,7 +15,6 @@ class TextEditor extends EventEmitter {
|
|||
constructor(textEditor, scrollLimit) {
|
||||
super();
|
||||
hljs.initHighlightingOnLoad();
|
||||
this.dateUtils = new DateUtils();
|
||||
this.textEditor = textEditor;
|
||||
this.fixLimit = scrollLimit;
|
||||
this.caretPos = null;
|
||||
|
|
|
@ -15,7 +15,46 @@ export default class DataUtils extends EventEmitter {
|
|||
//--------------------------
|
||||
// methods
|
||||
//--------------------------
|
||||
|
||||
request(
|
||||
requestURL,
|
||||
eventType,
|
||||
requestType = REQUEST_TYPE_GET,
|
||||
contentType = CONTENT_TYPE_JSON,
|
||||
requestData = null
|
||||
) {
|
||||
var self = this;
|
||||
return new Promise(function(resolve, reject) {
|
||||
var request = new XMLHttpRequest();
|
||||
request.upload.onprogress = self.handleLoadProgress;
|
||||
request.open(requestType, requestURL, true);
|
||||
request.onload = () => {
|
||||
if (request.status == 200) {
|
||||
resolve({
|
||||
request,
|
||||
eventType
|
||||
});
|
||||
} else {
|
||||
reject({
|
||||
request,
|
||||
eventType
|
||||
});
|
||||
}
|
||||
};
|
||||
if (requestType == REQUEST_TYPE_PUT || requestType == REQUEST_TYPE_POST) {
|
||||
switch (contentType) {
|
||||
case CONTENT_TYPE_JSON:
|
||||
request.setRequestHeader('Content-type', 'application/' + contentType);
|
||||
request.send(JSON.stringify(requestData));
|
||||
break;
|
||||
case CONTENT_TYPE_FORM:
|
||||
request.send(requestData);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
request.send();
|
||||
}
|
||||
});
|
||||
}
|
||||
imgLoad(url) {
|
||||
'use strict';
|
||||
// Create new promise with the Promise() constructor;
|
||||
|
@ -73,6 +112,7 @@ export default class DataUtils extends EventEmitter {
|
|||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a function to convert the serialize and convert the form data to JSON
|
||||
* @param : $('#form_example');
|
||||
|
|
Loading…
Reference in a new issue