cleaned up save settings payload, added render on save toggle

pull/20/head
Ro 5 years ago
parent ab19d8027e
commit 03a96dfed1

@ -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,38 +31,47 @@ 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);
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 {
//console.log('USER NOT UPDATED');
//no need to save
}
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)
if (needToUpdate) {
fs.writeJson('site/settings.json', settings)
.then(() => {
res.json({
type: DataEvent.SETTINGS_UPDATED,
@ -79,10 +81,9 @@ router.post('/sync', (req, res) => {
.catch(() => {
//console.error(err);
});
})
.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."
});
}
});

@ -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'

@ -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

@ -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,
global: {
base_url: url,
title: title,
descriptions: desc,
background: background,
private: privacy,
theme: selected,
mailSettings: {
activeProtocol: mailActive,
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,

@ -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();

@ -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

Loading…
Cancel
Save