From 5ad1118244c608d67989ff02341a2c3a20c0dc85 Mon Sep 17 00:00:00 2001 From: Ro Date: Thu, 1 Apr 2021 12:54:03 -0700 Subject: [PATCH] clean up API, re-connected API to front end, base template tweak --- .eslintrc | 70 +++ .gitignore | 1 + brain/controller/APIControl.inc.php | 20 +- brain/controller/RouteControl.inc.php | 5 +- brain/data/Auth.inc.php | 25 +- brain/data/Settings.inc.php | 2 +- brain/views/dash/_frame.twig | 26 +- brain/views/dash/start.twig | 2 - src/com/Base.js | 240 +++++----- src/com/Start.js | 12 +- src/libraries/FipamoAPI.js | 251 +++++----- src/libraries/FipamoAdminAPI.js | 629 +++++++++++++------------- src/package-lock.json | 23 + src/package.json | 15 + 14 files changed, 737 insertions(+), 584 deletions(-) create mode 100644 .eslintrc create mode 100644 src/package-lock.json create mode 100644 src/package.json diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..a1f75dd --- /dev/null +++ b/.eslintrc @@ -0,0 +1,70 @@ +{ + "parserOptions": { + "ecmaVersion": 7, + "sourceType": "module", + "ecmaFeatures": {} + }, + "rules": { + "constructor-super": 2, + "for-direction": 2, + "getter-return": 2, + "no-case-declarations": 2, + "no-class-assign": 2, + "no-compare-neg-zero": 2, + "no-cond-assign": 2, + "no-console": 1, + "no-const-assign": 2, + "no-constant-condition": 2, + "no-control-regex": 1, + "no-debugger": 2, + "no-delete-var": 2, + "no-dupe-args": 2, + "no-dupe-class-members": 2, + "no-dupe-keys": 2, + "no-duplicate-case": 2, + "no-empty": 2, + "no-empty-character-class": 2, + "no-empty-pattern": 2, + "no-ex-assign": 2, + "no-extra-boolean-cast": 2, + "no-extra-semi": 2, + "no-fallthrough": 2, + "no-func-assign": 2, + "no-global-assign": 2, + "no-inner-declarations": 2, + "no-invalid-regexp": 2, + "no-irregular-whitespace": 2, + "no-mixed-spaces-and-tabs": 2, + "no-new-symbol": 2, + "no-obj-calls": 2, + "no-octal": 2, + "no-redeclare": 2, + "no-regex-spaces": 2, + "no-self-assign": 2, + "no-sparse-arrays": 2, + "no-this-before-super": 2, + "no-undef": 2, + "no-unexpected-multiline": 2, + "no-unreachable": 2, + "no-unsafe-finally": 2, + "no-unsafe-negation": 2, + "no-unused-labels": 2, + "no-unused-vars": 2, + "no-useless-escape": 1, + "require-yield": 2, + "use-isnan": 2, + "valid-typeof": 2, + "no-duplicate-imports": 2 + }, + "env": { + "node": true, + "browser": true, + "es6": true + }, + "globals": { + "_": false, + "hljs": false, + "Sortable": false, + "Prism": false + } +} \ No newline at end of file diff --git a/.gitignore b/.gitignore index 4264576..85257c2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules/ +src/node_modules/ .sass-cache/ .cache/ .nova/ diff --git a/brain/controller/APIControl.inc.php b/brain/controller/APIControl.inc.php index caad565..c55e7bd 100644 --- a/brain/controller/APIControl.inc.php +++ b/brain/controller/APIControl.inc.php @@ -6,7 +6,23 @@ use Psr\Http\Message\ServerRequestInterface; class APIControl { - public static function start( + public static function get( + ServerRequestInterface $request, + ResponseInterface $response, + array $args + ): ResponseInterface { + switch (isset($args["third"]) ? $args["third"] : "none") { + case "status": + $result = Auth::status(); + break; + default: + break; + } + + $response->getBody()->write(json_encode($result)); + return $response->withHeader("Content-Type", "application/json"); + } + public static function post( ServerRequestInterface $request, ResponseInterface $response, array $args @@ -24,7 +40,6 @@ class APIControl switch (isset($args["third"]) ? $args["third"] : "none") { case "login": $result = Auth::login($body); - break; case "logout": $result = Auth::logout($body); @@ -38,7 +53,6 @@ class APIControl } $response->getBody()->write(json_encode($result)); - return $response->withHeader("Content-Type", "application/json"); } } diff --git a/brain/controller/RouteControl.inc.php b/brain/controller/RouteControl.inc.php index 8b80ec8..b207b1f 100644 --- a/brain/controller/RouteControl.inc.php +++ b/brain/controller/RouteControl.inc.php @@ -17,6 +17,9 @@ class RouteControl case "dashboard": return DashControl::start($request, $response, $args); break; + case "api": + return APIControl::get($request, $response, $args); + break; default: return IndexControl::start($request, $response, $args); break; @@ -30,7 +33,7 @@ class RouteControl ): ResponseInterface { switch (isset($args["first"]) ? $args["first"] : "index") { case "api": - return APIControl::start($request, $response, $args); + return APIControl::post($request, $response, $args); break; default: //echo "YES"; diff --git a/brain/data/Auth.inc.php b/brain/data/Auth.inc.php index 38c4851..8dda0f0 100644 --- a/brain/data/Auth.inc.php +++ b/brain/data/Auth.inc.php @@ -19,6 +19,25 @@ class Auth //return $this->secret; } + public static function status() + { + $result = []; + if (Session::active()) { + $result = [ + "message" => "Authorized", + "type" => "apiUseAuthorized", + "token" => Session::get("token"), + ]; + } else { + $result = [ + "message" => "Not Authorized", + "type" => "apiUseNotAuthorized", + ]; + } + + return $result; + } + public static function login($who) { //grab member list @@ -47,19 +66,19 @@ class Auth $result = [ "message" => "Welcome back", - "type" => "TASK_LOGIN", + "type" => "requestGood", ]; } else { $result = [ "message" => "Check your password, sport", - "type" => "TASK_LOGIN", + "type" => "requestLame", ]; } } else { //if name is not found $result = [ "message" => "Need to see some id, champ", - "type" => "TASK_LOGIN", + "type" => "requestLame", ]; } return $result; diff --git a/brain/data/Settings.inc.php b/brain/data/Settings.inc.php index d540bce..0ff27c1 100644 --- a/brain/data/Settings.inc.php +++ b/brain/data/Settings.inc.php @@ -17,7 +17,7 @@ class Settings ); } - public function getFolks($key) + public function getFolks($key = null) { if (isset($key)) { $member = Session::get("member"); diff --git a/brain/views/dash/_frame.twig b/brain/views/dash/_frame.twig index 8605cac..47cf39d 100644 --- a/brain/views/dash/_frame.twig +++ b/brain/views/dash/_frame.twig @@ -13,14 +13,15 @@
- +
- +
- +
+

@@ -41,24 +42,7 @@
- + {% block javascripts %}{% endblock %} \ No newline at end of file diff --git a/brain/views/dash/start.twig b/brain/views/dash/start.twig index d142948..6ddd994 100644 --- a/brain/views/dash/start.twig +++ b/brain/views/dash/start.twig @@ -11,8 +11,6 @@ {% block mainContent %}
- STATUS: - {{ status }} {% if status %} DASH INDEX {% else %} diff --git a/src/com/Base.js b/src/com/Base.js index 52a8f6e..b8d4f35 100644 --- a/src/com/Base.js +++ b/src/com/Base.js @@ -1,9 +1,9 @@ -import FipamoApi from '../libraries/FipamoAPI'; -import FipamoAdminAPI from '../libraries/FipamoAdminAPI'; -import DataUitls from './utils/DataUtils'; -import * as DataEvent from './events/DataEvent'; -import DashManager from './controllers/DashManager'; -import Notfications from './ui/Notifications'; +import FipamoApi from "../libraries/FipamoAPI"; +import FipamoAdminAPI from "../libraries/FipamoAdminAPI"; +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(); @@ -11,119 +11,125 @@ const data = new DataUitls(); const notify = new Notfications(); export default class Base { - //-------------------------- - // constructor - //-------------------------- - constructor() { - this.start(); - } + //-------------------------- + // constructor + //-------------------------- + constructor() { + this.start(); + } - //-------------------------- - // methods - //-------------------------- - start() { - if (document.getElementById('dash-form') || document.getElementById('dash-init')) { - 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('dash-form')) { - 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 { - new DashManager(); - } - } - //-------------------------- - // event handlers - //-------------------------- - handleLogin(e) { - e.stopPropagation(); - e.preventDefault(); - let authForm = data.formDataToJSON(document.getElementById('login')); - notify.alert('Looking, hold up', null); - api.login(authForm) - .then(response => { - if (response.type === DataEvent.REQUEST_LAME) { - notify.alert(response.message, false); - } else { - notify.alert(response.message, true); - e.target.innerHTML = response.message; - setTimeout(() => { - window.location = '/@/dashboard'; - }, 500); - } - }) - .catch(err => { - notify.alert(err, false); - }); - } + //-------------------------- + // methods + //-------------------------- + start() { + if ( + 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)); + } + 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)); + document + .getElementById("blog-restore") + .addEventListener("click", (e) => this.handleRestore(e)); + } + } else { + new DashManager(); + } + } + //-------------------------- + // event handlers + //-------------------------- + handleLogin(e) { + e.stopPropagation(); + e.preventDefault(); + let authForm = data.formDataToJSON(document.getElementById("login")); + notify.alert("Looking, hold up", null); + api + .login(authForm) + .then((response) => { + if (response.type === DataEvent.REQUEST_LAME) { + notify.alert(response.message, false); + } else { + notify.alert(response.message, true); + 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.init(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'; - }, 700); - } - }) - .catch(err => { - notify.alert(err, false); - }); - } - handleRestore(e) { - e.stopPropagation(); - e.preventDefault(); - var form = document.getElementById('init-restore'); - admin - .handleInitRestore(form) - .then(response => { - if (response.type === DataEvent.REQUEST_LAME) { - notify.alert(response.message, false); - } else { - notify.alert(response.message, true); - setTimeout(() => { - //window.location = '/@/dashboard'; - }, 700); - } - }) - .catch(err => { - notify.alert(err, false); - }); - } - handleOptions(e) { - e.stopPropagation(); - e.preventDefault(); - let init = document.getElementById('dash-init'); - let restore = document.getElementById('dash-restore'); - if (e.target.id === 'init-switch-restore') { - init.style.display = 'none'; - init.style.visibility = 'hidden'; + handleSetup(e) { + e.stopPropagation(); + e.preventDefault(); + let setUpForm = data.formDataToJSON(document.getElementById("init-form")); + api + .init(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"; + }, 700); + } + }) + .catch((err) => { + notify.alert(err, false); + }); + } + handleRestore(e) { + e.stopPropagation(); + e.preventDefault(); + var form = document.getElementById("init-restore"); + admin + .handleInitRestore(form) + .then((response) => { + if (response.type === DataEvent.REQUEST_LAME) { + notify.alert(response.message, false); + } else { + notify.alert(response.message, true); + setTimeout(() => { + //window.location = '/@/dashboard'; + }, 700); + } + }) + .catch((err) => { + notify.alert(err, false); + }); + } + handleOptions(e) { + e.stopPropagation(); + e.preventDefault(); + let init = document.getElementById("dash-init"); + let restore = document.getElementById("dash-restore"); + if (e.target.id === "init-switch-restore") { + init.style.display = "none"; + init.style.visibility = "hidden"; - restore.style.display = 'block'; - restore.style.visibility = 'visible'; - } else { - init.style.display = 'block'; - init.style.visibility = 'visible'; + restore.style.display = "block"; + restore.style.visibility = "visible"; + } else { + init.style.display = "block"; + init.style.visibility = "visible"; - restore.style.display = 'none'; - restore.style.visibility = 'hidden'; - } - } + restore.style.display = "none"; + restore.style.visibility = "hidden"; + } + } } diff --git a/src/com/Start.js b/src/com/Start.js index b843adb..167671c 100644 --- a/src/com/Start.js +++ b/src/com/Start.js @@ -1,9 +1,9 @@ -import Base from './Base'; +import Base from "./Base"; document.addEventListener( - 'DOMContentLoaded', - function () { - new Base(); - }, - false + "DOMContentLoaded", + function () { + new Base(); + }, + false ); diff --git a/src/libraries/FipamoAPI.js b/src/libraries/FipamoAPI.js index 48adf2a..bf4cebe 100644 --- a/src/libraries/FipamoAPI.js +++ b/src/libraries/FipamoAPI.js @@ -1,121 +1,138 @@ -export const REQUEST_TYPE_POST = 'POST'; -export const REQUEST_TYPE_GET = 'GET'; -export const REQUEST_TYPE_PUT = 'PUT'; -export const REQUEST_TYPE_DELETE = 'DELETE'; -export const CONTENT_TYPE_JSON = 'json'; -export const CONTENT_TYPE_FORM = 'x-www-form-urlencoded'; -export const API_STATUS = '/api/v1/auth/status'; -export const API_INIT = '/api/v1/auth/init'; -export const API_LOGIN = '/api/v1/auth/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 const REQUEST_TYPE_POST = "POST"; +export const REQUEST_TYPE_GET = "GET"; +export const REQUEST_TYPE_PUT = "PUT"; +export const REQUEST_TYPE_DELETE = "DELETE"; +export const CONTENT_TYPE_JSON = "json"; +export const CONTENT_TYPE_FORM = "x-www-form-urlencoded"; +export const API_STATUS = "/api/v1/status"; +export const API_INIT = "/api/v1/init"; +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 { - //-------------------------- - // constructor - //-------------------------- - constructor() {} - //-------------------------- - // public - //-------------------------- - login(data) { - return new Promise((resolve, reject) => { - this._request( - API_LOGIN, - DataEvent.AUTH_STATUS, - REQUEST_TYPE_POST, - CONTENT_TYPE_JSON, - data - ) - .then(result => { - resolve(result); - }) - .catch(err => { - reject(err); - }); - }); - } - init(data) { - return new Promise((resolve, reject) => { - this._request(API_INIT, DataEvent.API_INIT, REQUEST_TYPE_POST, CONTENT_TYPE_JSON, data) - .then(result => { - resolve(result); - }) - .catch(err => { - reject(err); - }); - }); - } - getPages(num) { - let pageNum = num; - if (pageNum === null || pageNum === '' || !pageNum) pageNum = 1; - return new Promise((resolve, reject) => { - this._request(API_GET_PAGES + '/' + pageNum, DataEvent.API_GET_PAGES, REQUEST_TYPE_GET) - .then(result => { - resolve(result); - }) - .catch(err => { - reject(err); - }); - }); - } + //-------------------------- + // constructor + //-------------------------- + constructor() {} + //-------------------------- + // public + //-------------------------- + login(data) { + return new Promise((resolve, reject) => { + this._request( + API_LOGIN, + DataEvent.AUTH_STATUS, + REQUEST_TYPE_POST, + CONTENT_TYPE_JSON, + data + ) + .then((result) => { + resolve(result); + }) + .catch((err) => { + reject(err); + }); + }); + } + init(data) { + return new Promise((resolve, reject) => { + this._request( + API_INIT, + DataEvent.API_INIT, + REQUEST_TYPE_POST, + CONTENT_TYPE_JSON, + data + ) + .then((result) => { + resolve(result); + }) + .catch((err) => { + reject(err); + }); + }); + } + getPages(num) { + let pageNum = num; + if (pageNum === null || pageNum === "" || !pageNum) pageNum = 1; + return new Promise((resolve, reject) => { + this._request( + API_GET_PAGES + "/" + pageNum, + DataEvent.API_GET_PAGES, + REQUEST_TYPE_GET + ) + .then((result) => { + resolve(result); + }) + .catch((err) => { + reject(err); + }); + }); + } - getPage(id) { - return new Promise((resolve, reject) => { - this._request(API_GET_PAGE + '/' + id, DataEvent.API_GET_PAGES, REQUEST_TYPE_GET) - .then(result => { - resolve(result); - }) - .catch(err => { - reject(err); - }); - }); - } - //-------------------------- - // private - //-------------------------- - _request( - requestURL, - eventType, - requestType = REQUEST_TYPE_GET, - contentType = CONTENT_TYPE_JSON, - requestData = null - ) { - var self = this; - return new Promise(function (resolve, reject) { - var request = new XMLHttpRequest(); - request.upload.onprogress = self.handleLoadProgress; - request.open(requestType, requestURL, true); - request.onload = () => { - if (request.status == 200) { - let response = JSON.parse(request['response']); - resolve(response); - } else { - let error = JSON.parse(request['response']); - reject(error); - } - }; - if (requestType == REQUEST_TYPE_PUT || requestType == REQUEST_TYPE_POST) { - switch (contentType) { - case CONTENT_TYPE_JSON: - request.setRequestHeader('Content-type', 'application/' + contentType); - request.send(JSON.stringify(requestData)); - break; - case CONTENT_TYPE_FORM: - request.send(requestData); - break; - } - } else { - request.send(); - } - }); - } + getPage(id) { + return new Promise((resolve, reject) => { + this._request( + API_GET_PAGE + "/" + id, + DataEvent.API_GET_PAGES, + REQUEST_TYPE_GET + ) + .then((result) => { + resolve(result); + }) + .catch((err) => { + reject(err); + }); + }); + } + //-------------------------- + // private + //-------------------------- + _request( + requestURL, + eventType, + requestType = REQUEST_TYPE_GET, + contentType = CONTENT_TYPE_JSON, + requestData = null + ) { + var self = this; + return new Promise(function (resolve, reject) { + var request = new XMLHttpRequest(); + request.upload.onprogress = self.handleLoadProgress; + request.open(requestType, requestURL, true); + request.onload = () => { + if (request.status == 200) { + let response = JSON.parse(request["response"]); + resolve(response); + } else { + let error = JSON.parse(request["response"]); + reject(error); + } + }; + if (requestType == REQUEST_TYPE_PUT || requestType == REQUEST_TYPE_POST) { + switch (contentType) { + case CONTENT_TYPE_JSON: + request.setRequestHeader( + "Content-type", + "application/" + contentType + ); + request.send(JSON.stringify(requestData)); + break; + case CONTENT_TYPE_FORM: + request.send(requestData); + break; + } + } else { + request.send(); + } + }); + } - //-------------------------- - // event handlers - //-------------------------- - handleLoadProgress(e) { - this.percentComplete = Math.ceil((e.loaded / e.total) * 100); - //pass element to display request progress - } + //-------------------------- + // event handlers + //-------------------------- + handleLoadProgress(e) { + this.percentComplete = Math.ceil((e.loaded / e.total) * 100); + //pass element to display request progress + } } diff --git a/src/libraries/FipamoAdminAPI.js b/src/libraries/FipamoAdminAPI.js index d34b1d7..9304123 100644 --- a/src/libraries/FipamoAdminAPI.js +++ b/src/libraries/FipamoAdminAPI.js @@ -1,325 +1,328 @@ -export const REQUEST_TYPE_POST = 'POST'; -export const REQUEST_TYPE_GET = 'GET'; -export const REQUEST_TYPE_PUT = 'PUT'; -export const REQUEST_TYPE_DELETE = 'DELETE'; -export const TASK_PAGE_CREATE = 'createNewPage'; -export const TASK_PAGE_EDIT = 'editPage'; -export const TASK_PAGE_DELETE = 'deletePage'; -export const CONTENT_TYPE_JSON = 'json'; -export const CONTENT_TYPE_FORM = 'x-www-form-urlencoded'; -export const API_STATUS = '/api/v1/auth/status'; -export const API_GET_NAV = '/api/settings/nav'; -export const API_NEW_PAGE = '/api/v1/page/write/new'; -export const API_EDIT_PAGE = '/api/v1/page/write'; -export const API_DELETE_PAGE = '/api/v1/page/delete'; -export const API_IMAGE_UPLOAD = '/api/v1/page/add-post-image'; -export const API_SETTINGS_SYNC = '/api/v1/settings/sync'; -export const API_UPLOAD_AVATAR = '/api/v1/settings/add-avatar'; -export const API_UPLOAD_BACKGROUND = '/api/v1/settings/add-feature-background'; -export const API_PUBLISH_PAGES = '/api/v1/settings/publish-pages'; -export const API_NAV_SYNC = '/api/v1/settings/nav-sync'; -export const API_REINDEX_PAGES = '/api/v1/settings/reindex'; -export const API_CREATE_BACKUP = '/api/v1/backup/create'; -export const API_DOWNLOAD_BACKUP = '/api/v1/backup/download'; -export const API_RESTORE_BACKUP = '/api/v1/backup/restore'; -export const API_INIT_RESTORE_BACKUP = '/api/v1/backup/init-restore'; -export const API_SEND_MAIL = '/api/v1/mailer'; -import * as DataEvent from '../com/events/DataEvent'; +export const REQUEST_TYPE_POST = "POST"; +export const REQUEST_TYPE_GET = "GET"; +export const REQUEST_TYPE_PUT = "PUT"; +export const REQUEST_TYPE_DELETE = "DELETE"; +export const TASK_PAGE_CREATE = "createNewPage"; +export const TASK_PAGE_EDIT = "editPage"; +export const TASK_PAGE_DELETE = "deletePage"; +export const CONTENT_TYPE_JSON = "json"; +export const CONTENT_TYPE_FORM = "x-www-form-urlencoded"; +export const API_STATUS = "/api/v1/status"; +export const API_GET_NAV = "/api/settings/nav"; +export const API_NEW_PAGE = "/api/v1/page/write/new"; +export const API_EDIT_PAGE = "/api/v1/page/write"; +export const API_DELETE_PAGE = "/api/v1/page/delete"; +export const API_IMAGE_UPLOAD = "/api/v1/page/add-post-image"; +export const API_SETTINGS_SYNC = "/api/v1/settings/sync"; +export const API_UPLOAD_AVATAR = "/api/v1/settings/add-avatar"; +export const API_UPLOAD_BACKGROUND = "/api/v1/settings/add-feature-background"; +export const API_PUBLISH_PAGES = "/api/v1/settings/publish-pages"; +export const API_NAV_SYNC = "/api/v1/settings/nav-sync"; +export const API_REINDEX_PAGES = "/api/v1/settings/reindex"; +export const API_CREATE_BACKUP = "/api/v1/backup/create"; +export const API_DOWNLOAD_BACKUP = "/api/v1/backup/download"; +export const API_RESTORE_BACKUP = "/api/v1/backup/restore"; +export const API_INIT_RESTORE_BACKUP = "/api/v1/backup/init-restore"; +export const API_SEND_MAIL = "/api/v1/mailer"; +import * as DataEvent from "../com/events/DataEvent"; export default class APIUtils { - //-------------------------- - // constructor - //-------------------------- - constructor() { - this.percentComplete = 0; - this.token = null; - //checks backend to see if user is logged in - //and requests encrypted token for api calls - this._request(API_STATUS).then(response => { - if (response.type === DataEvent.API_REQUEST_GOOD) { - this.token = response.token; - } else { - //don't set token - } - }); - } - //-------------------------- - // public - //-------------------------- - syncSettings(data) { - return new Promise((resolve, reject) => { - this._request( - API_SETTINGS_SYNC, - DataEvent.API_SETTINGS_WRITE, - REQUEST_TYPE_POST, - CONTENT_TYPE_JSON, - data - ) - .then(result => { - resolve(result); - }) - .catch(err => { - reject(err); - }); - }); - } - imageUpload(type, files) { - return new Promise((resolve, reject) => { - let url = ''; - switch (type) { - case 'avatar-upload': - url = API_UPLOAD_AVATAR; - break; - case 'background-upload': - url = API_UPLOAD_BACKGROUND; - break; - default: - url = API_IMAGE_UPLOAD; - break; - } - var imageData = new FormData(); - for (var i = 0; i < files.length; i++) { - var file = files[i]; - // Check the file type. - if (!file.type.match('image.*')) { - continue; - } - if (type === 'avatar-upload') { - imageData.append('avatar_upload', file, file.name); - } else if (type === 'background-upload') { - imageData.append('background_upload', file, file.name); - } else { - imageData.append('post_image', file, file.name); - } - } - this._request( - url, - DataEvent.API_IMAGES_UPLOAD, - REQUEST_TYPE_POST, - CONTENT_TYPE_FORM, - imageData - ) - .then(r => { - resolve(r); - }) - .catch(err => { - reject(err); - }); - }); - } - publishSite(data) { - return new Promise((resolve, reject) => { - this._request( - API_PUBLISH_PAGES, - DataEvent.API_RENDER_PAGES, - REQUEST_TYPE_POST, - CONTENT_TYPE_JSON, - data - ) - .then(result => { - resolve(result); - }) - .catch(err => { - reject(err); - }); - }); - } + //-------------------------- + // constructor + //-------------------------- + constructor() { + this.percentComplete = 0; + this.token = null; + //checks backend to see if user is logged in + //and requests encrypted token for api calls + this._request(API_STATUS).then((response) => { + if (response.type === DataEvent.API_REQUEST_GOOD) { + this.token = response.token; + } else { + //don't set token + } + }); + } + //-------------------------- + // public + //-------------------------- + syncSettings(data) { + return new Promise((resolve, reject) => { + this._request( + API_SETTINGS_SYNC, + DataEvent.API_SETTINGS_WRITE, + REQUEST_TYPE_POST, + CONTENT_TYPE_JSON, + data + ) + .then((result) => { + resolve(result); + }) + .catch((err) => { + reject(err); + }); + }); + } + imageUpload(type, files) { + return new Promise((resolve, reject) => { + let url = ""; + switch (type) { + case "avatar-upload": + url = API_UPLOAD_AVATAR; + break; + case "background-upload": + url = API_UPLOAD_BACKGROUND; + break; + default: + url = API_IMAGE_UPLOAD; + break; + } + var imageData = new FormData(); + for (var i = 0; i < files.length; i++) { + var file = files[i]; + // Check the file type. + if (!file.type.match("image.*")) { + continue; + } + if (type === "avatar-upload") { + imageData.append("avatar_upload", file, file.name); + } else if (type === "background-upload") { + imageData.append("background_upload", file, file.name); + } else { + imageData.append("post_image", file, file.name); + } + } + this._request( + url, + DataEvent.API_IMAGES_UPLOAD, + REQUEST_TYPE_POST, + CONTENT_TYPE_FORM, + imageData + ) + .then((r) => { + resolve(r); + }) + .catch((err) => { + reject(err); + }); + }); + } + publishSite(data) { + return new Promise((resolve, reject) => { + this._request( + API_PUBLISH_PAGES, + DataEvent.API_RENDER_PAGES, + REQUEST_TYPE_POST, + CONTENT_TYPE_JSON, + data + ) + .then((result) => { + resolve(result); + }) + .catch((err) => { + reject(err); + }); + }); + } - pageActions(task, data) { - let url, event, content; - switch (task) { - case TASK_PAGE_CREATE: - url = API_NEW_PAGE; - event = DataEvent.API_PAGE_WRITE; - content = CONTENT_TYPE_FORM; - break; - case TASK_PAGE_EDIT: - url = API_EDIT_PAGE; - event = DataEvent.API_PAGE_WRITE; - content = CONTENT_TYPE_FORM; - break; + pageActions(task, data) { + let url, event, content; + switch (task) { + case TASK_PAGE_CREATE: + url = API_NEW_PAGE; + event = DataEvent.API_PAGE_WRITE; + content = CONTENT_TYPE_FORM; + break; + case TASK_PAGE_EDIT: + url = API_EDIT_PAGE; + event = DataEvent.API_PAGE_WRITE; + content = CONTENT_TYPE_FORM; + break; - case TASK_PAGE_DELETE: - url = API_DELETE_PAGE; - event = DataEvent.API_PAGE_DELETE; - content = CONTENT_TYPE_JSON; - break; + case TASK_PAGE_DELETE: + url = API_DELETE_PAGE; + event = DataEvent.API_PAGE_DELETE; + content = CONTENT_TYPE_JSON; + break; - default: - break; - } + default: + break; + } - return new Promise((resolve, reject) => { - this._request(url, event, REQUEST_TYPE_POST, content, data) - .then(result => { - resolve(result); - }) - .catch(err => { - reject(err); - }); - }); - } + return new Promise((resolve, reject) => { + this._request(url, event, REQUEST_TYPE_POST, content, data) + .then((result) => { + resolve(result); + }) + .catch((err) => { + reject(err); + }); + }); + } - syncNav(data) { - return new Promise((resolve, reject) => { - this._request( - API_NAV_SYNC, - DataEvent.API_SETTINGS_WRITE, - REQUEST_TYPE_POST, - CONTENT_TYPE_JSON, - data - ) - .then(result => { - resolve(result); - }) - .catch(err => { - reject(err); - }); - }); - } - sendMail(message) { - return new Promise((resolve, reject) => { - this._request( - API_SEND_MAIL, - DataEvent.SEND_MAIL, - REQUEST_TYPE_POST, - CONTENT_TYPE_JSON, - message - ) - .then(result => { - resolve(result); - }) - .catch(err => { - reject(err); - }); - }); - } - handleBackup(id, files) { - return new Promise((resolve, reject) => { - var url, event, method, type, data; + syncNav(data) { + return new Promise((resolve, reject) => { + this._request( + API_NAV_SYNC, + DataEvent.API_SETTINGS_WRITE, + REQUEST_TYPE_POST, + CONTENT_TYPE_JSON, + data + ) + .then((result) => { + resolve(result); + }) + .catch((err) => { + reject(err); + }); + }); + } + sendMail(message) { + return new Promise((resolve, reject) => { + this._request( + API_SEND_MAIL, + DataEvent.SEND_MAIL, + REQUEST_TYPE_POST, + CONTENT_TYPE_JSON, + message + ) + .then((result) => { + resolve(result); + }) + .catch((err) => { + reject(err); + }); + }); + } + handleBackup(id, files) { + return new Promise((resolve, reject) => { + var url, event, method, type, data; - if (id === 'create-backup') { - url = API_CREATE_BACKUP; - event = DataEvent.API_BACKUP_CREATE; - method = REQUEST_TYPE_POST; - type = CONTENT_TYPE_JSON; - data = { task: 'create_backup' }; - } else { - url = API_RESTORE_BACKUP; - event = DataEvent.API_BACKUP_RESTORE; - method = REQUEST_TYPE_POST; - type = CONTENT_TYPE_FORM; - data = new FormData(); - for (var i = 0; i < files.length; i++) { - var file = files[i]; - // Check the file type. - if (!file.type.match('application.zip')) { - continue; - } - data.append('backup_upload', file, file.name); - } - } - this._request(url, event, method, type, data) - .then(result => { - resolve(result); - }) - .catch(err => { - reject(err); - }); - }); - } - handleInitRestore(form) { - return new Promise((resolve, reject) => { - var url, event, method, type, data; + if (id === "create-backup") { + url = API_CREATE_BACKUP; + event = DataEvent.API_BACKUP_CREATE; + method = REQUEST_TYPE_POST; + type = CONTENT_TYPE_JSON; + data = { task: "create_backup" }; + } else { + url = API_RESTORE_BACKUP; + event = DataEvent.API_BACKUP_RESTORE; + method = REQUEST_TYPE_POST; + type = CONTENT_TYPE_FORM; + data = new FormData(); + for (var i = 0; i < files.length; i++) { + var file = files[i]; + // Check the file type. + if (!file.type.match("application.zip")) { + continue; + } + data.append("backup_upload", file, file.name); + } + } + this._request(url, event, method, type, data) + .then((result) => { + resolve(result); + }) + .catch((err) => { + reject(err); + }); + }); + } + handleInitRestore(form) { + return new Promise((resolve, reject) => { + var url, event, method, type, data; - url = API_INIT_RESTORE_BACKUP; - event = DataEvent.API_BACKUP_RESTORE; - method = REQUEST_TYPE_POST; - type = CONTENT_TYPE_FORM; - data = new FormData(form); - this._request(url, event, method, type, data) - .then(result => { - resolve(result); - }) - .catch(err => { - reject(err); - }); - }); - } + url = API_INIT_RESTORE_BACKUP; + event = DataEvent.API_BACKUP_RESTORE; + method = REQUEST_TYPE_POST; + type = CONTENT_TYPE_FORM; + data = new FormData(form); + this._request(url, event, method, type, data) + .then((result) => { + resolve(result); + }) + .catch((err) => { + reject(err); + }); + }); + } - handleReindex(data) { - return new Promise((resolve, reject) => { - this._request( - API_REINDEX_PAGES, - DataEvent.API_REINDEX_PAGES, - REQUEST_TYPE_POST, - CONTENT_TYPE_JSON, - data - ) - .then(result => { - resolve(result); - }) - .catch(err => { - reject(err); - }); - }); - } - //-------------------------- - // private - //-------------------------- - _request( - requestURL, - eventType, - requestType = REQUEST_TYPE_GET, - contentType = CONTENT_TYPE_JSON, - requestData = null - ) { - var self = this; - return new Promise(function (resolve, reject) { - var request = new XMLHttpRequest(); - request.upload.onprogress = self.handleLoadProgress; - request.open(requestType, requestURL, true); - request.onload = () => { - if (request.status == 200) { - let response = JSON.parse(request['response']); - resolve(response); - } else { - let error = JSON.parse(request['response']); - reject(error); - } - }; - if (requestType == REQUEST_TYPE_PUT || requestType == REQUEST_TYPE_POST) { - if ( - eventType === DataEvent.API_PAGE_WRITE || - eventType === DataEvent.API_IMAGES_UPLOAD || - eventType === DataEvent.API_SETTINGS_WRITE || - eventType === DataEvent.API_PAGE_DELETE || - eventType === DataEvent.API_RENDER_PAGES || - eventType === DataEvent.API_BACKUP_CREATE || - eventType === DataEvent.API_BACKUP_RESTORE || - eventType === DataEvent.API_REINDEX_PAGES - ) - request.setRequestHeader('x-access-token', self.token); + handleReindex(data) { + return new Promise((resolve, reject) => { + this._request( + API_REINDEX_PAGES, + DataEvent.API_REINDEX_PAGES, + REQUEST_TYPE_POST, + CONTENT_TYPE_JSON, + data + ) + .then((result) => { + resolve(result); + }) + .catch((err) => { + reject(err); + }); + }); + } + //-------------------------- + // private + //-------------------------- + _request( + requestURL, + eventType, + requestType = REQUEST_TYPE_GET, + contentType = CONTENT_TYPE_JSON, + requestData = null + ) { + var self = this; + return new Promise(function (resolve, reject) { + var request = new XMLHttpRequest(); + request.upload.onprogress = self.handleLoadProgress; + request.open(requestType, requestURL, true); + request.onload = () => { + if (request.status == 200) { + let response = JSON.parse(request["response"]); + resolve(response); + } else { + let error = JSON.parse(request["response"]); + reject(error); + } + }; + if (requestType == REQUEST_TYPE_PUT || requestType == REQUEST_TYPE_POST) { + if ( + eventType === DataEvent.API_PAGE_WRITE || + eventType === DataEvent.API_IMAGES_UPLOAD || + eventType === DataEvent.API_SETTINGS_WRITE || + eventType === DataEvent.API_PAGE_DELETE || + eventType === DataEvent.API_RENDER_PAGES || + eventType === DataEvent.API_BACKUP_CREATE || + eventType === DataEvent.API_BACKUP_RESTORE || + eventType === DataEvent.API_REINDEX_PAGES + ) + request.setRequestHeader("x-access-token", self.token); - switch (contentType) { - case CONTENT_TYPE_JSON: - request.setRequestHeader('Content-type', 'application/' + contentType); - request.send(JSON.stringify(requestData)); - break; - case CONTENT_TYPE_FORM: - request.send(requestData); - break; - } - } else { - request.send(); - } - }); - } + switch (contentType) { + case CONTENT_TYPE_JSON: + request.setRequestHeader( + "Content-type", + "application/" + contentType + ); + request.send(JSON.stringify(requestData)); + break; + case CONTENT_TYPE_FORM: + request.send(requestData); + break; + } + } else { + request.send(); + } + }); + } - //-------------------------- - // event handlers - //-------------------------- - handleLoadProgress(e) { - this.percentComplete = Math.ceil((e.loaded / e.total) * 100); - //pass element to display request progress - } + //-------------------------- + // event handlers + //-------------------------- + handleLoadProgress(e) { + this.percentComplete = Math.ceil((e.loaded / e.total) * 100); + //pass element to display request progress + } } diff --git a/src/package-lock.json b/src/package-lock.json new file mode 100644 index 0000000..397733c --- /dev/null +++ b/src/package-lock.json @@ -0,0 +1,23 @@ +{ + "name": "codekit-project", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "animejs": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/animejs/-/animejs-3.2.1.tgz", + "integrity": "sha512-sWno3ugFryK5nhiDm/2BKeFCpZv7vzerWUcUPyAZLDhMek3+S/p418ldZJbJXo5ZUOpfm2kP2XRO4NJcULMy9A==" + }, + "caret-pos": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/caret-pos/-/caret-pos-2.0.0.tgz", + "integrity": "sha512-cOIiBS1SjzXg+LXSiQAzGg89dHDKq/y4c30+tB5hkVN7GbtXh1BNypOmjti4LwAWQrvP4y+bNG7RJFxLGoL3bA==" + }, + "sortablejs": { + "version": "1.13.0", + "resolved": "https://registry.npmjs.org/sortablejs/-/sortablejs-1.13.0.tgz", + "integrity": "sha512-RBJirPY0spWCrU5yCmWM1eFs/XgX2J5c6b275/YyxFRgnzPhKl/TDeU2hNR8Dt7ITq66NRPM4UlOt+e5O4CFHg==" + } + } +} diff --git a/src/package.json b/src/package.json new file mode 100644 index 0000000..f99247f --- /dev/null +++ b/src/package.json @@ -0,0 +1,15 @@ +{ + "name": "fipamo-dash", + "version": "1.2.4", + "description": "Front end script for the most chill blog framework ever.", + "scripts": {}, + "author": "Are0h", + "license": "UNLICENSED", + "repository": "https://code.playvicio.us/Are0h/Fipamo", + "private": true, + "dependencies": { + "animejs": "^3.2.1", + "caret-pos": "^2.0.0", + "sortablejs": "^1.13.0" + } +}