import DataUtils, { REQUEST_TYPE_POST, CONTENT_TYPE_JSON } from './utils/DataUtils'; import * as DataEvent from './events/DataEvent'; import DashManager from './controllers/DashManager'; import Notfications from './ui/Notifications'; const data = new DataUtils(); const notify = new Notfications(); export default class Base { //-------------------------- // constructor //-------------------------- constructor() { this.start(); } //-------------------------- // methods //-------------------------- start() { if (document.getElementById('dash-form')) { document .getElementById('login-btn') .addEventListener('click', e => this.handleLogin(e)); } else { let manager = new DashManager(); } } //-------------------------- // event handlers //-------------------------- handleLogin(e) { e.stopPropagation(); e.preventDefault(); 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) { notify.alert(response.message, false); } else { e.target.innerHTML = response.message; setTimeout(() => { window.location = '/@/dashboard'; }, 500); } }) .catch(err => { //console.log(err); }); } }