Skip to content

Commit b202293

Browse files
committed
feat: hide nodes in the wizard based on "userPermissionCondition"
Implementation of issue #3281
1 parent 9629a8f commit b202293

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

src/helpers/wizardHelper.js

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import _ from 'lodash'
2323
import update from 'react-addons-update'
2424
import { evaluate, getFieldNamesFromExpression, populatePreparedConditions } from 'expression-evaluator'
2525
import { flatten, unflatten } from 'flat'
26+
import { checkPermission } from './permissions'
2627

2728
/**
2829
* @typedef {Object} NodeObject
@@ -392,6 +393,13 @@ export const initWizard = (template, project, productTemplates, incompleteWizard
392393
node
393394
}
394395

396+
// precalculate the result for `userPermissionCondition`
397+
if (nodeObject.userPermissionCondition && !checkPermission(nodeObject.userPermissionCondition)) {
398+
// we calculate this value once as user permissions cannot be changed
399+
// and we would use this value to calculate value of `hiddenByCondition`
400+
nodeObject.__wizard.hiddenByPermission = true
401+
}
402+
395403
// add all found variables from condition to the list of dependant fields of the template
396404
if (nodeObject.condition) {
397405
wizardTemplate.__wizard.dependantFields = _.uniq([
@@ -426,6 +434,11 @@ export const initWizard = (template, project, productTemplates, incompleteWizard
426434
prevWizardStep = getPrevStepToShow(wizardTemplate, lastWizardStep)
427435
}
428436

437+
// if the first step we found is hidden, then search for the next step which is not hidden
438+
if (!isNodeVisible(wizardTemplate, currentWizardStep)) {
439+
currentWizardStep = getStepToShowByDir(wizardTemplate, currentWizardStep, NODE_DIR.NEXT)
440+
}
441+
// use provided `lastWizardStep` or use the first non-hidden step
429442
currentWizardStep = lastWizardStep || currentWizardStep
430443
}
431444

@@ -880,7 +893,9 @@ const getParentNode = (node) => {
880893
...node,
881894
subSectionIndex: -1
882895
}
883-
} else if (node.sectionIndex !== -1) {
896+
// we shouldn't return parent node with all indexes as `-1`
897+
// that's why if we reach this point and `node.sectionIndex === 0` we should also return `null`
898+
} else if (node.sectionIndex !== -1 && node.sectionIndex !== 0) {
884899
return {
885900
...node,
886901
sectionIndex: -1
@@ -1095,8 +1110,10 @@ const getNodeWhichMustBeUpdatedByCondition = (template, flatProjectData) => {
10951110
}
10961111

10971112
forEachNode(template, (nodeObject, node) => {
1098-
if (nodeObject.condition) {
1099-
const hiddenByCondition = !evaluate(nodeObject.condition, flatProjectData)
1113+
if (nodeObject.condition || nodeObject.userPermissionCondition) {
1114+
// take into account the result of `userPermissionCondition` which we keep in `hiddenByPermission`
1115+
const hiddenByCondition = nodeObject.__wizard.hiddenByPermission
1116+
|| nodeObject.condition && !evaluate(nodeObject.condition, flatProjectData)
11001117

11011118
// only update if the condition result has changed
11021119
if (hiddenByCondition !== nodeObject.__wizard.hiddenByCondition) {
@@ -1194,4 +1211,24 @@ export const buildProjectUpdateQueryByQueryParamSelectCondition = (template, que
11941211
})
11951212

11961213
return updateQuery
1214+
}
1215+
1216+
/**
1217+
* Check if node is visible taking into account parent nodes.
1218+
* If any parent node or node itself is hidden, then the node is treated as hidden.
1219+
*
1220+
* @param {Object} template template
1221+
* @param {Node} node node
1222+
*
1223+
* @returns {Boolean} true if node is visible
1224+
*/
1225+
const isNodeVisible = (template, node) => {
1226+
let isVisible = !_.get(getNodeObject(template, node), '__wizard.hiddenByCondition')
1227+
1228+
let tempNode = node
1229+
while (isVisible && (tempNode = getParentNode(tempNode))) {
1230+
isVisible = isVisible && !_.get(getNodeObject(template, tempNode), '__wizard.hiddenByCondition')
1231+
}
1232+
1233+
return isVisible
11971234
}

0 commit comments

Comments
 (0)