From e381d0d15a55ddbd80267b4ae71b010f72cdf1af Mon Sep 17 00:00:00 2001 From: Lachlan Kermode Date: Tue, 30 Jun 2020 12:55:13 +0200 Subject: [PATCH] add USE_FILTER_DESCRIPTIONS option --- src/actions/index.js | 4 ++-- src/components/Layout.js | 10 ++++----- src/reducers/domain.js | 2 +- src/reducers/validate/validators.js | 32 +++++++++++++++++++++++------ 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/actions/index.js b/src/actions/index.js index 06ecc2a..1ddd64a 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -137,10 +137,10 @@ export function fetchError (message) { } export const UPDATE_DOMAIN = 'UPDATE_DOMAIN' -export function updateDomain (domain) { +export function updateDomain (payload) { return { type: UPDATE_DOMAIN, - domain + payload } } diff --git a/src/components/Layout.js b/src/components/Layout.js index 41ac954..fd50d98 100644 --- a/src/components/Layout.js +++ b/src/components/Layout.js @@ -40,12 +40,10 @@ class Dashboard extends React.Component { componentDidMount () { if (!this.props.app.isMobile) { this.props.actions.fetchDomain() - .then(domain => this.props.actions.updateDomain(domain)) - .then(({ domain }) => { - if (domain.categories.length >= 4) { - this.props.actions.updateDimensions({ marginTop: 0 }) - } - }) + .then(domain => this.props.actions.updateDomain({ + domain, + features: this.props.features + })) } // NOTE: hack to get the timeline to always show. Not entirely sure why // this is necessary. diff --git a/src/reducers/domain.js b/src/reducers/domain.js index c0c3c27..0520b05 100644 --- a/src/reducers/domain.js +++ b/src/reducers/domain.js @@ -6,7 +6,7 @@ import { validateDomain } from './validate/validators.js' function updateDomain (domainState, action) { return { ...domainState, - ...validateDomain(action.domain) + ...validateDomain(action.payload.domain, action.payload.features) } } diff --git a/src/reducers/validate/validators.js b/src/reducers/validate/validators.js index 109794c..6da14d9 100644 --- a/src/reducers/validate/validators.js +++ b/src/reducers/validate/validators.js @@ -29,10 +29,30 @@ const isLeaf = node => (Object.keys(node.children).length === 0) const isDuplicate = (node, set) => { return (set.has(node.key)) } /* -* Traverse a filter tree and check its duplicates +* Traverse a filter tree and check its duplicates. Also recompose as +* description if `features.USE_FILTER_DESCRIPTIONS` is true. */ -function validateTree (node, parent, set, duplicates) { - if (!Array.isArray(node) || !node.length) { +function validateFilterTree (node, parent, set, duplicates, hasFilterDescriptions) { + if (hasFilterDescriptions) { + if (node.key === '_root') { + node.isDescription = true // setting first set of nodes to values + } else if (!parent.isDescription) { + node.isDescription = true + } else { + node.isDescription = false + } + + if (node.isDescription && node.key !== 'root') { + parent.description = node.key + parent.children = node.children + delete parent.isDescription + } + if (isLeaf(node)) { + delete parent.isDescription + } + } + + if (typeof (node) !== 'object' || typeof (node.children) !== 'object') { return } // If it's a leaf, check that it's not duplicate @@ -49,7 +69,7 @@ function validateTree (node, parent, set, duplicates) { } else { // If it's not a leaf, simply keep going Object.values(node.children).forEach((childNode) => { - validateTree(childNode, node, set, duplicates) + validateFilterTree(childNode, node, set, duplicates, hasFilterDescriptions) }) } } @@ -57,7 +77,7 @@ function validateTree (node, parent, set, duplicates) { /* * Validate domain schema */ -export function validateDomain (domain) { +export function validateDomain (domain, features) { const sanitizedDomain = { events: [], categories: [], @@ -143,7 +163,7 @@ export function validateDomain (domain) { // Validate uniqueness of filters const filterSet = new Set([]) const duplicateFilters = [] - validateTree(domain.filters, {}, filterSet, duplicateFilters) + validateFilterTree(domain.filters, {}, filterSet, duplicateFilters, features.USE_FILTER_DESCRIPTIONS) // Duplicated filters if (duplicateFilters.length > 0) {