From 03a96dfed181f17a411701cbdd09a3319f5ffdac Mon Sep 17 00:00:00 2001 From: Ro Date: Fri, 29 Nov 2019 11:12:15 -0800 Subject: [PATCH] cleaned up save settings payload, added render on save toggle --- brain/api/v1/settings.js | 107 +++++++++++++-------------- brain/routes/dash/settings.js | 2 +- brain/views/settings.pug | 26 ++++--- src/com/actions/SettingsActions.js | 35 ++++----- src/com/controllers/SettingsIndex.js | 27 ++++++- src/styles/main/_settings.styl | 6 ++ 6 files changed, 116 insertions(+), 87 deletions(-) diff --git a/brain/api/v1/settings.js b/brain/api/v1/settings.js index 7a0e36a..b722839 100644 --- a/brain/api/v1/settings.js +++ b/brain/api/v1/settings.js @@ -1,17 +1,10 @@ import * as DataEvent from '../../../src/com/events/DataEvent'; -import RightsManager, { - TASK_CREATE, - TASK_UPDATE, - OBJECT_POST, - OBJECT_SETTINGS -} from '../../../src/com/utils/RightsManager'; const express = require('express'); const router = express.Router(); const multer = require('multer'); const fs = require('fs-extra'); const moment = require('moment'); const _ = require('lodash'); -const rightsManager = new RightsManager(); const settings = require('../../../site/settings.json'); const folks = require('../../../site/folks.json'); const uploadPath = @@ -38,51 +31,59 @@ var background_upload = multer({ //** SYNC POSTS */ router.post('/sync', (req, res) => { let payload = req.body; - if (req.handle.user) { + if (req.session.user) { let user = req.session.user; - fs.readJson('site/settings.json') - .then(obj => { - if (user.hande != payload.handle || user.email != payload.email) { - user.update({ - handle: payload.handle, - email: payload.email - }) - .then(() => { - //console.log('UPDATED'); - }) - .catch(() => { - //console.log('ERR', err); - }); - } else { - //console.log('USER NOT UPDATED'); - } - obj.url = payload.url; - obj.title = payload.title; - obj.description = payload.descriptions; - obj.private = payload.private; - obj.theme = payload.theme; - obj.background = payload.background; //TODO: make this url relative - //mail stuff - obj.email.active = payload.mailSettings.activeProtocol; - obj.email.smtp.domain = payload.mailSettings.smtp.domain; - obj.email.smtp.email = payload.mailSettings.smtp.email; - obj.email.smtp.password = payload.mailSettings.smtp.password; - obj.email.mailgun.domain = payload.mailSettings.mailgun.domain; - obj.email.mailgun['api-key'] = payload.mailSettings.mailgun.key; - fs.writeJson('site/settings.json', obj) - .then(() => { - res.json({ - type: DataEvent.SETTINGS_UPDATED, - message: 'Settings Saved' - }); - }) - .catch(() => { - //console.error(err); - }); - }) - .catch(() => { - //console.error(err); + let found = _.find(folks, { id: user.id }); + let needToUpdate = false; + if (found) { + let index = found.id - 1; + if ( + folks[index].handle != payload.member.handle || + folks[index].email != payload.member.email + ) { + folks[index].handle = payload.member.handle; + folks[index].email = payload.member.email; + fs.writeJson('site/folks.json', folks); + } else { + //no need to save + } + } else { + res.json({ + type: DataEvent.REQUEST_LAME, + message: "You're not logged in, champ" }); + } + + if (!_.isEqual(settings.global, payload.global)) { + let bg = payload.global.background; + payload.global.background = bg.substr(21, bg.length); + settings.global = payload.global; + needToUpdate = true; + } else { + //no need to save + } + + if (!_.isEqual(settings.email, payload.email)) { + settings.email = payload.email; + needToUpdate = true; + } else { + //no need to save + } + + if (needToUpdate) { + fs.writeJson('site/settings.json', settings) + .then(() => { + res.json({ + type: DataEvent.SETTINGS_UPDATED, + message: 'Settings Saved' + }); + }) + .catch(() => { + //console.error(err); + }); + } else { + //no need to update + } } }); @@ -104,7 +105,7 @@ router.get('/nav', function(req, res) { router.post('/nav-sync', (req, res) => { let payload = req.body; - if (rightsManager.check(user.role, OBJECT_SETTINGS, TASK_UPDATE)) { + if (req.session.user) { fs.readJson('config/site-settings.json') .then(obj => { obj.menu = payload; @@ -121,10 +122,6 @@ router.post('/nav-sync', (req, res) => { .catch(() => { //console.error(err); }); - } else { - res.json({ - message: "Nah. You can't do that. Talk to the admin, sport." - }); } }); diff --git a/brain/routes/dash/settings.js b/brain/routes/dash/settings.js index 3f4a3da..2a2df63 100644 --- a/brain/routes/dash/settings.js +++ b/brain/routes/dash/settings.js @@ -54,7 +54,7 @@ function loadThemes() { let themes = []; for (let index = 0; index < files.length; index++) { fs.readJSON(files[index], (err, theme) => { - if (theme.name == settings.theme) { + if (theme.name == settings.global.theme) { themes.push({ theme: theme, current: 'true' diff --git a/brain/views/settings.pug b/brain/views/settings.pug index 3931d1e..58dc46d 100644 --- a/brain/views/settings.pug +++ b/brain/views/settings.pug @@ -2,7 +2,7 @@ extends frame block main-content #site-background label FEATURE SITE IMAGE - img#background(src=settings.background, alt="image for site background", for="background-upload") + img#background(src=settings.global.background, alt="image for site background", for="background-upload") input(id="background-upload" type="file" name="backgrond-upload") #settings-index @@ -12,22 +12,28 @@ block main-content label AVATAR #member-avatar-drop img#avatar(src=member.avi, for="avatar-upload") - button#save-toggle SAVE SETTINGS + button#privacy-toggle(data-private=settings.global.private) + -if (settings.global.private == 'false') + | SITE IS PRIVATE + -else + | SITE IS PUBLIC + button#render-toggle(data-render=settings.global.renderOnSave) + -if (settings.global.renderOnSave == 'false') + | DON'T RENDER PAGES ON SAVE + -else + | RENDER PAGES ON SAVE input(id="avatar-upload" type="file" name="avatar-upload") #member-settings-2.column label INFO #member-info input(type='text', name='handle' id='settings-handle', placeholder='handle', value=member.handle, autofocus) input(type='text', name='email' id='settings-email', placeholder='email', value=member.email, autofocus) - input(type='text', name='base-url' id='settings-url', placeholder='url', value=settings.base_url, autofocus) - input(type='text', name='base-title' id='settings-title', placeholder='site title', value=settings.title, autofocus) + input(type='text', name='base-url' id='settings-url', placeholder='url', value=settings.global.base_url, autofocus) + input(type='text', name='base-title' id='settings-title', placeholder='site title', value=settings.global.title, autofocus) textarea(id="settings-desc" type='text', name='settings_desc' class='settings-dec', placeholder='description stuff', autofocus) - =settings.description - button#privacy-toggle(data-private=settings.private) - -if (settings.private == 'false') - | SITE IS PRIVATE - -else - | SITE IS PUBLIC + =settings.global.descriptions + button#save-toggle SAVE SETTINGS + #option-settings.columns #theme-settings.column label THEMES diff --git a/src/com/actions/SettingsActions.js b/src/com/actions/SettingsActions.js index d4e4cca..8cbc2b2 100644 --- a/src/com/actions/SettingsActions.js +++ b/src/com/actions/SettingsActions.js @@ -1,23 +1,22 @@ import DataUtils, { REQUEST_TYPE_POST, CONTENT_TYPE_JSON } from '../../../src/com/utils/DataUtils'; import * as DataEvent from '../../../src/com/events/DataEvent'; +const utils = new DataUtils(); export default class SettingsActions { //-------------------------- // constructor //-------------------------- - constructor() { - this.dataUtils = new DataUtils(); - } + constructor() {} //-------------------------- // methods //-------------------------- save() { - let self = this; let handle = document.getElementById('settings-handle').value; let email = document.getElementById('settings-email').value; let url = document.getElementById('settings-url').value; let title = document.getElementById('settings-title').value; - let desc = document.getElementById('settings-desc').innerHTML; + let desc = document.getElementById('settings-desc').value; let privacy = document.getElementById('privacy-toggle').getAttribute('data-private'); + let render = document.getElementById('render-toggle').getAttribute('data-render'); let background = document.getElementById('background').src; let selected = ''; let selects = document.querySelectorAll('.theme-select'); @@ -38,16 +37,18 @@ export default class SettingsActions { mailActive = mailOptions[i].id; } let settingsData = { - handle: handle, - email: email, - url: url, - title: title, - descriptions: desc, - background: background, - private: privacy, - theme: selected, - mailSettings: { - activeProtocol: mailActive, + global: { + base_url: url, + title: title, + descriptions: desc, + background: background, + private: privacy, + renderOnSave: render, + theme: selected + }, + member: { handle: handle, email: email }, + email: { + active: mailActive, smtp: { domain: smtpDomain, email: smtpEmail, @@ -60,9 +61,9 @@ export default class SettingsActions { } }; return new Promise(function(resolve, reject) { - self.dataUtils + utils .request( - '/api/settings/sync', + '/api/v1/settings/sync', DataEvent.SETTINGS_UPDATED, REQUEST_TYPE_POST, CONTENT_TYPE_JSON, diff --git a/src/com/controllers/SettingsIndex.js b/src/com/controllers/SettingsIndex.js index 1dfcc4b..f62b541 100644 --- a/src/com/controllers/SettingsIndex.js +++ b/src/com/controllers/SettingsIndex.js @@ -5,13 +5,13 @@ import Mailer from '../actions/Mailer'; import Notifications from '../ui/Notifications'; const utils = new DataUtils(); const notify = new Notifications(); +const mailer = new Mailer(); export default class SettingsIndex { //-------------------------- // constructor //-------------------------- constructor() { this.start(); - this.mailer = new Mailer(); } //-------------------------- // methods @@ -22,8 +22,13 @@ export default class SettingsIndex { document.getElementById('save-toggle').addEventListener('click', () => new SettingsActions() .save() - .then(() => { - //console.log(response); + .then(response => { + let r = JSON.parse(response.request['response']); + if (r.type == DataEvent.SETTINGS_UPDATED) { + notify.alert(r.message, true); + } else { + notify.alert(r.message, true); + } }) .catch(() => { //console.log(err); @@ -54,6 +59,9 @@ export default class SettingsIndex { document .getElementById('privacy-toggle') .addEventListener('click', e => this.togglePrivacy(e)); + document + .getElementById('render-toggle') + .addEventListener('click', e => this.toggleRender(e)); document.getElementById('send-mail').addEventListener('click', e => this.handleMailer(e)); //handle theme toggle let themeBtns = document.querySelectorAll('.theme-select'); @@ -80,8 +88,19 @@ export default class SettingsIndex { e.target.innerHTML = 'SITE IS PRIVATE'; } } + toggleRender(e) { + e.stopPropagation(); + e.preventDefault(); + if (e.target.getAttribute('data-render') == 'false') { + e.target.setAttribute('data-render', 'true'); + e.target.innerHTML = 'RENDER PAGES ON SAVE'; + } else { + e.target.setAttribute('data-render', 'false'); + e.target.innerHTML = "DON'T RENDER PAGES ON SAVE"; + } + } handleMailer() { - this.mailer.sendMail(); + mailer.sendMail(); } handleThemes(e) { e.stopPropagation(); diff --git a/src/styles/main/_settings.styl b/src/styles/main/_settings.styl index 1f6b4c1..e833ccb 100644 --- a/src/styles/main/_settings.styl +++ b/src/styles/main/_settings.styl @@ -61,10 +61,16 @@ border-radius 5px overflow hidden cursor pointer + display block + margin-bottom 2px input visibility hidden display none + #privacy-toggle + width 50% + #render-toggle + width 50% #member-info vertical-align top