fipamo/src/com/Base.js

91 lines
2.2 KiB
JavaScript
Raw Normal View History

import FipamoApi, {
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
API_LOGIN,
API_INIT
} from '../libraries/FipamoAPI';
import DataUitls from './utils/DataUtils';
import * as DataEvent from './events/DataEvent';
import DashManager from './controllers/DashManager';
import Notfications from './ui/Notifications';
const api = new FipamoApi();
const data = new DataUitls();
const notify = new Notfications();
export default class Base {
//--------------------------
// constructor
//--------------------------
constructor() {
this.start();
}
//--------------------------
// methods
//--------------------------
start() {
if (document.getElementById('dash-form') || document.getElementById('dash-init')) {
if (document.getElementById('dash-form')) {
document
.getElementById('login-btn')
.addEventListener('click', e => this.handleLogin(e));
} else {
document
.getElementById('init-blog')
.addEventListener('click', e => this.handleSetup(e));
}
} else {
new DashManager();
}
}
//--------------------------
// event handlers
//--------------------------
handleLogin(e) {
e.stopPropagation();
e.preventDefault();
let authForm = data.formDataToJSON(document.getElementById('login'));
api.request(
API_LOGIN,
DataEvent.AUTH_STATUS,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
authForm
)
.then(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 => {
notify.alert(err, false);
});
}
handleSetup(e) {
e.stopPropagation();
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(response => {
if (response.type === DataEvent.API_INIT_LAME) {
notify.alert(response.message, false);
} else {
notify.alert(response.message, true);
setTimeout(() => {
//window.location = '/@/dashboard';
}, 500);
}
})
.catch(err => {
notify.alert(err, false);
});
}
}