♻️ Refactors the config accumulator into a Class

This commit is contained in:
Alicia Sykes 2021-07-04 09:00:53 +01:00
parent 87c592c399
commit a27316d597

View File

@ -3,51 +3,58 @@
* Also ensures that any missing attributes are populated with defaults, and the * Also ensures that any missing attributes are populated with defaults, and the
* object is structurally sound, to avoid any error if the user is missing something * object is structurally sound, to avoid any error if the user is missing something
* The main config object is made up of three parts: appConfig, pageInfo and sections * The main config object is made up of three parts: appConfig, pageInfo and sections
* For anything other than these three sections, please see @utils/ConfigHelpers.js
*/ */
import Defaults, { localStorageKeys } from '@/utils/defaults'; import {
localStorageKeys,
appConfig as defaultAppConfig,
pageInfo as defaultPageInfo,
iconSize as defaultIconSize,
layout as defaultLayout,
} from '@/utils/defaults';
import conf from '../../public/conf.yml'; import conf from '../../public/conf.yml';
/** export default class ConfigAccumulator {
* Returns the appConfig section, as JSON constructor() {
*/ this.conf = conf;
export const appConfig = (() => { }
const appConfigFile = conf.appConfig || {};
let usersAppConfig = Defaults.appConfig; /* App Config */
appConfig() {
const appConfigFile = this.conf.appConfig || {};
let usersAppConfig = defaultAppConfig;
if (localStorage[localStorageKeys.APP_CONFIG]) { if (localStorage[localStorageKeys.APP_CONFIG]) {
usersAppConfig = JSON.parse(localStorage[localStorageKeys.APP_CONFIG]); usersAppConfig = JSON.parse(localStorage[localStorageKeys.APP_CONFIG]);
} else if (appConfigFile !== {}) { } else if (appConfigFile !== {}) {
usersAppConfig = appConfigFile; usersAppConfig = appConfigFile;
} }
usersAppConfig.layout = localStorage[localStorageKeys.LAYOUT_ORIENTATION] usersAppConfig.layout = localStorage[localStorageKeys.LAYOUT_ORIENTATION]
|| appConfigFile.layout || Defaults.layout; || appConfigFile.layout || defaultLayout;
usersAppConfig.iconSize = localStorage[localStorageKeys.ICON_SIZE] usersAppConfig.iconSize = localStorage[localStorageKeys.ICON_SIZE]
|| appConfigFile.iconSize || Defaults.iconSize; || appConfigFile.iconSize || defaultIconSize;
return usersAppConfig; return usersAppConfig;
})(); }
/** /* Page Info */
* Returns the pageInfo section, as JSON pageInfo() {
*/ const defaults = defaultPageInfo;
export const pageInfo = (() => {
const defaults = Defaults.pageInfo;
let localPageInfo; let localPageInfo;
try { try {
localPageInfo = JSON.parse(localStorage[localStorageKeys.PAGE_INFO]); localPageInfo = JSON.parse(localStorage[localStorageKeys.PAGE_INFO]);
} catch (e) { } catch (e) {
localPageInfo = {}; localPageInfo = {};
} }
const pi = conf.pageInfo || defaults; // The page info object to return const pi = this.conf.pageInfo || defaults; // The page info object to return
pi.title = localPageInfo.title || conf.pageInfo.title || defaults.title; pi.title = localPageInfo.title || conf.pageInfo.title || defaults.title;
pi.description = localPageInfo.description || conf.pageInfo.description || defaults.description; pi.description = localPageInfo.description || conf.pageInfo.description || defaults.description;
pi.navLinks = localPageInfo.navLinks || conf.pageInfo.navLinks || defaults.navLinks; pi.navLinks = localPageInfo.navLinks || conf.pageInfo.navLinks || defaults.navLinks;
pi.footerText = localPageInfo.footerText || conf.pageInfo.footerText || defaults.footerText; pi.footerText = localPageInfo.footerText || conf.pageInfo.footerText || defaults.footerText;
return pi; return pi;
})(); }
/** /* Sections */
* Returns the sections section, as an array of JSON objects sections() {
*/
export const sections = (() => {
// If the user has stored sections in local storage, return those // If the user has stored sections in local storage, return those
const localSections = localStorage[localStorageKeys.CONF_SECTIONS]; const localSections = localStorage[localStorageKeys.CONF_SECTIONS];
if (localSections) { if (localSections) {
@ -59,17 +66,15 @@ export const sections = (() => {
} }
} }
// If the function hasn't yet returned, then return the config file sections // If the function hasn't yet returned, then return the config file sections
return conf.sections; return this.conf.sections;
})(); }
/** /* Complete config */
* Returns the complete configuration, as JSON config() {
*/ return {
export const config = (() => { appConfig: this.appConfig(),
const result = { pageInfo: this.pageInfo(),
appConfig, sections: this.sections(),
pageInfo,
sections,
}; };
return result; }
})(); }