@ukhomeoffice/cop-react-form-renderer 6.15.3 → 6.15.4-alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/dist/components/CollectionSummary/CollectionSummary.js +2 -2
  2. package/dist/components/CollectionSummary/SummaryCard.js +2 -2
  3. package/dist/components/FormComponent/Collection.js +2 -2
  4. package/dist/components/FormComponent/Container.js +2 -2
  5. package/dist/components/FormComponent/FormComponent.js +2 -2
  6. package/dist/components/FormComponent/helpers/addLabel.js +2 -3
  7. package/dist/components/FormPage/FormPage.js +7 -20
  8. package/dist/components/FormPage/FormPage.test.js +0 -53
  9. package/dist/components/FormRenderer/FormRenderer.js +8 -2
  10. package/dist/components/FormRenderer/helpers/index.js +1 -3
  11. package/dist/components/FormRenderer/onCYAAction.js +0 -12
  12. package/dist/components/FormRenderer/onCYAAction.test.js +0 -5
  13. package/dist/components/FormRenderer/onPageAction.js +0 -7
  14. package/dist/components/FormRenderer/onPageAction.test.js +0 -5
  15. package/dist/components/PageActions/ActionButton.js +2 -2
  16. package/dist/components/SummaryList/SummaryList.js +2 -2
  17. package/dist/components/TaskList/TaskList.js +2 -2
  18. package/dist/utils/CollectionPage/getQuickEditPage.js +2 -2
  19. package/dist/utils/CollectionPage/mergeCollectionPages.js +0 -1
  20. package/package.json +1 -1
  21. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-chained-component-show-whens-containerised.json +0 -119
  22. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-chained-component-show-whens-dependent-component-nested-block.json +0 -112
  23. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-chained-component-show-whens-dependent-component-nested.json +0 -105
  24. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-chained-component-show-whens-refdata.json +0 -105
  25. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-chained-component-show-whens-target-component-nested.json +0 -98
  26. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-chained-component-show-whens.json +0 -105
  27. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-chained-show-whens-page-hidden.json +0 -112
  28. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-cop-airpax.json +0 -26407
  29. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-cop-mandec.json +0 -9404
  30. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-hidden-collection-component-dependent-on-external-data.json +0 -157
  31. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-hidden-collection-component.json +0 -85
  32. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-hidden-component-referring-to-collection.json +0 -172
  33. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-hidden-component-referring-to-hidden-collection.json +0 -201
  34. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-hidden-component-show-when-in-component-and-page.json +0 -62
  35. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-hidden-component-with-nested-questions.json +0 -111
  36. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-hidden-component.json +0 -57
  37. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-hidden-containerised-component.json +0 -96
  38. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-hidden-embedded-collection-component.json +0 -82
  39. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-hidden-embedded-component.json +0 -54
  40. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-hidden-multilevel-containerised-component-leaf-hidden.json +0 -105
  41. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-hidden-multilevel-containerised-component.json +0 -105
  42. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-hidden-options.json +0 -98
  43. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-hidden-page-collection.json +0 -127
  44. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-hidden-page-component-used-elsewhere.json +0 -108
  45. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-hidden-page-same-component-reused.json +0 -61
  46. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-hidden-page.json +0 -95
  47. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-nested-answers-hidden-by-option.json +0 -111
  48. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-page-same-component-reused-one-shown.json +0 -74
  49. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/cop-airpax-change-what-happened-before.json +0 -300
  50. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/cop-airpax-remove-photos-before.json +0 -384
  51. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/cop-mandec-remove-business-interests-before.json +0 -140
  52. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/cop-mandec-remove-criminality-before.json +0 -186
  53. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/cop-mandec-remove-unspent-convictions-before.json +0 -143
  54. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-chained-component-show-whens-containerised.json +0 -11
  55. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-chained-component-show-whens-dependent-component-nested-block.json +0 -10
  56. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-chained-component-show-whens-dependent-component-nested.json +0 -9
  57. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-chained-component-show-whens-refdata.json +0 -11
  58. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-chained-component-show-whens-target-component-nested.json +0 -9
  59. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-chained-component-show-whens.json +0 -9
  60. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-chained-show-whens-page-hidden.json +0 -9
  61. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-hidden-collection-component-dependent-on-external-data.json +0 -26
  62. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-hidden-collection-component.json +0 -15
  63. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-hidden-component-referring-to-collection.json +0 -25
  64. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-hidden-component-referring-to-hidden-collection.json +0 -26
  65. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-hidden-component-show-when-in-component-and-page.json +0 -6
  66. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-hidden-component-with-nested-questions.json +0 -12
  67. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-hidden-component.json +0 -5
  68. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-hidden-containerised-component.json +0 -11
  69. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-hidden-embedded-collection-component.json +0 -15
  70. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-hidden-embedded-component.json +0 -5
  71. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-hidden-multilevel-containerised-component-leaf-hidden.json +0 -14
  72. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-hidden-multilevel-containerised-component.json +0 -14
  73. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-hidden-options.json +0 -10
  74. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-hidden-page-collection.json +0 -18
  75. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-hidden-page-component-used-elsewhere.json +0 -9
  76. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-hidden-page-same-component-reused.json +0 -6
  77. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-hidden-page.json +0 -9
  78. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-nested-answers-hidden-by-option.json +0 -12
  79. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-page-same-component-reused-one-shown.json +0 -8
  80. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/cop-airpax-change-what-happened-after.json +0 -280
  81. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/cop-airpax-remove-photos-after.json +0 -355
  82. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/cop-mandec-base-file.json +0 -80
  83. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/cop-mandec-remove-business-interests-after.json +0 -122
  84. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/cop-mandec-remove-criminality-after.json +0 -86
  85. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/cop-mandec-remove-unspent-convictions-after.json +0 -127
  86. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-chained-component-show-whens-containerised-removed.json +0 -11
  87. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-chained-component-show-whens-dependent-component-nested-block-removed.json +0 -7
  88. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-chained-component-show-whens-dependent-component-nested-removed.json +0 -7
  89. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-chained-component-show-whens-refdata-removed.json +0 -7
  90. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-chained-component-show-whens-removed.json +0 -7
  91. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-chained-component-show-whens-target-component-nested-removed.json +0 -6
  92. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-chained-show-whens-page-hidden-removed.json +0 -6
  93. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-hidden-collection-component-dependent-on-external-data-removed.json +0 -23
  94. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-hidden-collection-component-removed.json +0 -4
  95. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-hidden-component-referring-to-collection-removed.json +0 -21
  96. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-hidden-component-referring-to-hidden-collection-removed.json +0 -6
  97. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-hidden-component-removed.json +0 -4
  98. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-hidden-component-show-when-in-component-and-page-removed.json +0 -5
  99. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-hidden-component-with-nested-questions-removed.json +0 -10
  100. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-hidden-containerised-component-removed.json +0 -10
  101. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-hidden-embedded-collection-component-removed.json +0 -4
  102. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-hidden-embedded-component-removed.json +0 -4
  103. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-hidden-multilevel-containerised-component-leaf-hidden-removed.json +0 -13
  104. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-hidden-multilevel-containerised-component-removed.json +0 -10
  105. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-hidden-options-removed.json +0 -8
  106. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-hidden-page-collection-removed.json +0 -15
  107. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-hidden-page-component-used-elsewhere-removed.json +0 -5
  108. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-hidden-page-removed.json +0 -4
  109. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-hidden-page-same-component-reused-removed.json +0 -5
  110. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-nested-answers-hidden-by-option-removed.json +0 -11
  111. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-page-same-component-reused-one-shown-removed.json +0 -7
  112. package/dist/components/FormRenderer/helpers/clearOutUncompletedRoutes.js +0 -323
  113. package/dist/components/FormRenderer/helpers/clearOutUncompletedRoutes.test.js +0 -349
  114. package/dist/components/FormRenderer/helpers/clearOutUncompletedRoutesUtils.js +0 -381
  115. package/dist/components/FormRenderer/helpers/clearOutUncompletedRoutesUtils.test.js +0 -559
@@ -1,323 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- var _mergeCollectionPages = _interopRequireDefault(require("../../../utils/CollectionPage/mergeCollectionPages"));
8
- var Utils = _interopRequireWildcard(require("./clearOutUncompletedRoutesUtils"));
9
- var _optionIsSelected = _interopRequireDefault(require("../../../utils/Component/optionIsSelected"));
10
- function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
11
- function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
12
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
13
- function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
14
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
15
- function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
16
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
17
- function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /* eslint-disable no-param-reassign */
18
- /**
19
- *
20
- * This function recursively reads in the non-collection pages and components and builds lists from it:
21
- *
22
- * 1: allComponents. This is a map of all components in the form, keyed by the fully qualified component path.
23
- * If the component belongs to a page (or other parent entity) that has its own show_when rule, then this rule
24
- * is combined with the show_when rule of the component, so the decision as to whether the component's data should
25
- * exist can be made just by processing the component.
26
- * The main purpose of this map is to allow us to build a dependency graph in the function createComponentDependenciesGraph
27
- *
28
- * 2: componentsToKeep: This is an object containing a count of how many times each component is defined. This is to support the
29
- * fact that a component can be used more than once in a form (but with mutually exclusive show_when rules so only used
30
- * once with the same path).
31
- *
32
- * @param {Array} condensedPages All pages in the form, with the collection pages collated into a single object per collection
33
- * @param {Object} formData The form payload
34
- * @param {Map} componentByIdMap Map of all components, to enable performant lookup by id
35
- * @param {Map} componentByFieldIdMap Map of all components, to enable performant lookup by fieldId
36
- * @param {Object}
37
- * @return {Map, Object, Map} allComponents All components in form (including nested), keyed by path:
38
- * componentsToKeep A count of how many times each component is used, to prevent us deleting components defined > 1 times
39
- * allCollections A map of collection objects, which are a grouping of the pages that make up a single collection
40
- */
41
- const createComponentMapsFromForm = (condensedPages, componentByIdMap, componentByFieldIdMap, formData) => {
42
- const allComponents = new Map();
43
- const componentsToKeep = {};
44
- const allCollections = new Map();
45
-
46
- /*
47
- * Inner function to support the recursion required to traverse through the nested structures of the form
48
- */
49
- const recursivelyMapFieldsAndDeleteHiddenNested = (page, component, path) => {
50
- var _component$data;
51
- if (!component.fieldId) return; // Many component, such as warnings, html and details do not have data so we can ignore them
52
- path = path ? "".concat(path, ".").concat(component.fieldId) : component.fieldId; // Build up the path to reflect nested components
53
- componentsToKeep[path] = (componentsToKeep[path] || 0) + 1; // We need to keep track of the count of each component path, to avoid pruning paths that exist elsewhere
54
- component.path = path; // Add the fully qualified path to the component, which will be required when resolving the dependencies later
55
-
56
- // If the parent page has a rule, combine it with any component rule as we will be resolving dependencies for components only
57
- if (page.show_when) {
58
- component.show_when = component.show_when ? [].concat(Utils.toArray(page.show_when), Utils.toArray(component.show_when)) : page.show_when;
59
- }
60
- Utils.addValue(path, component, allComponents); // There can be more than one component per path, so keep a map of <path, array>
61
- // recurse if there is nesting
62
- if (component.components) {
63
- component.components.forEach(c => {
64
- recursivelyMapFieldsAndDeleteHiddenNested(page, c, path, allComponents, componentsToKeep, formData);
65
- });
66
- } else if (component !== null && component !== void 0 && (_component$data = component.data) !== null && _component$data !== void 0 && _component$data.options) {
67
- var _component$data2;
68
- component === null || component === void 0 || (_component$data2 = component.data) === null || _component$data2 === void 0 || (_component$data2 = _component$data2.options) === null || _component$data2 === void 0 || _component$data2.forEach(option => {
69
- if (option.nested) {
70
- if (!(0, _optionIsSelected.default)(formData[component.id], option)) {
71
- option.nested.forEach(nestedComponent => {
72
- // delete hidden nested question payload items when the parent option is not selected
73
- Utils.deleteNodeByPath(formData, Utils.getNestedQuestionPath(path, nestedComponent.fieldId));
74
- });
75
- } else {
76
- // If the option is selected, then add any nested components to the allComponents list. If the nested block has a show_when, pass it down to the child questions
77
- const blockShowWhen = option.show_when;
78
- option.nested.forEach(nestedComponent => {
79
- if (blockShowWhen) nestedComponent.show_when = blockShowWhen;
80
- recursivelyMapFieldsAndDeleteHiddenNested(page, nestedComponent, Utils.getImmediateParent(path), allComponents, componentsToKeep, formData);
81
- });
82
- }
83
- }
84
- });
85
- }
86
- };
87
-
88
- // Entry point for the recursive traverse of the form specification.
89
- condensedPages === null || condensedPages === void 0 || condensedPages.forEach(page => {
90
- // Make the distinction between collections (which will be processed separately) and non-collections, which will be processed here.
91
- if (page.childPages) {
92
- allCollections.set(page.collection.name, page);
93
- } else {
94
- var _page$components;
95
- (_page$components = page.components) === null || _page$components === void 0 || _page$components.forEach(useComponentInPage => {
96
- const componentObj = Utils.findComponentDefinitionInForm(useComponentInPage, componentByIdMap, componentByFieldIdMap);
97
- recursivelyMapFieldsAndDeleteHiddenNested(page, componentObj, null, allComponents, componentsToKeep, formData);
98
- });
99
- }
100
- });
101
- return {
102
- allComponents,
103
- componentsToKeep,
104
- allCollections
105
- };
106
- };
107
- /**
108
- *
109
- * This function iterates through the allComponents list. For each component, it will derive all payload paths
110
- * that this component is dependent on. The same component might be defined more than once in the form, so add the dependencies
111
- * for all the component definitions.
112
- *
113
- * @param {Map} allDependencyRelationships A map of each component with dependencies. The key is the fully qualified path and the value is a Set of all the paths that this component is dependent on.
114
- * @param {Map} allComponents All components in form (including nested), keyed by path. Used to build the allDependencyRelationships
115
- * @return {Map }allDependencyRelationships graph of the relationships between all entities
116
- */
117
- const createComponentDependenciesGraph = allComponents => {
118
- const allDependencyRelationships = new Map();
119
- allComponents === null || allComponents === void 0 || allComponents.forEach(componentArray => {
120
- componentArray === null || componentArray === void 0 || componentArray.forEach(component => {
121
- if (!component.fieldId) return;
122
- const componentDependencies = Utils.getDependencies(component);
123
- if (componentDependencies) {
124
- if (allDependencyRelationships.has(component.path)) {
125
- const existingSet = allDependencyRelationships.get(component.path);
126
- componentDependencies.forEach(dep => existingSet.add(dep));
127
- } else {
128
- allDependencyRelationships.set(component.path, new Set(componentDependencies));
129
- }
130
- }
131
- });
132
- });
133
- return allDependencyRelationships;
134
- };
135
-
136
- /**
137
- *
138
- * This function loops through each entry in the allDependencyRelationships map. Each entry will contain a Set of all
139
- * paths that the key path is dependent on. Recursively delve into each dependency path and repeat the process until we
140
- * have reached an entry that has no dependencies. At this point we can safely resolve the dependency using its show_when rule,
141
- * and unwind the recursion.
142
- *
143
- * Because all the components are inter-connected, this could result in resolving the same entry repeatedly, so keep a 'visited'
144
- * array to prevent this.
145
- *
146
- * @param {Object} formData The form payload
147
- * @param {Map} allComponents All components in form (including nested), keyed by path
148
- * @param {Object} componentsToKeep A count of how many times each component is used, to prevent us deleting components defined > 1 times
149
- * @param {Map} allDependencyRelationships A map of each component with dependencies. The key is the fully qualified path and the value is a Set of all the paths that this component is dependent on.
150
- * @param {Map} allCollections A map of collection objects, which are a grouping of the pages that make up a single collection
151
- * @param {Object} form The form specification
152
- */
153
- const resolveComponentDependenciesGraph = (allDependencyRelationships, allComponents, componentsToKeep, allCollections, form, formData) => {
154
- /*
155
- * Inner function to support the recursion required to traverse through the nested nature of the allDependencyRelationships
156
- */
157
- const recursivelyResolveDependencies = (visited, dependentComponent, dependencyRelationships) => {
158
- if (visited.has(dependentComponent)) return;
159
- visited.add(dependentComponent);
160
- const dependencies = dependencyRelationships.get(dependentComponent.path);
161
- dependencies === null || dependencies === void 0 || dependencies.forEach(dependencyPath => {
162
- const dependencyComponents = Utils.getDependencyObjectFromPath(dependencyPath, allComponents);
163
- if (dependencyComponents) {
164
- dependencyComponents.forEach(dependency => {
165
- if (!visited.has(dependency)) {
166
- recursivelyResolveDependencies(visited, dependency, dependencyRelationships);
167
- }
168
- });
169
- } else {
170
- // We are here if the dependency path was not found in the list of allComponents. This is valid and can happen when
171
- // a component is dependent on a field that was generated by cop-ui, e.g. jobHolderStaffDetails.linemanagerEmail.
172
- // However, there is an edge case (described in the message below) that we need to validate for.
173
- const arrayNamePattern = /([a-zA-Z_$][\w$]*)\[\d+\]/;
174
- const match = dependencyPath.match(arrayNamePattern);
175
- if (match) {
176
- const collectionName = match[1];
177
- const collection = allCollections.get(collectionName);
178
- if (collection) {
179
- var _collection$childPage;
180
- (_collection$childPage = collection.childPages) === null || _collection$childPage === void 0 || _collection$childPage.forEach(childPage => {
181
- if (childPage.show_when) {
182
- const message = "It is not possible to reliably clean hidden data when a component is dependent on a \n collection's data, and that collection is itself dependent on data elsewhere in the form.\n The dependency path is ".concat(dependencyPath, ", and the chained show_when is ").concat(JSON.stringify(childPage.show_when), ".\n The form will need to be changed to break this chained dependency");
183
- throw new Error(message);
184
- }
185
- ;
186
- });
187
- }
188
- }
189
- }
190
- });
191
- // Whilst unravelling the recursion, we can start deleting from the leaf upwards
192
- if (!Utils.isShowEntity(dependentComponent, formData)) {
193
- Utils.deleteComponentData(formData, dependentComponent.path, dependentComponent, componentsToKeep);
194
- }
195
- };
196
- const visited = new Set();
197
- allDependencyRelationships === null || allDependencyRelationships === void 0 || allDependencyRelationships.forEach((dependencies, dependentComponentPath) => {
198
- // Each path that a component is dependent on may have > 1 possible dependent components, if a field is used twice
199
- const dependentComponents = allComponents.get(dependentComponentPath);
200
- dependentComponents === null || dependentComponents === void 0 || dependentComponents.forEach(dependentComponent => {
201
- if (!visited.has(dependentComponent)) {
202
- recursivelyResolveDependencies(visited, dependentComponent, allDependencyRelationships, form, formData, allComponents, componentsToKeep, allCollections);
203
- }
204
- });
205
- });
206
- };
207
-
208
- /**
209
- * For each collection, iterate through the payload array's objects, and apply the collections show_when
210
- * rules for each payload object.
211
- *
212
- * Some of the rules may be dependent on payload items outside the collection. These will already have been cleansed
213
- * so will be in a reliable state.
214
- *
215
- * @param {Map} allCollections A map of collection objects, which are a grouping of the pages that make up a single collection
216
- * @param {Object} formData The form payload
217
- * @param {Map} componentByIdMap Map of all components, to enable performant lookup by id
218
- * @param {Map} componentByFieldIdMap Map of all components, to enable performant lookup by fieldId
219
- */
220
- const cleanseCollectionData = (allCollections, formData, componentByIdMap, componentByFieldIdMap) => {
221
- allCollections === null || allCollections === void 0 || allCollections.forEach((collection, collectionName) => {
222
- const collectionDataArray = formData[collectionName];
223
- collectionDataArray === null || collectionDataArray === void 0 || collectionDataArray.forEach(collectionDataEntry => {
224
- var _collection$childPage2;
225
- // The forms are written such that a show_when in a collection array object can be evaluated against the data relative to that data object
226
- // OR against the data relative to the root of the form. To achieve that, we'll copy the collection object to the root of the form before evaluating
227
- const dataForEvaluation = _objectSpread(_objectSpread({}, formData), collectionDataEntry);
228
- const pathsToKeep = new Set();
229
- const componentsToPrune = new Map();
230
- (_collection$childPage2 = collection.childPages) === null || _collection$childPage2 === void 0 || _collection$childPage2.forEach(childPage => {
231
- var _childPage$components;
232
- const showPage = Utils.isShowEntity(childPage, dataForEvaluation);
233
- (_childPage$components = childPage.components) === null || _childPage$components === void 0 || _childPage$components.forEach(useComponentInPage => {
234
- const componentObj = Utils.findComponentDefinitionInForm(useComponentInPage, componentByIdMap, componentByFieldIdMap);
235
- // Non-data components can be ignored (eg html)
236
- if (!componentObj.fieldId) return;
237
- const showComponentOnPage = Utils.isShowEntity(useComponentInPage, dataForEvaluation);
238
- if (showPage && showComponentOnPage && Utils.isShowEntity(componentObj, dataForEvaluation)) {
239
- // There may be >1 components with the same path (eg quantity in EAB2), so don't delete hidden components if they are required elsewhere
240
- pathsToKeep.add(componentObj.fieldId);
241
- } else {
242
- componentsToPrune.set(componentObj.id, componentObj);
243
- }
244
- });
245
- });
246
- Utils.pruneCollectionEntry(pathsToKeep, componentsToPrune, collectionDataEntry, formData);
247
- });
248
- //
249
- if (collectionDataArray) Utils.removeObjectWithOnlySingleIdField(collectionDataArray);
250
- });
251
- };
252
-
253
- /**
254
- *
255
- * The purpose of this function is to apply the show_when rules defined in the form specification to the
256
- * form payload (formData), and remove any data that should not be shown according to those rules.
257
- * (the reason this data exists in the payload is that users can answer a question in such a way that
258
- * other questions are 'shown' to them, which are then complete. The user can then go back and change the original
259
- * question to then 'hide' those questions, but the answers they gave persist in the payload. The must be cleansed
260
- * before being submitted.
261
- *
262
- * There are 2 types of payload data that have to be treated differently by this function for it to work.
263
- *
264
- * 1. Data from non-repeating pages, containing components.
265
- *
266
- * Each data item captured will be written to the payload as a field with the name of the fieldId of the payload.
267
- * In the case of components that are nested within 'container' components, the payload will reflect the nesting.
268
- * There is no limit to the level of nesting in a form.
269
- *
270
- * To cleanse the payload for this type of data, we do the following (high level description, see method comments for detail):
271
- *
272
- * - build a map of all components, keyed by their payload path (required for the next step)
273
- * - for all of these components that have dependencies (show_when rules), create a graph datastructure to show all components
274
- * on which a component is dependent, and then all components that those components may be dependent on. There is no limit to
275
- * the depth of these chained dependencies.
276
- * - the reason for building a graph of these dependency chains is so we know the sequence in which we must resolve the dependencies.
277
- * For any given chain of dependencies it is essential that we resolve the dependencies starting at the end of the chains, and work our way
278
- * back up the chain. If not, we could be resolving a dependency based on a payload item that itself will later be deleted.
279
- * Therefore, the final step is to recursively traverse the dependency graph, resolving the dependency rule for the components in
280
- * the chain from the leaf back up to the parent node.
281
- *
282
- * 2. Page collection data
283
- *
284
- * A page collection is a set of one or more pages which (as a group) can be filled in as many times is required
285
- * by the user. For example, in EAB, you can add as many item-seal-details to an EAB form as are required. An item-seal-detail
286
- * is made up of several different pages, all grouped. The data saved will be an array representing the whole collection, with
287
- * each object in the array representing a single collection entry (a single item-seal-detail in this example).
288
- *
289
- * Cleanse collection data has to be driven from the payload rather than the form, as there are repeated objects in an array
290
- * representing the data. Each one has to be treated independently as the data to be cleansed can be different in each one.
291
- *
292
- * - For each collection type, iterate through each payload object and treat it like an independent payload..
293
- * - For each collection payload object, iterate through the collection's pages and components, and apply all the show_when rules
294
- * found at both page and component level to the payload.
295
- * - Repeat for each payload object
296
- * - Repeat for all collections
297
- *
298
- * @param {*} form
299
- * @param {*} formData
300
- * @returns {*} cleansed form data
301
- */
302
- const clearOutUncompletedRoutes = (form, formData) => {
303
- // Load components into maps keyed on id and field for subsequent performant access
304
- const componentByIdMap = new Map(form.components.map(c => [c.id, c]));
305
- const componentByFieldIdMap = new Map(form.components.map(c => [c.fieldId, c]));
306
-
307
- // Group all pages relating to a page-collection into a single object to aid collection
308
- // processing, and to allow us to differentiate between a collection and non-collection page.
309
- const condensedPages = (0, _mergeCollectionPages.default)(form.pages);
310
- const {
311
- allComponents,
312
- componentsToKeep,
313
- allCollections
314
- } = createComponentMapsFromForm(condensedPages, componentByIdMap, componentByFieldIdMap, formData);
315
- const allDependencyRelationships = createComponentDependenciesGraph(allComponents);
316
- resolveComponentDependenciesGraph(allDependencyRelationships, allComponents, componentsToKeep, allCollections, form, formData);
317
- cleanseCollectionData(allCollections, formData, componentByIdMap, componentByFieldIdMap);
318
-
319
- // The cleansing above may have left empty arrays and collection IDs. Tidy these up.
320
- Utils.removeEmptyArraysAndUnusedCollectionIDs(formData);
321
- return formData;
322
- };
323
- var _default = exports.default = clearOutUncompletedRoutes;