From 21520b1d60c9b6ee5d3e84f38eb568eb097ac688 Mon Sep 17 00:00:00 2001 From: Ro Date: Sat, 23 May 2020 13:55:25 -0700 Subject: [PATCH] added auth to nav update method, moved nav update method to nad data class --- brain/api/v1/settings.js | 37 ++++++++++++++++++++------------- brain/data/Navigation.js | 27 ++++++++++++++++++++---- src/com/actions/NavActions.js | 21 +++---------------- src/com/controllers/NavIndex.js | 19 ++++++++++------- 4 files changed, 60 insertions(+), 44 deletions(-) diff --git a/brain/api/v1/settings.js b/brain/api/v1/settings.js index 07433bd..e15d917 100644 --- a/brain/api/v1/settings.js +++ b/brain/api/v1/settings.js @@ -2,6 +2,7 @@ import * as DataEvent from '../../../src/com/events/DataEvent'; import Auth from '../../data/Auth'; import Render from '../../data/Render'; import SettingsData from '../../data/Settings'; +import Navigation from '../../data/Navigation'; import Book from '../../data/Book'; const express = require('express'); const router = express.Router(); @@ -15,6 +16,7 @@ const auth = new Auth(); const render = new Render(); const book = new Book(); const settingsData = new SettingsData(); +const nav = new Navigation(); const uploadPath = './public/assets/images/user/' + moment().format('YYYY') + '/' + moment().format('MM'); fs.ensureDir(uploadPath, () => { @@ -63,23 +65,28 @@ router.post('/sync', (req, res) => { }); router.post('/nav-sync', (req, res) => { - let payload = req.body; - if (req.session.user) { - settings.menu = payload; - fs.writeJson('site/settings.json', settings) - .then(() => { - res.json({ - type: DataEvent.SETTINGS_UPDATED, - message: 'Menu order saved, champ' - }); - }) - .catch(err => { - res.json({ - type: DataEvent.REQUEST_LAME, - message: err + auth.authCheck(req) + .then(() => { + nav.sync(req) + .then(response => { + res.json({ + type: response.type, + message: response.message + }); + }) + .catch(err => { + res.json({ + type: DataEvent.REQUEST_LAME, + message: err + }); }); + }) + .catch(err => { + res.json({ + type: err.type, + message: err.message }); - } + }); }); router.post('/publish-pages', (req, res) => { diff --git a/brain/data/Navigation.js b/brain/data/Navigation.js index 682929b..b3700b2 100644 --- a/brain/data/Navigation.js +++ b/brain/data/Navigation.js @@ -1,9 +1,7 @@ -//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'); +const settings = require('../../site/settings.json'); export default class Navigation { //-------------------------- @@ -13,7 +11,28 @@ export default class Navigation { //-------------------------- // methods //-------------------------- - start() {} + sync(req) { + return new Promise((resolve, reject) => { + let payload = req.body; + settings.menu = payload; + let response = []; + fs.writeJson('site/settings.json', settings) + .then(() => { + response = { + type: DataEvent.SETTINGS_UPDATED, + message: 'Menu order saved, champ' + }; + resolve(response); + }) + .catch(err => { + response = { + type: DataEvent.REQUEST_LAME, + message: err + }; + reject(response); + }); + }); + } editMenu(task, item) { switch (task) { diff --git a/src/com/actions/NavActions.js b/src/com/actions/NavActions.js index 5219ee9..d9808d0 100644 --- a/src/com/actions/NavActions.js +++ b/src/com/actions/NavActions.js @@ -1,6 +1,3 @@ -import ApiUtils, { REQUEST_TYPE_POST, CONTENT_TYPE_JSON } from '../../../src/com/utils/APIUtils'; -import * as DataEvent from '../../../src/com/events/DataEvent'; -const api = new ApiUtils(); export default class NavActions { //-------------------------- // constructor @@ -9,7 +6,7 @@ export default class NavActions { //-------------------------- // methods //-------------------------- - save() { + syncMenu() { let navData = []; let items = document.getElementById('nav-pages').children; for (let index = 0; index < items.length; index++) { @@ -20,20 +17,8 @@ export default class NavActions { uuid: items[index].getAttribute('data-uuid') }); } - return new Promise(function(resolve, reject) { - api.request( - '/api/v1/settings/nav-sync', - DataEvent.API_SETTINGS_WRITE, - REQUEST_TYPE_POST, - CONTENT_TYPE_JSON, - navData - ) - .then(response => { - resolve(response); - }) - .catch(err => { - reject(err); - }); + return new Promise(function (resolve) { + resolve(navData); }); } //-------------------------- diff --git a/src/com/controllers/NavIndex.js b/src/com/controllers/NavIndex.js index bdcd3c4..d7f6b45 100644 --- a/src/com/controllers/NavIndex.js +++ b/src/com/controllers/NavIndex.js @@ -1,14 +1,16 @@ +import ApiUtils, { REQUEST_TYPE_POST, CONTENT_TYPE_JSON } from '../../../src/com/utils/APIUtils'; import NavActions from '../actions/NavActions'; import * as DataEvent from '../events/DataEvent'; import Notifications from '../ui/Notifications'; const notify = new Notifications(); +const api = new ApiUtils(); export default class NavIndex { //-------------------------- // constructor //-------------------------- constructor() { + api.authStatus(); this.start(); - //this.dataUtils = new DataUtils(); } //-------------------------- // methods @@ -16,19 +18,22 @@ export default class NavIndex { start() { Sortable.create(document.getElementById('nav-pages'), { onUpdate: () => { - new NavActions() - .save() - .then(response => { + new NavActions().syncMenu().then(data => { + api.request( + '/api/v1/settings/nav-sync', + DataEvent.API_SETTINGS_WRITE, + REQUEST_TYPE_POST, + CONTENT_TYPE_JSON, + data + ).then(response => { let r = JSON.parse(response.request['response']); if (r.type == DataEvent.MENU_UPDATED) { notify.alert(r.message, true); } else { notify.alert(r.message, true); } - }) - .catch(() => { - //console.log(err); }); + }); } }); var nav = document.querySelectorAll('.nav-btn');