From 018bfc410b0437bf045769c29802aed58f51c99c Mon Sep 17 00:00:00 2001 From: Ro Date: Sat, 23 Nov 2019 16:56:28 -0800 Subject: [PATCH] request promise way too large, switching back to custom request method --- src/com/Base.js | 49 ++++++++++++++++++------------- src/com/controllers/PostEditor.js | 14 ++------- src/com/ui/TextEditor.js | 1 - src/com/utils/DataUtils.js | 42 +++++++++++++++++++++++++- 4 files changed, 71 insertions(+), 35 deletions(-) diff --git a/src/com/Base.js b/src/com/Base.js index 0326283..fb10426 100644 --- a/src/com/Base.js +++ b/src/com/Base.js @@ -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 { - window.location = '/@/dashboard'; + e.target.innerHTML = response.message; + setTimeout(() => { + window.location = '/@/dashboard'; + }, 500); } }) .catch(err => { - //console.log('ERROR', err); + //console.log(err); }); } } diff --git a/src/com/controllers/PostEditor.js b/src/com/controllers/PostEditor.js index 3c9265e..9898afc 100644 --- a/src/com/controllers/PostEditor.js +++ b/src/com/controllers/PostEditor.js @@ -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(); } } //-------------------------- diff --git a/src/com/ui/TextEditor.js b/src/com/ui/TextEditor.js index 11a22a6..338a62c 100644 --- a/src/com/ui/TextEditor.js +++ b/src/com/ui/TextEditor.js @@ -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; diff --git a/src/com/utils/DataUtils.js b/src/com/utils/DataUtils.js index 10dad10..2d49f79 100644 --- a/src/com/utils/DataUtils.js +++ b/src/com/utils/DataUtils.js @@ -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');