diff --git a/brain/api/v1/pages.js b/brain/api/v1/pages.js index 0089f88..5d9315d 100644 --- a/brain/api/v1/pages.js +++ b/brain/api/v1/pages.js @@ -51,7 +51,60 @@ router.get('/', (req, res) => { Update Page */ router.post('/write', feature_upload, (req, res) => { - console.log('FILEZ BRAH', req.files); + var feature = ''; + if (req.files.lengh > 0) { + } else { + var url = req.body.feature_image; + feature = url.substring(21, url.length); + } + + var pageWrite = + '---\n' + + 'id: ' + + req.body.page_id + + '\n' + + 'uuid: ' + + req.body.page_uuid + + '\n' + + 'title: ' + + req.body.title + + '\n' + + 'feature: ' + + feature + + '\n' + + 'layout: ' + + 'page' + + '\n' + + 'tags: ' + + req.body.tags + + '\n' + + 'author: ' + + req.session.user.handle + + '\n' + + 'created: ' + + req.body.created + + '\n' + + 'updated: ' + + moment(Date.now()).format() + + '\n' + + 'featured: ' + + req.body.featureStatus + + '\n' + + 'published: ' + + req.body.publishedStatus + + '\n' + + 'slug: ' + + req.body.slug + + '\n' + + '---\n\n' + + req.body.content; + fs.writeFile('content/pages/test.md', pageWrite, err => { + // throws an error, you could also catch it here + if (err) res.json({ type: DataEvent.PAGE_ERROR, message: err }); + + // success case, the file was saved + res.json({ type: DataEvent.PAGE_UPDATED, message: 'Page Has been saved' }); + }); }); module.exports = router; diff --git a/brain/routes/dash/pages.js b/brain/routes/dash/pages.js index 1b6fbbf..aab43d9 100644 --- a/brain/routes/dash/pages.js +++ b/brain/routes/dash/pages.js @@ -57,12 +57,14 @@ router.get('/edit/:id', function(req, res) { let pretty = hljs.highlight('markdown', page.content).value; res.render('page-edit', { id: page.metadata.id, + uuid: page.metadata.uuid, title: 'Edit Page', user_status: true, welcome: 'Edit Page', mode: 'admin', page: page.metadata, date: moment(page.metadata.created).format('YYYY MMM DD'), + rawDate: page.metadata.created, colored: pretty, feature: page.metadata.feature, status: [ diff --git a/brain/views/page-edit.pug b/brain/views/page-edit.pug index 9743c9e..72c1f37 100644 --- a/brain/views/page-edit.pug +++ b/brain/views/page-edit.pug @@ -18,7 +18,7 @@ block main-content -post_status = status form#test-form - #post-edit-index(data-index=id) + #post-edit-index(data-index=id data-uuid=uuid) #post-edit-index-wrapper //h2 EDIT =post_title @@ -35,7 +35,7 @@ block main-content svg#new-feature-upload(viewBox="0 0 20 20" class="icons") use(xlink:href='/assets/images/global/sprite.svg#entypo-image-inverted') #featured-image-drop - img(src=post_feature) + img#featured-image(src=post_feature) #post-header.columns #post-title.column textarea(id="post_title" type='text', name='post_title' class='post-edit', placeholder='title', required, autofocus) @@ -43,7 +43,7 @@ block main-content #calendar-icon svg(viewBox="0 0 20 20" class="icons") use(xlink:href='/assets/images/global/sprite.svg#entypo-calendar') - input(id="post-date" type="text" value=post_date) + input(id="post-date" type="text" value=post_date data-raw=rawDate) #post-options button#option-page.option-inactive.post-option-btn(data-active= status[0]) svg#option-page-icon(viewBox="0 0 20 20" class="icons") diff --git a/src/com/actions/PostActions.js b/src/com/actions/PageActions.js similarity index 67% rename from src/com/actions/PostActions.js rename to src/com/actions/PageActions.js index dcc4b97..87ca2c9 100644 --- a/src/com/actions/PostActions.js +++ b/src/com/actions/PageActions.js @@ -1,6 +1,7 @@ import DataUtils, { REQUEST_TYPE_POST, CONTENT_TYPE_JSON } from '../utils/DataUtils'; import StringUtils from '../utils/StringUtils'; import * as DataEvent from '../events/DataEvent'; +import { isMaster } from 'cluster'; var uuidv4 = require('uuid/v4'); export default class PostActions { //-------------------------- @@ -12,6 +13,62 @@ export default class PostActions { //-------------------------- // methods //-------------------------- + collectInfo(image) { + return new Promise((resolve, reject) => { + let pageInfo = new FormData(); + let txt = document.createElement('textarea'); + txt.innerHTML = document.getElementById('edit-post-text').innerHTML; + let html = txt.value; + html = html.replace(/<\/?span[^>]*>/g, ''); //removes highightjs styling + html = html.replace(/<\/?br[^>]*>/g, '\n'); //convert back to encoded line break for storage + pageInfo.append( + 'page_id', + document.getElementById('post-edit-index').getAttribute('data-index') + ); + pageInfo.append( + 'page_uuid', + document.getElementById('post-edit-index').getAttribute('data-uuid') + ); + pageInfo.append('content', html); + pageInfo.append('title', document.getElementById('post_title').value); + pageInfo.append( + 'created', + document.getElementById('post-date').getAttribute('data-raw') + ); + pageInfo.append( + 'slug', + new StringUtils().cleanString(document.getElementById('post_title').value) + ); + pageInfo.append('tags', document.getElementById('post_tags').value); + pageInfo.append( + 'pageStatus', + document.getElementById('option-page').getAttribute('data-active') + ); + pageInfo.append( + 'featureStatus', + document.getElementById('option-feature').getAttribute('data-active') + ); + pageInfo.append( + 'publishedStatus', + document.getElementById('option-published').getAttribute('data-active') + ); + if (image != null || image != undefined) { + if (image.type.match('image.*')) { + pageInfo.append('feature_image', image, image.name); + } else { + reject('Not an image file'); + } + } else { + //check to see if image exists + var imageURL = document.getElementById('featured-image').src; + imageURL != null || imageURL != undefined + ? pageInfo.append('feature_image', imageURL) + : pageInfo.append('feature_image', null); + } + + resolve(pageInfo); + }); + } update(id, data, files, lastKey) { let self = this; let freshData; diff --git a/src/com/controllers/PostEditor.js b/src/com/controllers/PageEditor.js similarity index 86% rename from src/com/controllers/PostEditor.js rename to src/com/controllers/PageEditor.js index ef12da3..debf3e8 100644 --- a/src/com/controllers/PostEditor.js +++ b/src/com/controllers/PageEditor.js @@ -1,10 +1,10 @@ //TOOLS -import DataUtils, { REQUEST_TYPE_POST, CONTENT_TYPE_FORM } from '../../../src/com/utils/DataUtils'; -import * as DataEvent from '../../../src/com/events/DataEvent'; -import PostActions from '../actions/PostActions'; -import * as EditorEvent from '../../../src/com/events/EditorEvent'; +import DataUtils, { REQUEST_TYPE_POST, CONTENT_TYPE_FORM } from '../utils/DataUtils'; +import * as DataEvent from '../events/DataEvent'; +import PageActions from '../actions/PageActions'; +import * as EditorEvent from '../events/EditorEvent'; import TinyDatePicker from 'tiny-date-picker'; -import TextEditor from '../../../src/com/ui/TextEditor'; +import TextEditor from '../ui/TextEditor'; const data = new DataUtils(); export default class PostEditor { //TODO - FIX POST FEATURE URLS IN DB @@ -117,7 +117,7 @@ export default class PostEditor { let self = this; switch (e) { case EditorEvent.EDITOR_SAVE: - new PostActions() + new PageActions() .update(this.postID, this.post, PostEditor.uploadFiles, FINAL_KEY) .then(response => { setTimeout(() => { @@ -131,40 +131,28 @@ export default class PostEditor { }); break; case EditorEvent.EDITOR_UPDATE: - var pageData = new FormData(); - pageData.append('name', 'HAMMOCK LANSING'); - var image = document.getElementById('featured-image-upload').files[0]; - if (image != null || image != undefined) { - pageData.append('feature_image', image, image.name); - } - - data.request( - '/api/v1/page/write', - DataEvent.API_PAGE_WRITE, - REQUEST_TYPE_POST, - CONTENT_TYPE_FORM, - pageData - ) - .then(r => { - console.log('RESPONSE', r); - }) - .catch(err => { - console.log('ERROR', err); + new PageActions() + .collectInfo(document.getElementById('featured-image-upload').files[0]) + .then(page => { + data.request( + '/api/v1/page/write', + DataEvent.API_PAGE_WRITE, + REQUEST_TYPE_POST, + CONTENT_TYPE_FORM, + page + ) + .then(response => { + let r = JSON.parse(response.request['response']); + console.log('RESPONSE', r.message); + }) + .catch(err => { + console.log('ERROR', err); + }); }); - /** - new PostActions() - .update(this.postID, this.post, PostEditor.uploadFiles, FINAL_KEY) - .then(() => { - this.editor.notify(DataEvent.POST_UPDATED, this.postID); - }) - .catch(() => { - //console.log("ERRORZ", err) - }); - */ break; case EditorEvent.EDITOR_DELETE: if (confirm("Aye! You know you're deleting this post, right?")) { - new PostActions() + new PageActions() .deletePost(this.postID, this.post) .then(() => { setTimeout(() => { diff --git a/src/com/controllers/PostIndex.js b/src/com/controllers/PostIndex.js index 3d6157f..41d2798 100644 --- a/src/com/controllers/PostIndex.js +++ b/src/com/controllers/PostIndex.js @@ -1,4 +1,4 @@ -import PostEditor from './PostEditor'; +import PageEditor from './PageEditor'; export default class PostIndex { //-------------------------- // constructor @@ -17,7 +17,7 @@ export default class PostIndex { switch (page) { case 'edit': case 'add': - this.currentPage = new PostEditor(); + this.currentPage = new PageEditor(); break; default: //just chill diff --git a/src/com/events/DataEvent.js b/src/com/events/DataEvent.js index f7c88fe..ae554ba 100644 --- a/src/com/events/DataEvent.js +++ b/src/com/events/DataEvent.js @@ -5,11 +5,10 @@ export const IMG_REQUEST_LAME = 'imgRequestLame'; export const SETTINGS_LOADED = 'settingsLoaded'; export const POST_IMAGE_ADDED = 'postImageAdded'; export const FEATURE_IMAGE_ADDED = 'featureImageAdded'; -export const POST_ERROR = 'postError'; -export const POST_ADDED = 'postAdded'; -export const POST_UPDATED = 'postUpdated'; -export const POST_DELETED = 'postImageAdded'; -export const POSTS_SYNCED = 'postsSynced'; +export const PAGE_ERROR = 'postError'; +export const PAGE_ADDED = 'postAdded'; +export const PAGE_UPDATED = 'postUpdated'; +export const PAGE_DELETED = 'postImageAdded'; export const SETTINGS_UPDATED = 'settingsUpdated'; export const AVATAR_UPLOADED = 'avatarUploaded'; export const SITE_BACKGROUND_UPLOADED = 'siteBackgroundUploaded';