From ec95fafe04ae9620d2db12ce6dbbe7049dc9a5d9 Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Sun, 4 Jul 2021 09:01:56 +0100 Subject: [PATCH] :zap: Adds a helper function, to process component visiblity --- src/utils/ConfigHelpers.js | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/utils/ConfigHelpers.js diff --git a/src/utils/ConfigHelpers.js b/src/utils/ConfigHelpers.js new file mode 100644 index 00000000..365d1b19 --- /dev/null +++ b/src/utils/ConfigHelpers.js @@ -0,0 +1,42 @@ +import ConfigAccumulator from '@/utils/ConfigAccumalator'; + +import { visibleComponents } from '@/utils/defaults'; + +/** + * Initiates the Accumulator class and generates a complete config object + * Self-executing function, returns the full user config as a JSON object + */ +export const config = (() => { + const Accumulator = new ConfigAccumulator(); + return Accumulator.config(); +})(); + +/** + * Generates an object containing booleans indicating which + * components should be hidden. This enables the user to hide + * parts of the page and disable functionality that they don't need/ want + * All options fallback on the values defined in the defaults + * @param {object} appConfig The full app config + * @returns {object} result + */ +export const componentVisibility = (appConfig) => { + // Get users choice from app config + const usersChoice = appConfig.hideComponents || {}; + // Checks if value is defined, and is a boolean + const isThere = (userValue) => typeof userValue === 'boolean'; + // For each option, return users choice (if specified), else use the default + return { + pageTitle: isThere(usersChoice.hideHeading) + ? !usersChoice.hideHeading : visibleComponents.pageTitle, + navigation: isThere(usersChoice.hideNav) + ? !usersChoice.hideNav : visibleComponents.navigation, + searchBar: isThere(usersChoice.hideSearch) + ? !usersChoice.hideSearch : visibleComponents.searchBar, + settings: isThere(usersChoice.hideSettings) + ? !usersChoice.hideSettings : visibleComponents.settings, + footer: isThere(usersChoice.hideFooter) + ? !usersChoice.hideFooter : visibleComponents.footer, + splashScreen: isThere(usersChoice.hideSplashScreen) + ? !usersChoice.hideSplashScreen : visibleComponents.splashScreen, + }; +};