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,6 +26,9 @@ export default class Render {
*/ */
publishAll(pages, theme) { publishAll(pages, theme) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
settings
.load(SETTINGS_FILE)
.then(config => {
let response = []; let response = [];
let count = _.filter(pages, page => { let count = _.filter(pages, page => {
return page.metadata.deleted === false; return page.metadata.deleted === false;
@ -79,7 +82,10 @@ export default class Render {
fs.writeFile(writeFile, 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.PAGES_NOT_RENDERED,
message: err
};
reject(response); reject(response);
} }
@ -98,11 +104,18 @@ export default class Render {
//don't render delete pages //don't render delete pages
} }
} }
})
.catch(err => {
reject(err);
});
}); });
} }
publishTags(pages) { publishTags(pages) {
let self = this;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let tags = tags_list.tags; self.loadRenderData()
.then(result => {
let tags = result.tags.tags;
let renderList = []; let renderList = [];
for (let index = 0; index < tags.length; index++) { for (let index = 0; index < tags.length; index++) {
let tag = tags[index]; let tag = tags[index];
@ -112,7 +125,10 @@ export default class Render {
let page = pages[i]; let page = pages[i];
if (_.includes(page.metadata.tags, tag.tag_name)) { if (_.includes(page.metadata.tags, tag.tag_name)) {
pageList.push({ title: page.metadata.title, slug: page.metadata.slug }); pageList.push({
title: page.metadata.title,
slug: page.metadata.slug
});
} }
} }
renderList.push({ tag: tag.tag_name, tag_list: pageList, slug: tag.slug }); renderList.push({ tag: tag.tag_name, tag_list: pageList, slug: tag.slug });
@ -120,12 +136,15 @@ export default class Render {
let response = []; let response = [];
for (let index = 0; index < renderList.length; index++) { for (let index = 0; index < renderList.length; index++) {
let item = renderList[index]; let item = renderList[index];
let file = pug.renderFile('content/themes/' + config.global.theme + '/tags.pug', { let file = pug.renderFile(
'content/themes/' + result.settings.global.theme + '/tags.pug',
{
title: item.tag, title: item.tag,
default_bg: config.global.background, default_bg: result.settings.global.background,
content_tags: 'THESE ARE TAGS', content_tags: 'THESE ARE TAGS',
tag_list: item.tag_list tag_list: item.tag_list
}); }
);
fs.writeFile('public/tags/' + item.slug + '.html', file, err => { fs.writeFile('public/tags/' + item.slug + '.html', file, err => {
// throws an error, you could also catch it here // throws an error, you could also catch it here
@ -141,15 +160,25 @@ export default class Render {
resolve(response); resolve(response);
}); });
} }
})
.catch(err => {
reject(err);
});
}); });
} }
publishArchive(archive) { publishArchive(archive) {
let file = pug.renderFile('content/themes/' + config.global.theme + '/archive.pug', { settings
.load(SETTINGS_FILE)
.then(settings => {
let file = pug.renderFile(
'content/themes/' + settings.global.theme + '/archive.pug',
{
title: 'ARCHIVES', title: 'ARCHIVES',
default_bg: config.global.background, default_bg: settings.global.background,
content_tags: 'COLD STORAGE', content_tags: 'COLD STORAGE',
archives: archive archives: archive
}); }
);
fs.writeFile('public/archives.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
@ -159,6 +188,25 @@ export default class Render {
} }
// success case, the file was saved // success case, the file was saved
}); });
})
.catch(err => {
console.log(err);
});
}
loadRenderData() {
return new Promise((resolve, reject) => {
let getSettings = settings.load(SETTINGS_FILE);
let getTags = settings.load(SETTINGS_TAG);
Promise.all([getSettings, getTags])
.then(result => {
const [settings, tags] = result;
let data = { settings: settings, tags: tags };
resolve(data);
})
.catch(err => {
reject(err);
});
});
} }
//-------------------------- //--------------------------

@ -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,21 +14,24 @@ export default class Settings {
// methods // methods
//-------------------------- //--------------------------
sync(req) { sync(req) {
let self = this;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
self.loadConfigData()
.then(result => {
let payload = req.body; let payload = req.body;
let user = req.session.user; let user = req.session.user;
let found = _.find(folks, { id: user.id }); let found = _.find(result.folks, { id: user.id });
let needToUpdate = false; let needToUpdate = false;
let response = []; let response = [];
if (found) { if (found) {
let index = found.id - 1; let index = found.id - 1;
if ( if (
folks[index].handle != payload.member.handle || result.folks[index].handle != payload.member.handle ||
folks[index].email != payload.member.email result.folks[index].email != payload.member.email
) { ) {
folks[index].handle = payload.member.handle; result.folks[index].handle = payload.member.handle;
folks[index].email = payload.member.email; result.folks[index].email = payload.member.email;
fs.writeJson('site/folks.json', folks); fs.writeJson('site/folks.json', result.folks);
} else { } else {
//no need to save //no need to save
} }
@ -40,24 +43,24 @@ export default class Settings {
reject(response); 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,
@ -71,10 +74,17 @@ export default class Settings {
} 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) => {
self.load(SETTINGS_TAG)
.then(config => {
if (!_.isEqual(config.tags, tags)) { if (!_.isEqual(config.tags, tags)) {
config.tags = tags; config.tags = tags;
fs.writeJson('site/tags.json', config) fs.writeJson('site/tags.json', config)
@ -95,6 +105,10 @@ export default class Settings {
}; };
resolve(response); resolve(response);
} }
})
.catch(err => {
reject(err);
});
}); });
} }
@ -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