From 75b4cb9ac26feff65b9f4d2c00b312c61c46102e Mon Sep 17 00:00:00 2001 From: Ro Date: Fri, 26 Jun 2020 14:05:20 -0700 Subject: [PATCH] changed Fipamo API methods to be based on tasks, not api call type --- brain/api/v1/settings.js | 2 +- src/com/Base.js | 17 +-- src/com/controllers/NavIndex.js | 22 +--- src/com/controllers/PageEditor.js | 31 +----- src/com/controllers/SettingsIndex.js | 42 +------ src/libraries/FipamoAPI.js | 161 ++++++++++++++++++++++++++- 6 files changed, 172 insertions(+), 103 deletions(-) diff --git a/brain/api/v1/settings.js b/brain/api/v1/settings.js index 2853c6e..bf0a20d 100644 --- a/brain/api/v1/settings.js +++ b/brain/api/v1/settings.js @@ -179,7 +179,7 @@ router.post('/add-feature-background', background_upload, (req, res) => { .then(settings => { var path = req.files[0].path; var image = path.substr(7, path.length); - settings.background = '/' + image; + settings.global.background = '/' + image; fs.writeJson('site/settings.json', settings); res.json({ type: DataEvent.SITE_BACKGROUND_UPLOADED, diff --git a/src/com/Base.js b/src/com/Base.js index 65a1e60..e938466 100644 --- a/src/com/Base.js +++ b/src/com/Base.js @@ -1,9 +1,4 @@ -import FipamoApi, { - REQUEST_TYPE_POST, - CONTENT_TYPE_JSON, - API_LOGIN, - API_INIT -} from '../libraries/FipamoAPI'; +import FipamoApi from '../libraries/FipamoAPI'; import DataUitls from './utils/DataUtils'; import * as DataEvent from './events/DataEvent'; import DashManager from './controllers/DashManager'; @@ -46,13 +41,7 @@ export default class Base { 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 - ) + api.login(authForm) .then(response => { if (response.type === DataEvent.REQUEST_LAME) { notify.alert(response.message, false); @@ -72,7 +61,7 @@ export default class Base { 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) + api.init(setUpForm) .then(response => { if (response.type === DataEvent.API_INIT_LAME) { notify.alert(response.message, false); diff --git a/src/com/controllers/NavIndex.js b/src/com/controllers/NavIndex.js index 6bcc155..284a365 100644 --- a/src/com/controllers/NavIndex.js +++ b/src/com/controllers/NavIndex.js @@ -1,8 +1,4 @@ -import FipamoAPI, { - REQUEST_TYPE_POST, - CONTENT_TYPE_JSON, - API_NAV_SYNC -} from '../../libraries/FipamoAPI'; +import FipamoAPI from '../../libraries/FipamoAPI'; import NavActions from '../actions/NavActions'; import * as DataEvent from '../events/DataEvent'; import Notifications from '../ui/Notifications'; @@ -22,13 +18,7 @@ export default class NavIndex { Sortable.create(document.getElementById('nav-pages'), { onUpdate: () => { new NavActions().syncMenu().then(data => { - api.request( - API_NAV_SYNC, - DataEvent.API_SETTINGS_WRITE, - REQUEST_TYPE_POST, - CONTENT_TYPE_JSON, - data - ).then(r => { + api.syncNav(data).then(r => { if (r.type == DataEvent.MENU_UPDATED) { notify.alert(r.message, true); } else { @@ -53,13 +43,7 @@ export default class NavIndex { id = e.target.getAttribute('data-id'); new NavActions().removeItem(id); new NavActions().syncMenu().then(data => { - api.request( - API_NAV_SYNC, - DataEvent.API_SETTINGS_WRITE, - REQUEST_TYPE_POST, - CONTENT_TYPE_JSON, - data - ).then(r => { + api.syncNav(data).then(r => { if (r.type == DataEvent.MENU_UPDATED) { notify.alert(r.message, true); } else { diff --git a/src/com/controllers/PageEditor.js b/src/com/controllers/PageEditor.js index 9c6980c..c1693db 100644 --- a/src/com/controllers/PageEditor.js +++ b/src/com/controllers/PageEditor.js @@ -1,13 +1,5 @@ //TOOLS -import FipamoAPI, { - REQUEST_TYPE_POST, - CONTENT_TYPE_FORM, - CONTENT_TYPE_JSON, - API_NEW_PAGE, - API_EDIT_PAGE, - API_DELETE_PAGE, - API_IMAGE_UPLOAD -} from '../../libraries/FipamoAPI'; +import FipamoAPI, { API_NEW_PAGE, API_EDIT_PAGE } from '../../libraries/FipamoAPI'; import * as DataEvent from '../events/DataEvent'; import PageActions from '../actions/PageActions'; import * as EditorEvent from '../events/EditorEvent'; @@ -137,15 +129,8 @@ export default class PostEditor { e === EditorEvent.EDITOR_SAVE ? (apiUrl = API_NEW_PAGE) : (apiUrl = API_EDIT_PAGE); new PageActions() .collectInfo(document.getElementById('featured-image-upload').files[0]) - .then(page => { - api.request( - apiUrl, - DataEvent.API_PAGE_WRITE, - REQUEST_TYPE_POST, - CONTENT_TYPE_FORM, - page - ) + api.pageEdit(apiUrl, page) .then(r => { if ( r.type === DataEvent.PAGE_ERROR || @@ -173,13 +158,7 @@ export default class PostEditor { } if (confirm("AYE! You know you're deleting this post, right?")) { let id = { id: this.postUUID }; - api.request( - API_DELETE_PAGE, - DataEvent.API_PAGE_DELETE, - REQUEST_TYPE_POST, - CONTENT_TYPE_JSON, - id - ) + api.pageDelete(id) .then(() => { window.location = '/@/dashboard/page/list/'; }) @@ -238,8 +217,6 @@ export default class PostEditor { } } handleImageUpload(type, files) { - let url = API_IMAGE_UPLOAD; - let eventType = DataEvent.API_IMAGES_UPLOAD; let self = this; var imageData = new FormData(); for (var i = 0; i < files.length; i++) { @@ -250,7 +227,7 @@ export default class PostEditor { } imageData.append('post_image', file, file.name); } - api.request(url, eventType, REQUEST_TYPE_POST, CONTENT_TYPE_FORM, imageData) + api.pageImageUpload(imageData) .then(r => { if (r.type == DataEvent.POST_IMAGE_ADDED) self.editor.notify(EditorEvent.EDITOR_UPLOAD_POST_IMAGE, r.url); diff --git a/src/com/controllers/SettingsIndex.js b/src/com/controllers/SettingsIndex.js index b94257d..ffd3180 100644 --- a/src/com/controllers/SettingsIndex.js +++ b/src/com/controllers/SettingsIndex.js @@ -1,13 +1,5 @@ import SettingsActions from '../actions/SettingsActions'; -import FipamoAPI, { - REQUEST_TYPE_POST, - CONTENT_TYPE_FORM, - CONTENT_TYPE_JSON, - API_SETTINGS_SYNC, - API_UPLOAD_AVATAR, - API_UPLOAD_BACKGROUND, - API_PUBLISH_PAGES -} from '../../libraries/FipamoAPI'; +import FipamoAPI from '../../libraries/FipamoAPI'; import * as DataEvent from '../../../src/com/events/DataEvent'; import Mailer from '../actions/Mailer'; import Notifications from '../ui/Notifications'; @@ -31,13 +23,7 @@ export default class SettingsIndex { new SettingsActions() .getInfo() .then(data => { - api.request( - API_SETTINGS_SYNC, - DataEvent.API_SETTINGS_WRITE, - REQUEST_TYPE_POST, - CONTENT_TYPE_JSON, - data - ).then(r => { + api.syncSettings(data).then(r => { if (r.type == DataEvent.SETTINGS_UPDATED) { notify.alert(r.message, true); } else { @@ -155,21 +141,7 @@ export default class SettingsIndex { } } handleImageUpload(type, files) { - let url = ''; - let eventType = DataEvent.API_IMAGES_UPLOAD; - type == 'avatar-upload' ? (url = API_UPLOAD_AVATAR) : (url = API_UPLOAD_BACKGROUND); - 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; - } - type == 'avatar-upload' - ? imageData.append('avatar_upload', file, file.name) - : imageData.append('background_upload', file, file.name); - } - api.request(url, eventType, REQUEST_TYPE_POST, CONTENT_TYPE_FORM, imageData) + api.settingsImageUpload(type, files) .then(r => { if (r.type == DataEvent.AVATAR_UPLOADED) { notify.alert(r.message, true); @@ -187,13 +159,7 @@ export default class SettingsIndex { e.preventDefault(); e.stopPropagation(); let task = { task: 'publish all pages' }; - api.request( - API_PUBLISH_PAGES, - DataEvent.API_RENDER_PAGES, - REQUEST_TYPE_POST, - CONTENT_TYPE_JSON, - task - ) + api.publishSite(task) .then(r => { notify.alert(r.message, true); }) diff --git a/src/libraries/FipamoAPI.js b/src/libraries/FipamoAPI.js index 74bee86..b1a4304 100644 --- a/src/libraries/FipamoAPI.js +++ b/src/libraries/FipamoAPI.js @@ -27,7 +27,7 @@ export default class APIUtils { 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 => { + this._request(API_STATUS).then(response => { if (response.type === DataEvent.API_REQUEST_GOOD) { this.token = response.token; } else { @@ -36,10 +36,163 @@ export default class APIUtils { }); } //-------------------------- - // methods + // public //-------------------------- - //TODO: Make 'private' class - request( + 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); + }); + }); + } + 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); + }); + }); + } + settingsImageUpload(type, files) { + return new Promise((resolve, reject) => { + let url = ''; + let eventType = DataEvent.API_IMAGES_UPLOAD; + type == 'avatar-upload' ? (url = API_UPLOAD_AVATAR) : (url = API_UPLOAD_BACKGROUND); + 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; + } + type == 'avatar-upload' + ? imageData.append('avatar_upload', file, file.name) + : imageData.append('background_upload', file, file.name); + } + this._request(url, eventType, 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); + }); + }); + } + + pageEdit(url, data) { + return new Promise((resolve, reject) => { + this._request(url, DataEvent.API_PAGE_WRITE, REQUEST_TYPE_POST, CONTENT_TYPE_FORM, data) + .then(result => { + resolve(result); + }) + .catch(err => { + reject(err); + }); + }); + } + pageDelete(data) { + return new Promise((resolve, reject) => { + this._request( + API_DELETE_PAGE, + DataEvent.API_PAGE_DELETE, + REQUEST_TYPE_POST, + CONTENT_TYPE_JSON, + data + ) + .then(result => { + resolve(result); + }) + .catch(err => { + reject(err); + }); + }); + } + pageImageUpload(file) { + return new Promise((resolve, reject) => { + this._request( + API_IMAGE_UPLOAD, + DataEvent.API_IMAGES_UPLOAD, + REQUEST_TYPE_POST, + CONTENT_TYPE_FORM, + file + ) + .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); + }); + }); + } + //-------------------------- + // private + //-------------------------- + _request( requestURL, eventType, requestType = REQUEST_TYPE_GET,