added auth to nav update method, moved nav update method to nad data class

pull/20/head
Ro 5 years ago
parent f3ae41cd2a
commit 21520b1d60

@ -2,6 +2,7 @@ import * as DataEvent from '../../../src/com/events/DataEvent';
import Auth from '../../data/Auth'; import Auth from '../../data/Auth';
import Render from '../../data/Render'; import Render from '../../data/Render';
import SettingsData from '../../data/Settings'; import SettingsData from '../../data/Settings';
import Navigation from '../../data/Navigation';
import Book from '../../data/Book'; import Book from '../../data/Book';
const express = require('express'); const express = require('express');
const router = express.Router(); const router = express.Router();
@ -15,6 +16,7 @@ const auth = new Auth();
const render = new Render(); const render = new Render();
const book = new Book(); const book = new Book();
const settingsData = new SettingsData(); const settingsData = new SettingsData();
const nav = new Navigation();
const uploadPath = const uploadPath =
'./public/assets/images/user/' + moment().format('YYYY') + '/' + moment().format('MM'); './public/assets/images/user/' + moment().format('YYYY') + '/' + moment().format('MM');
fs.ensureDir(uploadPath, () => { fs.ensureDir(uploadPath, () => {
@ -63,23 +65,28 @@ router.post('/sync', (req, res) => {
}); });
router.post('/nav-sync', (req, res) => { router.post('/nav-sync', (req, res) => {
let payload = req.body; auth.authCheck(req)
if (req.session.user) { .then(() => {
settings.menu = payload; nav.sync(req)
fs.writeJson('site/settings.json', settings) .then(response => {
.then(() => { res.json({
res.json({ type: response.type,
type: DataEvent.SETTINGS_UPDATED, message: response.message
message: 'Menu order saved, champ' });
}); })
}) .catch(err => {
.catch(err => { res.json({
res.json({ type: DataEvent.REQUEST_LAME,
type: DataEvent.REQUEST_LAME, message: err
message: err });
}); });
})
.catch(err => {
res.json({
type: err.type,
message: err.message
}); });
} });
}); });
router.post('/publish-pages', (req, res) => { router.post('/publish-pages', (req, res) => {

@ -1,9 +1,7 @@
//import fh from 'filehound';
import fs from 'fs-extra'; import fs from 'fs-extra';
//import metadataParser from 'markdown-yaml-metadata-parser';
import _ from 'lodash'; import _ from 'lodash';
import * as DataEvent from '../../src/com/events/DataEvent'; import * as DataEvent from '../../src/com/events/DataEvent';
//const moment = require('moment'); const settings = require('../../site/settings.json');
export default class Navigation { export default class Navigation {
//-------------------------- //--------------------------
@ -13,7 +11,28 @@ export default class Navigation {
//-------------------------- //--------------------------
// methods // 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) { editMenu(task, item) {
switch (task) { switch (task) {

@ -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 { export default class NavActions {
//-------------------------- //--------------------------
// constructor // constructor
@ -9,7 +6,7 @@ export default class NavActions {
//-------------------------- //--------------------------
// methods // methods
//-------------------------- //--------------------------
save() { syncMenu() {
let navData = []; let navData = [];
let items = document.getElementById('nav-pages').children; let items = document.getElementById('nav-pages').children;
for (let index = 0; index < items.length; index++) { for (let index = 0; index < items.length; index++) {
@ -20,20 +17,8 @@ export default class NavActions {
uuid: items[index].getAttribute('data-uuid') uuid: items[index].getAttribute('data-uuid')
}); });
} }
return new Promise(function(resolve, reject) { return new Promise(function (resolve) {
api.request( resolve(navData);
'/api/v1/settings/nav-sync',
DataEvent.API_SETTINGS_WRITE,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
navData
)
.then(response => {
resolve(response);
})
.catch(err => {
reject(err);
});
}); });
} }
//-------------------------- //--------------------------

@ -1,14 +1,16 @@
import ApiUtils, { REQUEST_TYPE_POST, CONTENT_TYPE_JSON } from '../../../src/com/utils/APIUtils';
import NavActions from '../actions/NavActions'; import NavActions from '../actions/NavActions';
import * as DataEvent from '../events/DataEvent'; import * as DataEvent from '../events/DataEvent';
import Notifications from '../ui/Notifications'; import Notifications from '../ui/Notifications';
const notify = new Notifications(); const notify = new Notifications();
const api = new ApiUtils();
export default class NavIndex { export default class NavIndex {
//-------------------------- //--------------------------
// constructor // constructor
//-------------------------- //--------------------------
constructor() { constructor() {
api.authStatus();
this.start(); this.start();
//this.dataUtils = new DataUtils();
} }
//-------------------------- //--------------------------
// methods // methods
@ -16,19 +18,22 @@ export default class NavIndex {
start() { start() {
Sortable.create(document.getElementById('nav-pages'), { Sortable.create(document.getElementById('nav-pages'), {
onUpdate: () => { onUpdate: () => {
new NavActions() new NavActions().syncMenu().then(data => {
.save() api.request(
.then(response => { '/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']); let r = JSON.parse(response.request['response']);
if (r.type == DataEvent.MENU_UPDATED) { if (r.type == DataEvent.MENU_UPDATED) {
notify.alert(r.message, true); notify.alert(r.message, true);
} else { } else {
notify.alert(r.message, true); notify.alert(r.message, true);
} }
})
.catch(() => {
//console.log(err);
}); });
});
} }
}); });
var nav = document.querySelectorAll('.nav-btn'); var nav = document.querySelectorAll('.nav-btn');

Loading…
Cancel
Save