request promise way too large, switching back to custom request method

This commit is contained in:
Ro 2019-11-23 16:56:28 -08:00
parent 7897a8bca9
commit 018bfc410b
4 changed files with 71 additions and 35 deletions

View file

@ -1,10 +1,8 @@
import DataUtils from './utils/DataUtils'; import DataUtils, { REQUEST_TYPE_POST, CONTENT_TYPE_JSON } from './utils/DataUtils';
import Manager from './controllers/DashManager'; import * as DataEvent from './events/DataEvent';
import request from 'request-promise'; import DashManager from './controllers/DashManager';
import * as DataEvent from '../com/events/DataEvent';
//import DBUtils from '../../../../../brain/utils/tools/DBUtils'; const data = new DataUtils();
const utils = new DataUtils();
const settings = require('../../site/settings.json');
export default class Base { export default class Base {
//-------------------------- //--------------------------
@ -23,7 +21,7 @@ export default class Base {
.getElementById('login-btn') .getElementById('login-btn')
.addEventListener('click', e => this.handleLogin(e)); .addEventListener('click', e => this.handleLogin(e));
} else { } else {
let manager = new Manager(); //let dm = new DashManager();
} }
} }
//-------------------------- //--------------------------
@ -32,23 +30,32 @@ export default class Base {
handleLogin(e) { handleLogin(e) {
e.stopPropagation(); e.stopPropagation();
e.preventDefault(); e.preventDefault();
let authForm = utils.formDataToJSON(document.getElementById('login')); let authForm = data.formDataToJSON(document.getElementById('login'));
data.request(
request({ '/api/v1/auth/login',
method: 'POST', DataEvent.AUTH_STATUS,
uri: settings.base_url + '/api/v1/auth/login', REQUEST_TYPE_POST,
body: authForm, CONTENT_TYPE_JSON,
json: true authForm
}) )
.then(res => { .then(r => {
if (res.type === DataEvent.REQUEST_LAME) { let response = JSON.parse(r.request['response']);
e.target.innerHTML = res.message; 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 { } else {
window.location = '/@/dashboard'; e.target.innerHTML = response.message;
setTimeout(() => {
window.location = '/@/dashboard';
}, 500);
} }
}) })
.catch(err => { .catch(err => {
//console.log('ERROR', err); //console.log(err);
}); });
} }
} }

View file

@ -5,7 +5,6 @@ import PostActions from '../actions/PostActions';
import * as EditorEvent from '../../../src/com/events/EditorEvent'; import * as EditorEvent from '../../../src/com/events/EditorEvent';
import TinyDatePicker from 'tiny-date-picker'; import TinyDatePicker from 'tiny-date-picker';
import TextEditor from '../../../src/com/ui/TextEditor'; import TextEditor from '../../../src/com/ui/TextEditor';
import DBUtils, { FINAL_KEY } from '../../../src/com/utils/DBUtils';
export default class PostEditor { export default class PostEditor {
//TODO - FIX POST FEATURE URLS IN DB //TODO - FIX POST FEATURE URLS IN DB
@ -14,22 +13,11 @@ export default class PostEditor {
//-------------------------- //--------------------------
constructor() { constructor() {
let self = this; let self = this;
this.dataUtils = new DataUtils();
this.urlPieces = document.URL.split('/'); this.urlPieces = document.URL.split('/');
this.dbUtils = new DBUtils();
this.post = []; this.post = [];
this.postID = null; this.postID = null;
if (document.getElementById('post-edit-index').getAttribute('data-index')) { if (document.getElementById('post-edit-index').getAttribute('data-index')) {
this.postID = 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')) { if (document.getElementById('edit-post-text')) {
this.editor = new TextEditor( this.editor = new TextEditor(
@ -71,6 +59,8 @@ export default class PostEditor {
return self.dateUtils.getDate('origin', date); return self.dateUtils.getDate('origin', date);
} }
}); });
this.start();
} }
} }
//-------------------------- //--------------------------

View file

@ -15,7 +15,6 @@ class TextEditor extends EventEmitter {
constructor(textEditor, scrollLimit) { constructor(textEditor, scrollLimit) {
super(); super();
hljs.initHighlightingOnLoad(); hljs.initHighlightingOnLoad();
this.dateUtils = new DateUtils();
this.textEditor = textEditor; this.textEditor = textEditor;
this.fixLimit = scrollLimit; this.fixLimit = scrollLimit;
this.caretPos = null; this.caretPos = null;

View file

@ -15,7 +15,46 @@ export default class DataUtils extends EventEmitter {
//-------------------------- //--------------------------
// methods // 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) { imgLoad(url) {
'use strict'; 'use strict';
// Create new promise with the Promise() constructor; // 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 * Create a function to convert the serialize and convert the form data to JSON
* @param : $('#form_example'); * @param : $('#form_example');