fipamo/public/assets/scripts/dash/app/controllers/InitController.js
ro c5afbb9131
API Decouplng Part 2
removed all remaining API requests from the front end and removed the
FipamoAdminAPI js file, changing it to ContentRequest as it now handles
posting data to the system directly, authenticating it self by checking
the embedded CSRF token that regulary rotates

also renamed MaintenanceManager to Maintenance request and moved it to
the same dir as ContentRequest as they are both libraries that connect
to the backend
2024-07-17 15:08:10 -06:00

127 lines
3.4 KiB
JavaScript

import ContentRequest from '../../libraries/ContentRequest.js';
import Maintenance from '../../libraries/MaintenanceRequest.js';
import DataUitls from '../utils/DataUtils.js';
import * as DataEvent from '../events/DataEvent.js';
import Notfications from '../ui/Notifications.js';
const data = new DataUitls();
const notify = new Notfications();
export default class InitController {
//--------------------------
// constructor
//--------------------------
constructor() {
this.processing = false;
this.start();
}
//--------------------------
// methods
//--------------------------
start() {
if (document.getElementById('login') || document.querySelector('.site-restore')) {
var options = document.getElementsByClassName('init-option');
for (let index = 0; index < options.length; index++) {
options[index].addEventListener('click', e => this.handleOptions(e));
}
if (document.getElementById('login')) {
document
.getElementById('login-btn')
.addEventListener('click', e => this.handleLogin(e));
} else {
document
.getElementById('init-blog')
.addEventListener('click', e => this.handleSetup(e));
document
.getElementById('blog-restore')
.addEventListener('click', e => this.handleRestore(e));
}
} else if (document.getElementById('dash-reset')) {
document
.getElementById('get-secret-btn')
.addEventListener('click', e => this.handleReset(e));
document
.getElementById('reset-btn')
.addEventListener('click', e => this.handleReset(e));
}
}
//--------------------------
// event handlers
//--------------------------
handleSetup(e) {
if (this.processing) return;
let self = this;
e.stopPropagation();
e.preventDefault();
let setUpForm = data.formDataToJSON(document.getElementById('init-form'));
let mm = new Maintenance();
this.processing = true;
mm.create(setUpForm)
.then(response => {
if (response.type === DataEvent.API_INIT_LAME) {
self.processing = false;
e.target.innerHTML = response.message;
} else {
self.processing = false;
e.target.innerHTML = response.message;
setTimeout(() => {
window.location = '/dashboard';
}, 700);
}
})
.catch(err => {
self.processing = false;
//notify.alert(err, false);
});
}
handleRestore(e) {
if (this.processing) return;
let self = this;
e.stopPropagation();
e.preventDefault();
let mm = new Maintenance();
var form = document.getElementById('init-restore');
this.processing = true;
mm.restore(form)
.then(response => {
if (response.type === DataEvent.REQUEST_LAME) {
self.processing = false;
e.target.innerHTML = response.message;
} else {
self.processing = false;
e.target.innerHTML = response.message;
setTimeout(() => {
window.location = '/dashboard';
}, 1500);
}
})
.catch(err => {
self.processing = false;
e.target.innerHTML = err;
});
}
handleOptions(e) {
e.stopPropagation();
e.preventDefault();
let init = document.querySelector('.restore-fresh');
let restore = document.querySelector('.restore-backup');
if (e.target.id === 'init-switch-restore') {
init.style.display = 'none';
init.style.visibility = 'hidden';
restore.style.display = 'grid';
restore.style.visibility = 'visible';
} else {
init.style.display = 'grid';
init.style.visibility = 'visible';
restore.style.display = 'none';
restore.style.visibility = 'hidden';
}
}
}