From 957a389b8b6c6b63a186cc065546046d4be38a95 Mon Sep 17 00:00:00 2001 From: Ro Date: Sun, 28 Jun 2020 14:25:14 -0700 Subject: [PATCH] consolidated fipamo api admin methods --- brain/data/Book.js | 2 + src/com/controllers/PageEditor.js | 27 ++++---- src/com/controllers/SettingsIndex.js | 2 +- src/libraries/FipamoAPI.js | 97 ++++++++++++++++------------ 4 files changed, 68 insertions(+), 60 deletions(-) diff --git a/brain/data/Book.js b/brain/data/Book.js index 709e342..59e6d08 100644 --- a/brain/data/Book.js +++ b/brain/data/Book.js @@ -191,9 +191,11 @@ export default class Book { this.getPage(id) .then(page => { let body = _.mapValues(page.metadata); + body.content = page.content; body.deleted = moment(Date.now()).format(); body.menu = false; + self.editPage(body, body.uuid, DataEvent.API_PAGE_WRITE, user) .then(() => { let item = { diff --git a/src/com/controllers/PageEditor.js b/src/com/controllers/PageEditor.js index c1693db..6349389 100644 --- a/src/com/controllers/PageEditor.js +++ b/src/com/controllers/PageEditor.js @@ -1,5 +1,9 @@ //TOOLS -import FipamoAPI, { API_NEW_PAGE, API_EDIT_PAGE } from '../../libraries/FipamoAPI'; +import FipamoAPI, { + TASK_PAGE_CREATE, + TASK_PAGE_EDIT, + TASK_PAGE_DELETE +} from '../../libraries/FipamoAPI'; import * as DataEvent from '../events/DataEvent'; import PageActions from '../actions/PageActions'; import * as EditorEvent from '../events/EditorEvent'; @@ -125,12 +129,12 @@ export default class PostEditor { switch (e) { case EditorEvent.EDITOR_SAVE: case EditorEvent.EDITOR_UPDATE: - var apiUrl = ''; - e === EditorEvent.EDITOR_SAVE ? (apiUrl = API_NEW_PAGE) : (apiUrl = API_EDIT_PAGE); + var task = ''; + e === EditorEvent.EDITOR_SAVE ? (task = TASK_PAGE_CREATE) : (task = TASK_PAGE_EDIT); new PageActions() .collectInfo(document.getElementById('featured-image-upload').files[0]) .then(page => { - api.pageEdit(apiUrl, page) + api.pageActions(task, page) .then(r => { if ( r.type === DataEvent.PAGE_ERROR || @@ -158,7 +162,7 @@ export default class PostEditor { } if (confirm("AYE! You know you're deleting this post, right?")) { let id = { id: this.postUUID }; - api.pageDelete(id) + api.pageActions(TASK_PAGE_DELETE, id) .then(() => { window.location = '/@/dashboard/page/list/'; }) @@ -218,22 +222,13 @@ export default class PostEditor { } handleImageUpload(type, files) { let self = this; - 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; - } - imageData.append('post_image', file, file.name); - } - api.pageImageUpload(imageData) + api.imageUpload(type, files) .then(r => { if (r.type == DataEvent.POST_IMAGE_ADDED) self.editor.notify(EditorEvent.EDITOR_UPLOAD_POST_IMAGE, r.url); }) .catch(() => { - //console.log(err) + //console.log('ERROR', err); }); } } diff --git a/src/com/controllers/SettingsIndex.js b/src/com/controllers/SettingsIndex.js index ffd3180..e773319 100644 --- a/src/com/controllers/SettingsIndex.js +++ b/src/com/controllers/SettingsIndex.js @@ -141,7 +141,7 @@ export default class SettingsIndex { } } handleImageUpload(type, files) { - api.settingsImageUpload(type, files) + api.imageUpload(type, files) .then(r => { if (r.type == DataEvent.AVATAR_UPLOADED) { notify.alert(r.message, true); diff --git a/src/libraries/FipamoAPI.js b/src/libraries/FipamoAPI.js index b1a4304..063c5ff 100644 --- a/src/libraries/FipamoAPI.js +++ b/src/libraries/FipamoAPI.js @@ -2,6 +2,9 @@ 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'; @@ -83,11 +86,20 @@ export default class APIUtils { }); }); } - settingsImageUpload(type, files) { + imageUpload(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); + 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]; @@ -95,11 +107,21 @@ export default class APIUtils { if (!file.type.match('image.*')) { continue; } - type == 'avatar-upload' - ? imageData.append('avatar_upload', file, file.name) - : imageData.append('background_upload', file, file.name); + 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, eventType, REQUEST_TYPE_POST, CONTENT_TYPE_FORM, imageData) + this._request( + url, + DataEvent.API_IMAGES_UPLOAD, + REQUEST_TYPE_POST, + CONTENT_TYPE_FORM, + imageData + ) .then(r => { resolve(r); }) @@ -126,43 +148,32 @@ export default class APIUtils { }); } - pageEdit(url, data) { + 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; + + default: + break; + } + 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 - ) + this._request(url, event, REQUEST_TYPE_POST, content, data) .then(result => { resolve(result); })