From a699fd1926ac4693b9ce1ff2da7cac5d0a6bb308 Mon Sep 17 00:00:00 2001 From: Ro Date: Wed, 14 Apr 2021 18:50:19 -0700 Subject: [PATCH] removed unnecessary api requests --- src/com/Base.js | 9 +- src/com/actions/Mailer.js | 53 ++-- src/com/controllers/NavIndex.js | 126 ++++---- src/com/controllers/PageEditor.js | 9 +- src/com/controllers/SettingsIndex.js | 434 ++++++++++++++------------- src/libraries/FipamoAPI.js | 2 +- 6 files changed, 319 insertions(+), 314 deletions(-) diff --git a/src/com/Base.js b/src/com/Base.js index 32f7a0f..c8e6733 100644 --- a/src/com/Base.js +++ b/src/com/Base.js @@ -4,9 +4,6 @@ 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 admin = new FipamoAdminAPI(); const data = new DataUitls(); const notify = new Notfications(); @@ -26,7 +23,6 @@ export default class Base { document.getElementById("dash-form") || document.getElementById("dash-init") ) { - console.log("GET THAT ID, boss"); var options = document.getElementsByClassName("init-option"); for (let index = 0; index < options.length; index++) { options[index].addEventListener("click", (e) => this.handleOptions(e)); @@ -55,6 +51,7 @@ export default class Base { e.preventDefault(); let authForm = data.formDataToJSON(document.getElementById("login")); notify.alert("Looking, hold up", null); + let api = new FipamoApi(); api .login(authForm) .then((response) => { @@ -93,10 +90,11 @@ export default class Base { notify.alert(err, false); }); } - /** + handleRestore(e) { e.stopPropagation(); e.preventDefault(); + let admin = new FipamoAdminAPI(); var form = document.getElementById("init-restore"); admin .handleInitRestore(form) @@ -114,7 +112,6 @@ export default class Base { notify.alert(err, false); }); } - **/ handleOptions(e) { e.stopPropagation(); e.preventDefault(); diff --git a/src/com/actions/Mailer.js b/src/com/actions/Mailer.js index 498a0e8..486b5cd 100644 --- a/src/com/actions/Mailer.js +++ b/src/com/actions/Mailer.js @@ -1,29 +1,30 @@ -import FipamoAdminAPI from '../../libraries/FipamoAdminAPI'; -import Notficaton from '../ui/Notifications'; -const admin = new FipamoAdminAPI(); +import FipamoAdminAPI from "../../libraries/FipamoAdminAPI"; +import Notficaton from "../ui/Notifications"; const notify = new Notficaton(); export default class Mailer { - //-------------------------- - // constructor - //-------------------------- - constructor() {} - //-------------------------- - // methods - //-------------------------- - sendMail() { - let mailData = { - content: 'This is a test email' - }; - admin - .sendMail(mailData) - .then(result => { - notify.alert(result.message, true); - }) - .catch(err => { - notify.alert(err.message, false); - }); - } - //-------------------------- - // event handlers - //-------------------------- + //-------------------------- + // constructor + //-------------------------- + constructor() { + this.admin = new FipamoAdminAPI(); + } + //-------------------------- + // methods + //-------------------------- + sendMail() { + let mailData = { + content: "This is a test email", + }; + this.admin + .sendMail(mailData) + .then((result) => { + notify.alert(result.message, true); + }) + .catch((err) => { + notify.alert(err.message, false); + }); + } + //-------------------------- + // event handlers + //-------------------------- } diff --git a/src/com/controllers/NavIndex.js b/src/com/controllers/NavIndex.js index 0e4b5d3..a2e932b 100644 --- a/src/com/controllers/NavIndex.js +++ b/src/com/controllers/NavIndex.js @@ -1,64 +1,68 @@ -import FipamoAdminAPI from '../../libraries/FipamoAdminAPI'; -import NavActions from '../actions/NavActions'; -import * as DataEvent from '../events/DataEvent'; -import Notifications from '../ui/Notifications'; -import Sortable from 'sortablejs'; +import FipamoAdminAPI from "../../libraries/FipamoAdminAPI"; +import NavActions from "../actions/NavActions"; +import * as DataEvent from "../events/DataEvent"; +import Notifications from "../ui/Notifications"; +import Sortable from "sortablejs"; const notify = new Notifications(); -const admin = new FipamoAdminAPI(); + export default class NavIndex { - //-------------------------- - // constructor - //-------------------------- - constructor() { - this.start(); - } - //-------------------------- - // methods - //-------------------------- - start() { - Sortable.create(document.getElementById('nav-pages'), { - onUpdate: () => { - new NavActions().syncMenu().then(data => { - notify.alert('Updating Menu', null); - admin.syncNav(data).then(r => { - if (r.type == DataEvent.MENU_UPDATED) { - notify.alert(r.message, true); - } else { - notify.alert(r.message, true); - } - }); - }); - } - }); - var nav = document.querySelectorAll('.nav-btn'); - for (var i = 0, length = nav.length; i < length; i++) { - nav[i].addEventListener('click', e => this.handleNavButton(e), false); - } - } - //-------------------------- - // event handlers - //-------------------------- - handleNavButton(e) { - let id = ''; - switch (e.target.id) { - case 'remove-item': - id = e.target.getAttribute('data-id'); - new NavActions().removeItem(id); - new NavActions().syncMenu().then(data => { - data.remove = e.target.getAttribute('data-uuid'); - notify.alert('Editing Menu', null); - admin.syncNav(data).then(r => { - if (r.type == DataEvent.MENU_UPDATED) { - notify.alert(r.message, true); - } else { - notify.alert(r.message, true); - } - }); - }); - break; - case 'edit-item': - window.location = '/@/dashboard/page/edit/' + e.target.getAttribute('data-id'); - break; - } - } + //-------------------------- + // constructor + //-------------------------- + constructor() { + this.admin = new FipamoAdminAPI(); + this.start(); + } + //-------------------------- + // methods + //-------------------------- + start() { + let self = this; + Sortable.create(document.getElementById("nav-pages"), { + onUpdate: () => { + new NavActions().syncMenu().then((data) => { + notify.alert("Updating Menu", null); + self.admin.syncNav(data).then((r) => { + if (r.type == DataEvent.MENU_UPDATED) { + notify.alert(r.message, true); + } else { + notify.alert(r.message, true); + } + }); + }); + }, + }); + var nav = document.querySelectorAll(".nav-btn"); + for (var i = 0, length = nav.length; i < length; i++) { + nav[i].addEventListener("click", (e) => this.handleNavButton(e), false); + } + } + //-------------------------- + // event handlers + //-------------------------- + handleNavButton(e) { + let id = ""; + let self = this; + switch (e.target.id) { + case "remove-item": + id = e.target.getAttribute("data-id"); + new NavActions().removeItem(id); + new NavActions().syncMenu().then((data) => { + data.remove = e.target.getAttribute("data-uuid"); + notify.alert("Editing Menu", null); + self.admin.syncNav(data).then((r) => { + if (r.type == DataEvent.MENU_UPDATED) { + notify.alert(r.message, true); + } else { + notify.alert(r.message, true); + } + }); + }); + break; + case "edit-item": + window.location = + "/@/dashboard/page/edit/" + e.target.getAttribute("data-id"); + break; + } + } } diff --git a/src/com/controllers/PageEditor.js b/src/com/controllers/PageEditor.js index 1600088..a4f10ea 100644 --- a/src/com/controllers/PageEditor.js +++ b/src/com/controllers/PageEditor.js @@ -10,7 +10,6 @@ import * as EditorEvent from "../events/EditorEvent"; //import TinyDatePicker from 'tiny-date-picker'; import TextEditor from "../ui/TextEditor"; import Notfications from "../ui/Notifications"; -const admin = new FipamoAdminAPI(); const notify = new Notfications(); export default class PostEditor { //-------------------------- @@ -18,6 +17,7 @@ export default class PostEditor { //-------------------------- constructor() { let self = this; + this.admin = new FipamoAdminAPI(); this.urlPieces = document.URL.split("/"); this.post = []; this.postID = null; @@ -136,6 +136,7 @@ export default class PostEditor { : currentOption.setAttribute("data-active", "false"); } handleEditorOptions(e) { + let self = this; switch (e) { case EditorEvent.EDITOR_SAVE: case EditorEvent.EDITOR_UPDATE: @@ -149,7 +150,7 @@ export default class PostEditor { ) .then((page) => { notify.alert("Writing down changes", null); - admin + self.admin .pageActions(task, page) .then((r) => { if ( @@ -178,7 +179,7 @@ export default class PostEditor { } if (confirm("AYE! You know you're deleting this post, right?")) { let id = { id: this.postUUID }; - admin + this.admin .pageActions(TASK_PAGE_DELETE, id) .then(() => { window.location = "/dashboard/pages"; @@ -240,7 +241,7 @@ export default class PostEditor { handleImageUpload(type, files) { let self = this; notify.alert("Uploading Image", null); - admin + this.admin .imageUpload(type, files) .then((r) => { if (r.type == DataEvent.POST_IMAGE_ADDED) diff --git a/src/com/controllers/SettingsIndex.js b/src/com/controllers/SettingsIndex.js index ee3b892..b02eb81 100644 --- a/src/com/controllers/SettingsIndex.js +++ b/src/com/controllers/SettingsIndex.js @@ -1,221 +1,223 @@ -import SettingsActions from '../actions/SettingsActions'; -import FipamoAdminAPI from '../../libraries/FipamoAdminAPI'; -import * as DataEvent from '../../../src/com/events/DataEvent'; -import Mailer from '../actions/Mailer'; -import Notifications from '../ui/Notifications'; -const admin = new FipamoAdminAPI(); +import SettingsActions from "../actions/SettingsActions"; +import FipamoAdminAPI from "../../libraries/FipamoAdminAPI"; +import * as DataEvent from "../../../src/com/events/DataEvent"; +import Mailer from "../actions/Mailer"; +import Notifications from "../ui/Notifications"; const notify = new Notifications(); -const mailer = new Mailer(); export default class SettingsIndex { - //-------------------------- - // constructor - //-------------------------- - constructor() { - this.start(); - } - //-------------------------- - // methods - //-------------------------- - start() { - let self = this; - //handle save button - document.getElementById('save-toggle').addEventListener('click', () => - new SettingsActions() - .getInfo() - .then(data => { - notify.alert('Saving Settings', null); - admin.syncSettings(data).then(r => { - if (r.type == DataEvent.SETTINGS_UPDATED) { - notify.alert(r.message, true); - } else { - notify.alert(r.message, true); - } - }); - }) - .catch(() => { - //console.log(err); - }) - ); - //handle set up image uploads - document.getElementById('avatar').addEventListener('click', () => { - document.getElementById('avatar-upload').click(); - }); - document.getElementById('background').addEventListener('click', () => { - document.getElementById('background-upload').click(); - }); - document.getElementById('restore-backup').addEventListener('click', () => { - document.getElementById('backup-upload').click(); - }); - document.getElementById('avatar-upload').addEventListener( - 'change', - e => { - self.handleImageUpload(e.target.id, e.target.files); - }, - false - ); - document.getElementById('background-upload').addEventListener( - 'change', - e => { - self.handleImageUpload(e.target.id, e.target.files); - }, - false - ); - document.getElementById('backup-upload').addEventListener( - 'change', - e => { - self.handleBackup(e); - }, - false - ); - //handle privacy toggle - //document - //.getElementById('privacy-toggle') - //.addEventListener('click', e => this.togglePrivacy(e)); - document - .getElementById('render-toggle') - .addEventListener('click', e => this.toggleRender(e)); - document.getElementById('send-mail').addEventListener('click', e => this.handleMailer(e)); - document - .getElementById('publish-pages') - .addEventListener('click', e => this.handlePublished(e)); - //handle theme toggle - let themeBtns = document.querySelectorAll('.theme-select'); - for (var i = 0, length = themeBtns.length; i < length; i++) { - themeBtns[i].addEventListener('click', e => this.handleThemes(e)); - } - //handle mail options - let mailBtn = document.querySelectorAll('.mail-option'); - for (i = 0, length = mailBtn.length; i < length; i++) { - mailBtn[i].addEventListener('click', e => this.handleMailOptions(e)); - } - //handle backup - document - .getElementById('create-backup') - .addEventListener('click', e => this.handleBackup(e)); + //-------------------------- + // constructor + //-------------------------- + constructor() { + this.start(); + this.admin = new FipamoAdminAPI(); + } + //-------------------------- + // methods + //-------------------------- + start() { + let self = this; + //handle save button + document.getElementById("save-toggle").addEventListener("click", () => + new SettingsActions() + .getInfo() + .then((data) => { + notify.alert("Saving Settings", null); + self.admin.syncSettings(data).then((r) => { + if (r.type == DataEvent.SETTINGS_UPDATED) { + notify.alert(r.message, true); + } else { + notify.alert(r.message, true); + } + }); + }) + .catch(() => { + //console.log(err); + }) + ); + //handle set up image uploads + document.getElementById("avatar").addEventListener("click", () => { + document.getElementById("avatar-upload").click(); + }); + document.getElementById("background").addEventListener("click", () => { + document.getElementById("background-upload").click(); + }); + document.getElementById("restore-backup").addEventListener("click", () => { + document.getElementById("backup-upload").click(); + }); + document.getElementById("avatar-upload").addEventListener( + "change", + (e) => { + self.handleImageUpload(e.target.id, e.target.files); + }, + false + ); + document.getElementById("background-upload").addEventListener( + "change", + (e) => { + self.handleImageUpload(e.target.id, e.target.files); + }, + false + ); + document.getElementById("backup-upload").addEventListener( + "change", + (e) => { + self.handleBackup(e); + }, + false + ); + //handle privacy toggle + //document + //.getElementById('privacy-toggle') + //.addEventListener('click', e => this.togglePrivacy(e)); + document + .getElementById("render-toggle") + .addEventListener("click", (e) => this.toggleRender(e)); + document + .getElementById("send-mail") + .addEventListener("click", (e) => this.handleMailer(e)); + document + .getElementById("publish-pages") + .addEventListener("click", (e) => this.handlePublished(e)); + //handle theme toggle + let themeBtns = document.querySelectorAll(".theme-select"); + for (var i = 0, length = themeBtns.length; i < length; i++) { + themeBtns[i].addEventListener("click", (e) => this.handleThemes(e)); + } + //handle mail options + let mailBtn = document.querySelectorAll(".mail-option"); + for (i = 0, length = mailBtn.length; i < length; i++) { + mailBtn[i].addEventListener("click", (e) => this.handleMailOptions(e)); + } + //handle backup + document + .getElementById("create-backup") + .addEventListener("click", (e) => this.handleBackup(e)); - document - .getElementById('reindex-pages') - .addEventListener('click', e => this.handleReindex(e)); - } - //-------------------------- - // event handlers - //-------------------------- - togglePrivacy(e) { - e.stopPropagation(); - e.preventDefault(); - if (e.target.getAttribute('data-private') == 'false') { - e.target.setAttribute('data-private', 'true'); - e.target.innerHTML = 'SITE IS PUBLIC'; - } else { - e.target.setAttribute('data-private', 'false'); - e.target.innerHTML = 'SITE IS PRIVATE'; - } - } - toggleRender(e) { - e.stopPropagation(); - e.preventDefault(); - if (e.target.getAttribute('data-render') == 'false') { - e.target.setAttribute('data-render', 'true'); - //e.target.innerHTML = 'RENDER PAGES ON SAVE'; - } else { - e.target.setAttribute('data-render', 'false'); - //e.target.innerHTML = "DON'T RENDER PAGES ON SAVE"; - } - } - handleMailer() { - mailer.sendMail(); - } - handleThemes(e) { - e.stopPropagation(); - e.preventDefault(); - let themes = document.querySelectorAll('.theme-select'); - for (var i = 0, length = themes.length; i < length; i++) { - e.target.id == themes[i].id - ? themes[i].setAttribute('data-enabled', 'true') - : themes[i].setAttribute('data-enabled', 'false'); - } - } - handleMailOptions(e) { - e.preventDefault(); - e.stopPropagation(); - let smtp = document.getElementById('mail-smtp'); - let mailgun = document.getElementById('mail-mg'); - let mail = document.querySelectorAll('.mail-option'); - for (var i = 0, length = mail.length; i < length; i++) { - if (e.target.id == mail[i].id) { - mail[i].setAttribute('data-enabled', 'true'); - if (e.target.id == 'option-smtp') { - smtp.setAttribute('data-enabled', 'true'); - mailgun.setAttribute('data-enabled', 'false'); - } else if (e.target.id == 'option-none') { - smtp.setAttribute('data-enabled', 'false'); - mailgun.setAttribute('data-enabled', 'false'); - } else { - smtp.setAttribute('data-enabled', 'false'); - mailgun.setAttribute('data-enabled', 'true'); - } - } else { - mail[i].setAttribute('data-enabled', 'false'); - } - } - } - handleImageUpload(type, files) { - notify.alert('Uploading Image... ', null); - admin - .imageUpload(type, files) - .then(r => { - if (r.type == DataEvent.AVATAR_UPLOADED) { - notify.alert(r.message, true); - document.getElementById('avatar').src = r.url; - } else { - notify.alert(r.message, true); - document.getElementById('background').src = r.url; - } - }) - .catch(() => { - //console.log(err) - }); - } - handlePublished(e) { - e.preventDefault(); - e.stopPropagation(); - let task = { task: 'publish all pages' }; - notify.alert('Publishing site...', null); - admin - .publishSite(task) - .then(r => { - notify.alert(r.message, true); - }) - .catch(err => { - notify.alert(err, false); - }); - } - handleBackup(e) { - e.preventDefault(); - e.stopPropagation(); - notify.alert('Creating backup', null); - admin - .handleBackup(e.target.id, e.target.files) - .then(r => { - notify.alert(r.message, true); - }) - .catch(err => { - notify.alert(err, false); - }); - } + document + .getElementById("reindex-pages") + .addEventListener("click", (e) => this.handleReindex(e)); + } + //-------------------------- + // event handlers + //-------------------------- + togglePrivacy(e) { + e.stopPropagation(); + e.preventDefault(); + if (e.target.getAttribute("data-private") == "false") { + e.target.setAttribute("data-private", "true"); + e.target.innerHTML = "SITE IS PUBLIC"; + } else { + e.target.setAttribute("data-private", "false"); + e.target.innerHTML = "SITE IS PRIVATE"; + } + } + toggleRender(e) { + e.stopPropagation(); + e.preventDefault(); + if (e.target.getAttribute("data-render") == "false") { + e.target.setAttribute("data-render", "true"); + //e.target.innerHTML = 'RENDER PAGES ON SAVE'; + } else { + e.target.setAttribute("data-render", "false"); + //e.target.innerHTML = "DON'T RENDER PAGES ON SAVE"; + } + } + handleMailer() { + let mailer = new Mailer(); + mailer.sendMail(); + } + handleThemes(e) { + e.stopPropagation(); + e.preventDefault(); + let themes = document.querySelectorAll(".theme-select"); + for (var i = 0, length = themes.length; i < length; i++) { + e.target.id == themes[i].id + ? themes[i].setAttribute("data-enabled", "true") + : themes[i].setAttribute("data-enabled", "false"); + } + } + handleMailOptions(e) { + e.preventDefault(); + e.stopPropagation(); + let smtp = document.getElementById("mail-smtp"); + let mailgun = document.getElementById("mail-mg"); + let mail = document.querySelectorAll(".mail-option"); + for (var i = 0, length = mail.length; i < length; i++) { + if (e.target.id == mail[i].id) { + mail[i].setAttribute("data-enabled", "true"); + if (e.target.id == "option-smtp") { + smtp.setAttribute("data-enabled", "true"); + mailgun.setAttribute("data-enabled", "false"); + } else if (e.target.id == "option-none") { + smtp.setAttribute("data-enabled", "false"); + mailgun.setAttribute("data-enabled", "false"); + } else { + smtp.setAttribute("data-enabled", "false"); + mailgun.setAttribute("data-enabled", "true"); + } + } else { + mail[i].setAttribute("data-enabled", "false"); + } + } + } + handleImageUpload(type, files) { + notify.alert("Uploading Image... ", null); + this.admin + .imageUpload(type, files) + .then((r) => { + if (r.type == DataEvent.AVATAR_UPLOADED) { + notify.alert(r.message, true); + document.getElementById("avatar").src = r.url; + } else { + notify.alert(r.message, true); + document.getElementById("background").src = r.url; + } + }) + .catch(() => { + //console.log(err) + }); + } + handlePublished(e) { + e.preventDefault(); + e.stopPropagation(); + let task = { task: "publish all pages" }; + notify.alert("Publishing site...", null); + this.admin + .publishSite(task) + .then((r) => { + notify.alert(r.message, true); + }) + .catch((err) => { + notify.alert(err, false); + }); + } + handleBackup(e) { + e.preventDefault(); + e.stopPropagation(); + notify.alert("Creating backup", null); + this.admin + .handleBackup(e.target.id, e.target.files) + .then((r) => { + notify.alert(r.message, true); + }) + .catch((err) => { + notify.alert(err, false); + }); + } - handleReindex(e) { - e.preventDefault(); - e.stopPropagation(); - let task = { task: 'cleanup pages indexes' }; - notify.alert('Cleaning up page indexes', null); - admin - .handleReindex(task) - .then(r => { - notify.alert(r.message, true); - }) - .catch(err => { - notify.alert(err, false); - }); - } + handleReindex(e) { + e.preventDefault(); + e.stopPropagation(); + let task = { task: "cleanup pages indexes" }; + notify.alert("Cleaning up page indexes", null); + this.admin + .handleReindex(task) + .then((r) => { + notify.alert(r.message, true); + }) + .catch((err) => { + notify.alert(err, false); + }); + } } diff --git a/src/libraries/FipamoAPI.js b/src/libraries/FipamoAPI.js index bf4cebe..8e092bb 100644 --- a/src/libraries/FipamoAPI.js +++ b/src/libraries/FipamoAPI.js @@ -10,7 +10,7 @@ export const API_LOGIN = "/api/v1/login"; export const API_GET_PAGES = "/api/v1/page/published"; export const API_GET_PAGE = "/api/v1/page/single"; import * as DataEvent from "../com/events/DataEvent"; -export default class APIUtils { +export default class FipamoAPI { //-------------------------- // constructor //--------------------------