import FipamoAdminAPI from '../../libraries/FipamoAdminAPI.js'; import Maintenance from './MaintenanceManager.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'; } } }