From ea1795e0fa695a1533c680c231720f819371770d Mon Sep 17 00:00:00 2001 From: Ro Date: Sun, 1 Mar 2020 13:13:18 -0800 Subject: [PATCH] added Navigation data class, menu updates on page soft delete --- brain/api/v1/pages.js | 22 +++++++++++----- brain/data/Book.js | 14 ++++++++-- brain/data/Navigation.js | 44 +++++++++++++++++++++++++++++++ brain/routes/dash/index.js | 10 +++++-- brain/routes/dash/pages.js | 5 +--- src/com/actions/NavActions.js | 12 --------- src/com/actions/PageActions.js | 4 +-- src/com/controllers/PageEditor.js | 1 - src/com/events/DataEvent.js | 2 ++ 9 files changed, 83 insertions(+), 31 deletions(-) create mode 100644 brain/data/Navigation.js diff --git a/brain/api/v1/pages.js b/brain/api/v1/pages.js index 85d69d0..3471eef 100644 --- a/brain/api/v1/pages.js +++ b/brain/api/v1/pages.js @@ -1,4 +1,5 @@ import Book from '../../data/Book'; +import Navigation from '../../data/Navigation'; import * as DataEvent from '../../../src/com/events/DataEvent'; const express = require('express'); const router = express.Router(); @@ -8,6 +9,7 @@ const moment = require('moment'); const jwt = require('jsonwebtoken'); const bCrypt = require('bcrypt-nodejs'); const book = new Book(); +const nav = new Navigation(); const _ = require('lodash'); const uploadPath = './public/assets/images/blog/' + moment().format('YYYY') + '/' + moment().format('MM'); @@ -68,13 +70,7 @@ router.post('/write/:task?', feature_upload, (req, res) => { book.editPage(body, body.page_uuid, task, req.session.user) .then(result => { if (result.type === DataEvent.PAGE_CREATE) { - fs.readJSON('site/settings.json').then(settings => { - settings.library_stats.current_index = ++settings.library_stats - .current_index; - settings.library_stats.total_pages = ++settings.library_stats - .total_pages; - fs.writeJSON('site/settings.json', settings); - }); + nav.updatePageStats(); } res.json(result); }) @@ -98,6 +94,7 @@ router.post('/delete', (req, res) => { .then(() => { book.editPage([], req.body.id, DataEvent.API_PAGE_DELETE, req.session.user) .then(result => { + //remove item from menu in settings res.json(result); }) .catch(err => { @@ -126,10 +123,21 @@ router.post('/add-post-image', post_upload, function(req, res) { module.exports = router; +/** + * Checks to make sure received token matches + * @parameter token: created token + * @parameter hashedToken: encrypted token + */ + function isTokenValid(token, hashedToken) { return bCrypt.compareSync(token, hashedToken); } +/** + * Makes sure access token is legit + * @parameter req + */ + function authCheck(req) { return new Promise((resolve, reject) => { let hash = req.headers['x-access-token']; diff --git a/brain/data/Book.js b/brain/data/Book.js index ac677c0..94b7b41 100644 --- a/brain/data/Book.js +++ b/brain/data/Book.js @@ -3,6 +3,8 @@ import fs from 'fs-extra'; import metadataParser from 'markdown-yaml-metadata-parser'; import _ from 'lodash'; import * as DataEvent from '../../src/com/events/DataEvent'; +import Navigation from './Navigation'; +const nav = new Navigation(); const moment = require('moment'); export default class Pages { @@ -130,7 +132,7 @@ export default class Pages { } else { response = { type: DataEvent.PAGE_UPDATED, - message: 'Page saved, boss' + message: 'Page saved. Nice Work' }; resolve(response); } @@ -144,9 +146,17 @@ export default class Pages { body.deleted = moment(Date.now()).format(); self.editPage(body, body.uuid, DataEvent.API_PAGE_WRITE, user) .then(() => { + let item = { + title: body.title, + id: body.id, + slug: body.slug, + uuid: body.uuid + }; + nav.editMenu(DataEvent.MENU_DELETE_ITEM, item); response = { type: DataEvent.PAGE_DELETED, - message: 'Page deleted, sport' + message: 'Page deleted, sport', + data: { uuid: body.uuid } }; resolve(response); }) diff --git a/brain/data/Navigation.js b/brain/data/Navigation.js new file mode 100644 index 0000000..4dce6c5 --- /dev/null +++ b/brain/data/Navigation.js @@ -0,0 +1,44 @@ +//import fh from 'filehound'; +import fs from 'fs-extra'; +//import metadataParser from 'markdown-yaml-metadata-parser'; +import _ from 'lodash'; +import * as DataEvent from '../../src/com/events/DataEvent'; +//const moment = require('moment'); + +export default class Navigation { + //-------------------------- + // constructor + //-------------------------- + constructor() {} + //-------------------------- + // methods + //-------------------------- + start() {} + + editMenu(task, item) { + switch (task) { + case DataEvent.MENU_ADD_ITEM: + break; + case DataEvent.MENU_DELETE_ITEM: + fs.readJSON('site/settings.json').then(settings => { + settings.menu = _.remove(settings.menu, m => { + return m.uuid != item.uuid; + }); + fs.writeJSON('site/settings.json', settings); + }); + break; + } + } + + updatePageStats() { + fs.readJSON('site/settings.json').then(settings => { + settings.library_stats.current_index = ++settings.library_stats.current_index; + settings.library_stats.total_pages = ++settings.library_stats.total_pages; + fs.writeJSON('site/settings.json', settings); + }); + } + + //-------------------------- + // event handlers + //-------------------------- +} diff --git a/brain/routes/dash/index.js b/brain/routes/dash/index.js index 4b644ea..f3785bb 100644 --- a/brain/routes/dash/index.js +++ b/brain/routes/dash/index.js @@ -10,11 +10,17 @@ router.get('/', function(req, res) { let book = new Book(); book.getPage().then(result => { result.sort((a, b) => parseFloat(b.metadata.id) - parseFloat(a.metadata.id)); + let indexPages = []; + result.forEach(page => { + if (typeof page.metadata.deleted === 'undefined' || page.metadata.deleted === false) { + indexPages.push(page); + } + }); let pageData = []; if (req.session.user) { - pageData = { title: settings.global.title, status: true, pages: result }; + pageData = { title: settings.global.title, status: true, pages: indexPages }; } else { - pageData = { title: settings.global.title, status: false, pages: result }; + pageData = { title: settings.global.title, status: false, pages: indexPages }; } res.render('index', pageData); diff --git a/brain/routes/dash/pages.js b/brain/routes/dash/pages.js index 92e8f5d..c12b72e 100644 --- a/brain/routes/dash/pages.js +++ b/brain/routes/dash/pages.js @@ -27,10 +27,7 @@ router.get('/list/:filter?/:page?', function(req, res) { let featured = []; for (let index = 0; index < pages.length; index++) { let item = pages[index].metadata; - if ( - typeof item.deleted === 'undefined' || - (item.deleted === false && item.layout === 'page') - ) { + if (typeof item.deleted === 'undefined' || item.deleted === false) { all.push(pages[index].metadata); if (item.published == true) published.push(pages[index].metadata); if (item.menu == true) menu.push(pages[index].metadata); diff --git a/src/com/actions/NavActions.js b/src/com/actions/NavActions.js index 0c76f33..5219ee9 100644 --- a/src/com/actions/NavActions.js +++ b/src/com/actions/NavActions.js @@ -36,18 +36,6 @@ export default class NavActions { }); }); } - removeItem(id) { - /** - let self = this; - this.dbutils.getPost(id).then(post => { - post.post.page = 'false'; - self.dbutils.modify(id, post.post).then(() => { - document.getElementById('nav-pages').removeChild(document.getElementById(id)); - self.save(); - }); - }); - */ - } //-------------------------- // event handlers //-------------------------- diff --git a/src/com/actions/PageActions.js b/src/com/actions/PageActions.js index d916445..a6d5652 100644 --- a/src/com/actions/PageActions.js +++ b/src/com/actions/PageActions.js @@ -66,9 +66,7 @@ export default class PostActions { resolve(pageInfo); }); } - deletePost(id, body) { - let self = this; - } + updateNav(add, id, post) { api.request('/api/settings/nav', DataEvent.SETTINGS_LOADED) .then(response => { diff --git a/src/com/controllers/PageEditor.js b/src/com/controllers/PageEditor.js index 5a9b94f..58eeced 100644 --- a/src/com/controllers/PageEditor.js +++ b/src/com/controllers/PageEditor.js @@ -171,7 +171,6 @@ export default class PostEditor { id ) .then(() => { - console.log('DELETED'); window.location = '/@/dashboard/page/list/'; }) .catch(err => { diff --git a/src/com/events/DataEvent.js b/src/com/events/DataEvent.js index f2aa2e4..6f916f3 100644 --- a/src/com/events/DataEvent.js +++ b/src/com/events/DataEvent.js @@ -12,6 +12,8 @@ export const PAGE_ADDED = 'postAdded'; export const PAGE_UPDATED = 'postUpdated'; export const PAGE_DELETED = 'postImageAdded'; export const SETTINGS_UPDATED = 'settingsUpdated'; +export const MENU_ADD_ITEM = 'menuAddItem'; +export const MENU_DELETE_ITEM = 'menuDeleteItem'; export const MENU_UPDATED = 'menuUpdated'; export const AVATAR_UPLOADED = 'avatarUploaded'; export const SITE_BACKGROUND_UPLOADED = 'siteBackgroundUploaded';