moved settings retrieval to Settting data class to get rid of settings caching problem

pull/20/head
Ro 4 years ago
parent 36d6631df9
commit e6b3917c51

@ -1,7 +1,9 @@
import fs from 'fs-extra'; import fs from 'fs-extra';
import _ from 'lodash'; import _ from 'lodash';
import * as DataEvent from '../../src/com/events/DataEvent'; import * as DataEvent from '../../src/com/events/DataEvent';
const settings = require('../../site/settings.json'); import Settings, { SETTINGS_FILE } from './Settings';
const settings = new Settings();
//const settings = require('../../site/settings.json');
export default class Navigation { export default class Navigation {
//-------------------------- //--------------------------
@ -39,11 +41,11 @@ export default class Navigation {
case DataEvent.MENU_ADD_ITEM: case DataEvent.MENU_ADD_ITEM:
break; break;
case DataEvent.MENU_DELETE_ITEM: case DataEvent.MENU_DELETE_ITEM:
fs.readJSON('site/settings.json').then(settings => { settings.load(SETTINGS_FILE).then(settings => {
settings.menu = _.remove(settings.menu, m => { settings.menu = _.remove(settings.menu, m => {
return m.uuid != item.uuid; return m.uuid != item.uuid;
}); });
fs.writeJSON('site/settings.json', settings); fs.writeJSON(SETTINGS_FILE, settings);
}); });
break; break;
} }

@ -1,13 +1,13 @@
import * as DataEvent from '../../src/com/events/DataEvent'; import * as DataEvent from '../../src/com/events/DataEvent';
import StringUtils from '../../src/com/utils/StringUtils'; import StringUtils from '../../src/com/utils/StringUtils';
import Settings, { SETTINGS_FILE, SETTINGS_TAG } from './Settings';
import fs from 'fs-extra'; import fs from 'fs-extra';
import sanitize from 'sanitize-html'; import sanitize from 'sanitize-html';
const config = require('../../site/settings.json');
const tags_list = require('../../site/tags.json');
const pug = require('pug'); const pug = require('pug');
const md = require('markdown-it')('commonmark'); const md = require('markdown-it')('commonmark');
const _ = require('lodash'); const _ = require('lodash');
const moment = require('moment'); const moment = require('moment');
const settings = new Settings();
export default class Render { export default class Render {
//-------------------------- //--------------------------
@ -26,138 +26,186 @@ export default class Render {
*/ */
publishAll(pages, theme) { publishAll(pages, theme) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let response = []; settings
let count = _.filter(pages, page => { .load(SETTINGS_FILE)
return page.metadata.deleted === false; .then(config => {
}).length; let response = [];
let rendered = 0; let count = _.filter(pages, page => {
for (let index = 0; index < pages.length; index++) { return page.metadata.deleted === false;
const page = pages[index]; }).length;
if (page.metadata.deleted === false) { let rendered = 0;
let writeFile, template; for (let index = 0; index < pages.length; index++) {
let path = const page = pages[index];
'public/' + if (page.metadata.deleted === false) {
moment(page.metadata.created).format('YYYY') + let writeFile, template;
'/' + let path =
moment(page.metadata.created).format('MM') + 'public/' +
'/'; moment(page.metadata.created).format('YYYY') +
if (page.metadata.layout === 'index') { '/' +
template = 'content/themes/' + theme + '/index.pug'; moment(page.metadata.created).format('MM') +
writeFile = 'public/index.html'; '/';
} else { if (page.metadata.layout === 'index') {
writeFile = path + page.metadata.slug + '.html'; template = 'content/themes/' + theme + '/index.pug';
template = 'content/themes/' + theme + '/page.pug'; writeFile = 'public/index.html';
} } else {
writeFile = path + page.metadata.slug + '.html';
template = 'content/themes/' + theme + '/page.pug';
}
let buffed = sanitize(page.content, {
allowedTags: ['del', 'a', 'iframe', 'img'],
allowedAttributes: {
a: ['href', 'name', 'target'],
img: ['src'],
iframe: [
'height',
'width',
'src',
'frameborder',
'allow',
'allowfullscreen'
]
}
});
buffed = new StringUtils().decodeHTML(buffed);
let html = md.render(buffed, { html: true, xhtmlOut: true });
let file = pug.renderFile(template, {
title: page.metadata.title,
default_bg: page.metadata.feature,
content: html,
tags: page.metadata.tags,
menu: config.menu
});
let buffed = sanitize(page.content, { fs.ensureDir(path).then(() => {
allowedTags: ['del', 'a', 'iframe', 'img'], fs.writeFile(writeFile, file, err => {
allowedAttributes: { // throws an error, you could also catch it here
a: ['href', 'name', 'target'], if (err) {
img: ['src'], response = {
iframe: [ type: DataEvent.PAGES_NOT_RENDERED,
'height', message: err
'width', };
'src', reject(response);
'frameborder', }
'allow',
'allowfullscreen' // success case, the file was saved
] });
});
rendered = ++rendered;
if (rendered === count) {
response = {
type: DataEvent.PAGES_RENDERED,
message: 'All Pages Rendered. Sweet.'
};
resolve(response);
}
} else {
//don't render delete pages
} }
}); }
})
.catch(err => {
reject(err);
});
});
}
publishTags(pages) {
let self = this;
return new Promise((resolve, reject) => {
self.loadRenderData()
.then(result => {
let tags = result.tags.tags;
let renderList = [];
for (let index = 0; index < tags.length; index++) {
let tag = tags[index];
//console.log('**TAG**', tag.tag_name);
var pageList = [];
for (let i = 0; i < pages.length; i++) {
let page = pages[i];
buffed = new StringUtils().decodeHTML(buffed); if (_.includes(page.metadata.tags, tag.tag_name)) {
let html = md.render(buffed, { html: true, xhtmlOut: true }); pageList.push({
let file = pug.renderFile(template, { title: page.metadata.title,
title: page.metadata.title, slug: page.metadata.slug
default_bg: page.metadata.feature, });
content: html, }
tags: page.metadata.tags, }
menu: config.menu renderList.push({ tag: tag.tag_name, tag_list: pageList, slug: tag.slug });
}); }
let response = [];
for (let index = 0; index < renderList.length; index++) {
let item = renderList[index];
let file = pug.renderFile(
'content/themes/' + result.settings.global.theme + '/tags.pug',
{
title: item.tag,
default_bg: result.settings.global.background,
content_tags: 'THESE ARE TAGS',
tag_list: item.tag_list
}
);
fs.ensureDir(path).then(() => { fs.writeFile('public/tags/' + item.slug + '.html', file, err => {
fs.writeFile(writeFile, file, err => {
// throws an error, you could also catch it here // throws an error, you could also catch it here
if (err) { if (err) {
response = { type: DataEvent.PAGES_NOT_RENDERED, message: err }; response = { type: DataEvent.TAG_PAGES_NOT_RENDERED, message: err };
reject(response); reject(response);
} }
// success case, the file was saved // success case, the file was saved
response = {
type: DataEvent.TAG_PAGES_RENDERED,
message: 'Tag Pages ready to go. Good job.'
};
resolve(response);
}); });
});
rendered = ++rendered;
if (rendered === count) {
response = {
type: DataEvent.PAGES_RENDERED,
message: 'All Pages Rendered. Sweet.'
};
resolve(response);
} }
} else { })
//don't render delete pages .catch(err => {
} reject(err);
} });
}); });
} }
publishTags(pages) { publishArchive(archive) {
return new Promise((resolve, reject) => { settings
let tags = tags_list.tags; .load(SETTINGS_FILE)
let renderList = []; .then(settings => {
for (let index = 0; index < tags.length; index++) { let file = pug.renderFile(
let tag = tags[index]; 'content/themes/' + settings.global.theme + '/archive.pug',
//console.log('**TAG**', tag.tag_name); {
var pageList = []; title: 'ARCHIVES',
for (let i = 0; i < pages.length; i++) { default_bg: settings.global.background,
let page = pages[i]; content_tags: 'COLD STORAGE',
archives: archive
if (_.includes(page.metadata.tags, tag.tag_name)) {
pageList.push({ title: page.metadata.title, slug: page.metadata.slug });
} }
} );
renderList.push({ tag: tag.tag_name, tag_list: pageList, slug: tag.slug });
}
let response = [];
for (let index = 0; index < renderList.length; index++) {
let item = renderList[index];
let file = pug.renderFile('content/themes/' + config.global.theme + '/tags.pug', {
title: item.tag,
default_bg: config.global.background,
content_tags: 'THESE ARE TAGS',
tag_list: item.tag_list
});
fs.writeFile('public/tags/' + item.slug + '.html', file, err => { fs.writeFile('public/archives.html', file, err => {
// throws an error, you could also catch it here // throws an error, you could also catch it here
if (err) { if (err) {
response = { type: DataEvent.TAG_PAGES_NOT_RENDERED, message: err }; console.log('ERROR', err);
reject(response); //response = { type: DataEvent.TAG_PAGES_NOT_RENDERED, message: err };
} }
// success case, the file was saved // success case, the file was saved
response = {
type: DataEvent.TAG_PAGES_RENDERED,
message: 'Tag Pages ready to go. Good job.'
};
resolve(response);
}); });
} })
}); .catch(err => {
console.log(err);
});
} }
publishArchive(archive) { loadRenderData() {
let file = pug.renderFile('content/themes/' + config.global.theme + '/archive.pug', { return new Promise((resolve, reject) => {
title: 'ARCHIVES', let getSettings = settings.load(SETTINGS_FILE);
default_bg: config.global.background, let getTags = settings.load(SETTINGS_TAG);
content_tags: 'COLD STORAGE', Promise.all([getSettings, getTags])
archives: archive .then(result => {
}); const [settings, tags] = result;
let data = { settings: settings, tags: tags };
fs.writeFile('public/archives.html', file, err => { resolve(data);
// throws an error, you could also catch it here })
if (err) { .catch(err => {
console.log('ERROR', err); reject(err);
//response = { type: DataEvent.TAG_PAGES_NOT_RENDERED, message: err }; });
}
// success case, the file was saved
}); });
} }

@ -1,9 +1,9 @@
import * as DataEvent from '../../src/com/events/DataEvent'; import * as DataEvent from '../../src/com/events/DataEvent';
import fs from 'fs-extra'; import fs from 'fs-extra';
const settings = require('../../site/settings.json');
const folks = require('../../site/folks.json');
const config = require('../../site/tags.json');
const _ = require('lodash'); const _ = require('lodash');
export const SETTINGS_FILE = 'site/settings.json';
export const SETTINGS_FOLKS = 'site/folks.json';
export const SETTINGS_TAG = 'site/tags.json';
export default class Settings { export default class Settings {
//-------------------------- //--------------------------
@ -14,87 +14,101 @@ export default class Settings {
// methods // methods
//-------------------------- //--------------------------
sync(req) { sync(req) {
let self = this;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let payload = req.body; self.loadConfigData()
let user = req.session.user; .then(result => {
let found = _.find(folks, { id: user.id }); let payload = req.body;
let needToUpdate = false; let user = req.session.user;
let response = []; let found = _.find(result.folks, { id: user.id });
if (found) { let needToUpdate = false;
let index = found.id - 1; let response = [];
if ( if (found) {
folks[index].handle != payload.member.handle || let index = found.id - 1;
folks[index].email != payload.member.email if (
) { result.folks[index].handle != payload.member.handle ||
folks[index].handle = payload.member.handle; result.folks[index].email != payload.member.email
folks[index].email = payload.member.email; ) {
fs.writeJson('site/folks.json', folks); result.folks[index].handle = payload.member.handle;
} else { result.folks[index].email = payload.member.email;
//no need to save fs.writeJson('site/folks.json', result.folks);
} } else {
} else { //no need to save
let response = { }
type: DataEvent.REQUEST_LAME, } else {
message: "You're not logged in, champ" let response = {
}; type: DataEvent.REQUEST_LAME,
reject(response); message: "You're not logged in, champ"
} };
reject(response);
}
if (!_.isEqual(settings.global, payload.global)) { if (!_.isEqual(result.settings.global, payload.global)) {
let bg = payload.global.background; let bg = payload.global.background;
payload.global.background = bg.substr(21, bg.length); payload.global.background = bg.substr(21, bg.length);
settings.global = payload.global; result.settings.global = payload.global;
needToUpdate = true; needToUpdate = true;
} else { } else {
//no need to save //no need to save
} }
if (!_.isEqual(settings.email, payload.email)) { if (!_.isEqual(result.settings.email, payload.email)) {
settings.email = payload.email; result.settings.email = payload.email;
needToUpdate = true; needToUpdate = true;
} else { } else {
//no need to save //no need to save
} }
if (needToUpdate) { if (needToUpdate) {
fs.writeJson('site/settings.json', settings) fs.writeJson('site/settings.json', result.settings)
.then(() => { .then(() => {
response = { response = {
type: DataEvent.SETTINGS_UPDATED, type: DataEvent.SETTINGS_UPDATED,
message: 'Settings Saved' message: 'Settings Saved'
}; };
resolve(response); resolve(response);
}) })
.catch(() => { .catch(() => {
//console.error(err); //console.error(err);
}); });
} else { } else {
//no need to update //no need to update
} }
})
.catch(err => {
reject(err);
});
}); });
} }
saveTags(tags) { saveTags(tags) {
let self = this;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!_.isEqual(config.tags, tags)) { self.load(SETTINGS_TAG)
config.tags = tags; .then(config => {
fs.writeJson('site/tags.json', config) if (!_.isEqual(config.tags, tags)) {
.then(() => { config.tags = tags;
fs.writeJson('site/tags.json', config)
.then(() => {
let response = {
type: DataEvent.SETTINGS_UPDATED,
message: 'Settings Saved'
};
resolve(response);
})
.catch(err => {
reject(err);
});
} else {
let response = { let response = {
type: DataEvent.SETTINGS_UPDATED, type: DataEvent.SETTINGS_NOT_UPDATED,
message: 'Settings Saved' message: 'Settings Already Saved'
}; };
resolve(response); resolve(response);
}) }
.catch(err => { })
reject(err); .catch(err => {
}); reject(err);
} else { });
let response = {
type: DataEvent.SETTINGS_NOT_UPDATED,
message: 'Settings Already Saved'
};
resolve(response);
}
}); });
} }
@ -105,6 +119,33 @@ export default class Settings {
fs.writeJSON('site/settings.json', settings); fs.writeJSON('site/settings.json', settings);
}); });
} }
load(fileToLoad) {
return new Promise((resolve, reject) => {
fs.readJSON(fileToLoad)
.then(file => {
resolve(file);
})
.catch(err => {
reject(err);
});
});
}
loadConfigData() {
return new Promise((resolve, reject) => {
let getSettings = this.load(SETTINGS_FILE);
let getFolks = this.load(SETTINGS_FOLKS);
Promise.all([getSettings, getFolks])
.then(result => {
const [settings, folks] = result;
let data = { settings: settings, folks: folks };
resolve(data);
})
.catch(err => {
reject(err);
});
});
}
//-------------------------- //--------------------------
// event handlers // event handlers

@ -1,12 +1,4 @@
import FipamoAPI, {
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
API_GET_NAV,
API_NAV_SYNC
} from '../../libraries/FipamoAPI';
import StringUtils from '../utils/StringUtils'; import StringUtils from '../utils/StringUtils';
import * as DataEvent from '../events/DataEvent';
const api = new FipamoAPI();
export default class PostActions { export default class PostActions {
//-------------------------- //--------------------------
// constructor // constructor
@ -75,44 +67,6 @@ export default class PostActions {
resolve(pageInfo); resolve(pageInfo);
}); });
} }
updateNav(add, id, post) {
api.request(API_GET_NAV, DataEvent.SETTINGS_LOADED)
.then(response => {
let menu = JSON.parse(response.request['response']);
let item = {
id: id,
uuid: post.uuid,
title: post.title,
slug: post.slug
};
if (add) {
menu.push(item);
} else {
for (let index = 0; index < menu.length; index++) {
if (menu[index].id == id) {
menu.splice(index, 1);
}
}
}
api.request(
API_NAV_SYNC,
DataEvent.API_SETTINGS_WRITE,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
menu
)
.then(() => {
//console.log(response);
})
.catch(() => {
//console.log(err);
});
})
.catch(() => {
//console.log(err);
});
}
//-------------------------- //--------------------------
// event handlers // event handlers
//-------------------------- //--------------------------

@ -26,7 +26,7 @@ export default class APIUtils {
this.token = null; this.token = null;
//checks backend to see if user is logged in //checks backend to see if user is logged in
//and requests encrypted token for api calls //and requests encrypted token for api calls
this.request('/api/v1/auth/status').then(r => { this.request(API_STATUS).then(r => {
let response = JSON.parse(r.request['response']); let response = JSON.parse(r.request['response']);
if (response.type === DataEvent.API_REQUEST_GOOD) { if (response.type === DataEvent.API_REQUEST_GOOD) {
this.token = response.token; this.token = response.token;
@ -93,6 +93,6 @@ export default class APIUtils {
//-------------------------- //--------------------------
handleLoadProgress(e) { handleLoadProgress(e) {
this.percentComplete = Math.ceil((e.loaded / e.total) * 100); this.percentComplete = Math.ceil((e.loaded / e.total) * 100);
//this.emitEvent(DataEvent.UPLOAD_PROGRESS); //pass element to display request progress
} }
} }

Loading…
Cancel
Save