From 8052c861bfed3b72aeb13b1e69c0e21fd65f3afa Mon Sep 17 00:00:00 2001 From: Ro Date: Tue, 5 May 2020 12:31:32 -0700 Subject: [PATCH] added page rendering framework to API --- brain/api/v1/pages.js | 2 +- brain/api/v1/settings.js | 19 ++++++++++++++++-- brain/data/Book.js | 13 +++++++++++++ brain/views/settings.pug | 2 ++ src/com/controllers/SettingsIndex.js | 29 +++++++++++++++++++++++++++- src/com/events/DataEvent.js | 1 + 6 files changed, 62 insertions(+), 4 deletions(-) diff --git a/brain/api/v1/pages.js b/brain/api/v1/pages.js index e7c16a6..13bc5eb 100644 --- a/brain/api/v1/pages.js +++ b/brain/api/v1/pages.js @@ -33,7 +33,7 @@ var post_upload = multer({ }).array('post_image'); /** - * Retrives list of Pages + * Retrieves list of Pages * @public */ router.get('/', (req, res) => { diff --git a/brain/api/v1/settings.js b/brain/api/v1/settings.js index 878756a..a563d56 100644 --- a/brain/api/v1/settings.js +++ b/brain/api/v1/settings.js @@ -14,10 +14,10 @@ fs.ensureDir(uploadPath, () => { // dir has now been created, including the directory it is to be placed in }); var storage = multer.diskStorage({ - destination: function(req, file, cb) { + destination: function (req, file, cb) { cb(null, uploadPath); }, - filename: function(req, file, cb) { + filename: function (req, file, cb) { var splice = file.originalname.split(':'); cb(null, splice[0]); } @@ -107,6 +107,21 @@ router.post('/nav-sync', (req, res) => { } }); +router.post('/publish-pages', (req, res) => { + if (req.session.user) { + console.log('PUBLISHING'); + res.json({ + type: DataEvent.API_RENDER_PAGES, + message: 'All Pages Rendered and Published' + }); + } else { + res.json({ + type: DataEvent.REQUEST_LAME, + message: "You're not logged in, champ" + }); + } +}); + /*** UPLOAD AVATAR */ diff --git a/brain/data/Book.js b/brain/data/Book.js index 94b7b41..027503f 100644 --- a/brain/data/Book.js +++ b/brain/data/Book.js @@ -173,6 +173,19 @@ export default class Pages { } }); } + publish() { + return new Promise((resolve, reject) => { + let self = this; + //get pages for rendering + this.getPage() + .then(pages => { + console.log('PAGES', pages); + }) + .catch(err => { + reject(err); + }); + }); + } //-------------------------- // event handlers //-------------------------- diff --git a/brain/views/settings.pug b/brain/views/settings.pug index 58dc46d..1765f73 100644 --- a/brain/views/settings.pug +++ b/brain/views/settings.pug @@ -22,6 +22,8 @@ block main-content | DON'T RENDER PAGES ON SAVE -else | RENDER PAGES ON SAVE + button#publish-pages + | Publish Site input(id="avatar-upload" type="file" name="avatar-upload") #member-settings-2.column label INFO diff --git a/src/com/controllers/SettingsIndex.js b/src/com/controllers/SettingsIndex.js index e02471b..f38e35d 100644 --- a/src/com/controllers/SettingsIndex.js +++ b/src/com/controllers/SettingsIndex.js @@ -1,5 +1,9 @@ import SettingsActions from '../actions/SettingsActions'; -import ApiUtils, { REQUEST_TYPE_POST, CONTENT_TYPE_FORM } from '../../../src/com/utils/APIUtils'; +import ApiUtils, { + REQUEST_TYPE_POST, + CONTENT_TYPE_FORM, + CONTENT_TYPE_JSON +} from '../../../src/com/utils/APIUtils'; import * as DataEvent from '../../../src/com/events/DataEvent'; import Mailer from '../actions/Mailer'; import Notifications from '../ui/Notifications'; @@ -63,6 +67,9 @@ export default class SettingsIndex { .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++) { @@ -155,6 +162,7 @@ export default class SettingsIndex { } api.request(url, eventType, REQUEST_TYPE_POST, CONTENT_TYPE_FORM, imageData) .then(response => { + //TODO: Move JSON processing to API class let r = JSON.parse(response.request['response']); if (r.type == DataEvent.AVATAR_UPLOADED) { notify.alert(r.message, true); @@ -168,4 +176,23 @@ export default class SettingsIndex { //console.log(err) }); } + handlePublished(e) { + e.preventDefault(); + e.stopPropagation(); + let task = { task: 'publish all pages' }; + api.request( + '/api/v1/settings/publish-pages', + DataEvent.API_RENDER_PAGES, + REQUEST_TYPE_POST, + CONTENT_TYPE_JSON, + task + ) + .then(response => { + let r = JSON.parse(response.request['response']); + console.log('RESPONSE', r); + }) + .catch(err => { + notify.alert(err, false); + }); + } } diff --git a/src/com/events/DataEvent.js b/src/com/events/DataEvent.js index 6f916f3..aa0f6fb 100644 --- a/src/com/events/DataEvent.js +++ b/src/com/events/DataEvent.js @@ -23,6 +23,7 @@ export const API_PAGE_CREATE = 'writingNewEntry'; export const API_PAGE_DELETE = 'erasingPage'; export const API_SETTINGS_WRITE = 'savingSettings'; export const API_IMAGES_UPLOAD = 'uploadProfileImages'; +export const API_RENDER_PAGES = 'renderPages'; class DataEvent { //-------------------------- // methods