@ukhomeoffice/cop-react-form-renderer 3.17.2 → 3.18.0

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.
@@ -229,15 +229,15 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
229
229
  if (hubDetails !== null && hubDetails !== void 0 && hubDetails.sections) {
230
230
  var _data$formStatus2;
231
231
 
232
- var tasks = data !== null && data !== void 0 && (_data$formStatus2 = data.formStatus) !== null && _data$formStatus2 !== void 0 && _data$formStatus2.tasks ? data.formStatus.tasks : {};
232
+ var tasks = (data === null || data === void 0 ? void 0 : (_data$formStatus2 = data.formStatus) === null || _data$formStatus2 === void 0 ? void 0 : _data$formStatus2.tasks) || {};
233
233
 
234
- var updatedSections = _helpers.default.getUpdatedSectionStates(hubDetails.sections, tasks);
234
+ var updatedSections = _helpers.default.getUpdatedSectionStates(hubDetails.sections, tasks, hubDetails.nonSequential);
235
235
 
236
236
  setHubDetails(function (prev) {
237
237
  return _objectSpread(_objectSpread({}, prev), updatedSections);
238
238
  });
239
239
  }
240
- }, [currentTask.fullPages, data, hubDetails === null || hubDetails === void 0 ? void 0 : hubDetails.sections]);
240
+ }, [currentTask.fullPages, data, hubDetails === null || hubDetails === void 0 ? void 0 : hubDetails.sections, hubDetails === null || hubDetails === void 0 ? void 0 : hubDetails.nonSequential]);
241
241
 
242
242
  var onPageChange = function onPageChange(newPageId) {
243
243
  clearErrors();
@@ -7,6 +7,28 @@ exports.default = void 0;
7
7
 
8
8
  var _models = require("../../../models");
9
9
 
10
+ var getCurrentTaskState = function getCurrentTaskState(_ref, defaultState) {
11
+ var complete = _ref.complete,
12
+ currentPage = _ref.currentPage;
13
+
14
+ if (complete) {
15
+ return _models.TaskStates.TYPES.COMPLETE;
16
+ }
17
+
18
+ if (currentPage) {
19
+ return _models.TaskStates.TYPES.IN_PROGRESS;
20
+ }
21
+
22
+ return defaultState;
23
+ };
24
+
25
+ var allDependentTasksComplete = function allDependentTasksComplete(tasks, mustBeComplete) {
26
+ return mustBeComplete.reduce(function (allComplete, taskName) {
27
+ var _tasks$taskName;
28
+
29
+ return allComplete && ((_tasks$taskName = tasks[taskName]) === null || _tasks$taskName === void 0 ? void 0 : _tasks$taskName.complete);
30
+ }, true);
31
+ };
10
32
  /**
11
33
  * Updates the given task list sections with the latest states
12
34
  * @param {object} sections The JSON defining the sections in the task list
@@ -19,15 +41,13 @@ var _models = require("../../../models");
19
41
  }
20
42
  * @returns An JSON representation of the task list sections with up to date states
21
43
  */
22
- var getUpdatedSectionStates = function getUpdatedSectionStates(sections, tasks) {
44
+
45
+
46
+ var getUpdatedSequentialStates = function getUpdatedSequentialStates(sections, tasks) {
23
47
  return sections.map(function (section, sectionIndex, sectionArray) {
24
48
  return section.tasks.map(function (task, taskIndex, taskArray) {
25
49
  if (tasks[task.name]) {
26
- if (tasks[task.name].complete) {
27
- task.state = _models.TaskStates.TYPES.COMPLETE;
28
- } else if (tasks[task.name].currentPage) {
29
- task.state = _models.TaskStates.TYPES.IN_PROGRESS;
30
- }
50
+ task.state = getCurrentTaskState(tasks[task.name], task.state);
31
51
  } else {
32
52
  var _taskArray, _sectionArray, _sectionArray$tasks$s;
33
53
 
@@ -46,5 +66,31 @@ var getUpdatedSectionStates = function getUpdatedSectionStates(sections, tasks)
46
66
  });
47
67
  };
48
68
 
69
+ var getUpdatedNonSequentialStates = function getUpdatedNonSequentialStates(sections, tasks) {
70
+ return sections.map(function (section) {
71
+ return section.tasks.map(function (task) {
72
+ if (tasks[task.name]) {
73
+ task.state = getCurrentTaskState(tasks[task.name], task.state);
74
+ } else if (task.depends_on && !allDependentTasksComplete(tasks, task.depends_on)) {
75
+ task.state = _models.TaskStates.TYPES.CANNOT_START_YET;
76
+ } else {
77
+ task.state = _models.TaskStates.TYPES.NOT_STARTED;
78
+ }
79
+
80
+ return task;
81
+ });
82
+ });
83
+ };
84
+
85
+ var getUpdatedSectionStates = function getUpdatedSectionStates(sections, tasks) {
86
+ var nonSequential = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
87
+
88
+ if (nonSequential === true) {
89
+ return getUpdatedNonSequentialStates(sections, tasks);
90
+ }
91
+
92
+ return getUpdatedSequentialStates(sections, tasks);
93
+ };
94
+
49
95
  var _default = getUpdatedSectionStates;
50
96
  exports.default = _default;
@@ -7,116 +7,174 @@ var _models = require("../../../models");
7
7
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
8
 
9
9
  // Local imports
10
- describe('components', function () {
11
- describe('FormRenderer', function () {
12
- describe('helpers', function () {
13
- describe('getUpdatedSectionStates', function () {
14
- it("should set all tasks to '".concat(_models.TaskStates.TYPES.CANNOT_START_YET, "' excluding the first which should be '").concat(_models.TaskStates.TYPES.NOT_STARTED, "'"), function () {
15
- var SECTIONS = [{
16
- name: 'Add event details',
17
- tasks: [{
18
- name: 'Date, location and mode details',
19
- pages: ['eventDate', 'eventMode']
20
- }, {
21
- name: 'Officer and agency details',
22
- pages: ['officeDetails']
23
- }]
24
- }, {
25
- name: 'Add people details',
26
- tasks: [{
27
- name: 'People details',
28
- pages: ['firstName', 'surname']
29
- }, {
30
- name: 'Immigration details',
31
- pages: ['immigrationDate']
32
- }, {
33
- name: 'Journey details',
34
- pages: ['journeyDetails']
35
- }]
36
- }];
37
- var updatedSections = (0, _getUpdatedSectionStates.default)(SECTIONS, {});
38
- expect(updatedSections[0][0].state).toEqual(_models.TaskStates.TYPES.NOT_STARTED);
39
- expect(updatedSections[0][1].state).toEqual(_models.TaskStates.TYPES.CANNOT_START_YET);
40
- expect(updatedSections[1][0].state).toEqual(_models.TaskStates.TYPES.CANNOT_START_YET);
41
- expect(updatedSections[1][1].state).toEqual(_models.TaskStates.TYPES.CANNOT_START_YET);
42
- expect(updatedSections[1][2].state).toEqual(_models.TaskStates.TYPES.CANNOT_START_YET);
43
- });
44
- it("should set tasks after any '".concat(_models.TaskStates.TYPES.COMPLETE, "' ones to '").concat(_models.TaskStates.TYPES.NOT_STARTED, "'"), function () {
45
- var SECTIONS = [{
46
- name: 'Add event details',
47
- tasks: [{
48
- name: 'Date, location and mode details',
49
- pages: ['eventDate', 'eventMode']
50
- }, {
51
- name: 'Officer and agency details',
52
- pages: ['officeDetails']
53
- }]
54
- }];
55
- var TASKS = {
56
- 'Date, location and mode details': {
57
- complete: true
58
- }
59
- };
60
- var updatedSections = (0, _getUpdatedSectionStates.default)(SECTIONS, TASKS);
61
- expect(updatedSections[0][0].state).toEqual(_models.TaskStates.TYPES.COMPLETE);
62
- expect(updatedSections[0][1].state).toEqual(_models.TaskStates.TYPES.NOT_STARTED);
63
- });
64
- it("should set tasks after any '".concat(_models.TaskStates.TYPES.COMPLETE, "' ones to '").concat(_models.TaskStates.TYPES.NOT_STARTED, " across different sections'"), function () {
65
- var SECTIONS = [{
66
- name: 'Add event details',
67
- tasks: [{
68
- name: 'Date, location and mode details',
69
- pages: ['eventDate', 'eventMode']
70
- }, {
71
- name: 'Officer and agency details',
72
- pages: ['officeDetails']
73
- }]
74
- }, {
75
- name: 'Add people details',
76
- tasks: [{
77
- name: 'People details',
78
- pages: ['firstName', 'surname']
79
- }, {
80
- name: 'Immigration details',
81
- pages: ['immigrationDate']
82
- }]
83
- }];
84
- var TASKS = {
85
- 'Date, location and mode details': {
86
- complete: true
87
- },
88
- 'Officer and agency details': {
89
- complete: true
90
- }
91
- };
92
- var updatedSections = (0, _getUpdatedSectionStates.default)(SECTIONS, TASKS);
93
- expect(updatedSections[0][0].state).toEqual(_models.TaskStates.TYPES.COMPLETE);
94
- expect(updatedSections[0][1].state).toEqual(_models.TaskStates.TYPES.COMPLETE);
95
- expect(updatedSections[1][0].state).toEqual(_models.TaskStates.TYPES.NOT_STARTED);
96
- expect(updatedSections[1][1].state).toEqual(_models.TaskStates.TYPES.CANNOT_START_YET);
97
- });
98
- it("should set tasks to '".concat(_models.TaskStates.TYPES.IN_PROGRESS, "' if they have a current page but are not complete"), function () {
99
- var SECTIONS = [{
100
- name: 'Add event details',
101
- tasks: [{
102
- name: 'Date, location and mode details',
103
- pages: ['eventDate', 'eventMode']
104
- }, {
105
- name: 'Officer and agency details',
106
- pages: ['officeDetails']
107
- }]
108
- }];
109
- var TASKS = {
110
- 'Date, location and mode details': {
111
- complete: false,
112
- currentPage: 'eventMode'
113
- }
114
- };
115
- var updatedSections = (0, _getUpdatedSectionStates.default)(SECTIONS, TASKS);
116
- expect(updatedSections[0][0].state).toEqual(_models.TaskStates.TYPES.IN_PROGRESS);
117
- expect(updatedSections[0][1].state).toEqual(_models.TaskStates.TYPES.CANNOT_START_YET);
118
- });
119
- });
10
+ describe('components.FormRenderer.helpers.getUpdatedSectionStates', function () {
11
+ describe('sequential tasks', function () {
12
+ it("should set all tasks to '".concat(_models.TaskStates.TYPES.CANNOT_START_YET, "' excluding the first which should be '").concat(_models.TaskStates.TYPES.NOT_STARTED, "'"), function () {
13
+ var SECTIONS = [{
14
+ name: 'Add event details',
15
+ tasks: [{
16
+ name: 'Date, location and mode details',
17
+ pages: ['eventDate', 'eventMode']
18
+ }, {
19
+ name: 'Officer and agency details',
20
+ pages: ['officeDetails']
21
+ }]
22
+ }, {
23
+ name: 'Add people details',
24
+ tasks: [{
25
+ name: 'People details',
26
+ pages: ['firstName', 'surname']
27
+ }, {
28
+ name: 'Immigration details',
29
+ pages: ['immigrationDate']
30
+ }, {
31
+ name: 'Journey details',
32
+ pages: ['journeyDetails']
33
+ }]
34
+ }];
35
+ var updatedSections = (0, _getUpdatedSectionStates.default)(SECTIONS, {});
36
+ expect(updatedSections[0][0].state).toEqual(_models.TaskStates.TYPES.NOT_STARTED);
37
+ expect(updatedSections[0][1].state).toEqual(_models.TaskStates.TYPES.CANNOT_START_YET);
38
+ expect(updatedSections[1][0].state).toEqual(_models.TaskStates.TYPES.CANNOT_START_YET);
39
+ expect(updatedSections[1][1].state).toEqual(_models.TaskStates.TYPES.CANNOT_START_YET);
40
+ expect(updatedSections[1][2].state).toEqual(_models.TaskStates.TYPES.CANNOT_START_YET);
41
+ });
42
+ it("should set tasks after any '".concat(_models.TaskStates.TYPES.COMPLETE, "' ones to '").concat(_models.TaskStates.TYPES.NOT_STARTED, "'"), function () {
43
+ var SECTIONS = [{
44
+ name: 'Add event details',
45
+ tasks: [{
46
+ name: 'Date, location and mode details',
47
+ pages: ['eventDate', 'eventMode']
48
+ }, {
49
+ name: 'Officer and agency details',
50
+ pages: ['officeDetails']
51
+ }]
52
+ }];
53
+ var TASKS = {
54
+ 'Date, location and mode details': {
55
+ complete: true
56
+ }
57
+ };
58
+ var updatedSections = (0, _getUpdatedSectionStates.default)(SECTIONS, TASKS);
59
+ expect(updatedSections[0][0].state).toEqual(_models.TaskStates.TYPES.COMPLETE);
60
+ expect(updatedSections[0][1].state).toEqual(_models.TaskStates.TYPES.NOT_STARTED);
61
+ });
62
+ it("should set tasks after any '".concat(_models.TaskStates.TYPES.COMPLETE, "' ones to '").concat(_models.TaskStates.TYPES.NOT_STARTED, " across different sections'"), function () {
63
+ var SECTIONS = [{
64
+ name: 'Add event details',
65
+ tasks: [{
66
+ name: 'Date, location and mode details',
67
+ pages: ['eventDate', 'eventMode']
68
+ }, {
69
+ name: 'Officer and agency details',
70
+ pages: ['officeDetails']
71
+ }]
72
+ }, {
73
+ name: 'Add people details',
74
+ tasks: [{
75
+ name: 'People details',
76
+ pages: ['firstName', 'surname']
77
+ }, {
78
+ name: 'Immigration details',
79
+ pages: ['immigrationDate']
80
+ }]
81
+ }];
82
+ var TASKS = {
83
+ 'Date, location and mode details': {
84
+ complete: true
85
+ },
86
+ 'Officer and agency details': {
87
+ complete: true
88
+ }
89
+ };
90
+ var updatedSections = (0, _getUpdatedSectionStates.default)(SECTIONS, TASKS);
91
+ expect(updatedSections[0][0].state).toEqual(_models.TaskStates.TYPES.COMPLETE);
92
+ expect(updatedSections[0][1].state).toEqual(_models.TaskStates.TYPES.COMPLETE);
93
+ expect(updatedSections[1][0].state).toEqual(_models.TaskStates.TYPES.NOT_STARTED);
94
+ expect(updatedSections[1][1].state).toEqual(_models.TaskStates.TYPES.CANNOT_START_YET);
95
+ });
96
+ it("should set tasks to '".concat(_models.TaskStates.TYPES.IN_PROGRESS, "' if they have a current page but are not complete"), function () {
97
+ var SECTIONS = [{
98
+ name: 'Add event details',
99
+ tasks: [{
100
+ name: 'Date, location and mode details',
101
+ pages: ['eventDate', 'eventMode']
102
+ }, {
103
+ name: 'Officer and agency details',
104
+ pages: ['officeDetails']
105
+ }]
106
+ }];
107
+ var TASKS = {
108
+ 'Date, location and mode details': {
109
+ complete: false,
110
+ currentPage: 'eventMode'
111
+ }
112
+ };
113
+ var updatedSections = (0, _getUpdatedSectionStates.default)(SECTIONS, TASKS);
114
+ expect(updatedSections[0][0].state).toEqual(_models.TaskStates.TYPES.IN_PROGRESS);
115
+ expect(updatedSections[0][1].state).toEqual(_models.TaskStates.TYPES.CANNOT_START_YET);
116
+ });
117
+ });
118
+ describe('non-sequential tasks', function () {
119
+ var NON_SEQUENTIAL = true;
120
+ var SECTIONS = [{
121
+ name: 'First',
122
+ tasks: [{
123
+ name: 'Alpha'
124
+ }, {
125
+ name: 'Bravo'
126
+ }]
127
+ }, {
128
+ name: 'Second',
129
+ tasks: [{
130
+ name: 'Charlie',
131
+ depends_on: ['Alpha', 'Bravo']
132
+ }]
133
+ }, {
134
+ name: 'Third',
135
+ tasks: [{
136
+ name: 'Delta'
137
+ }]
138
+ }];
139
+ it("should set all states to '".concat(_models.TaskStates.TYPES.NOT_STARTED, "', except any that depend on others"), function () {
140
+ var RESULT = (0, _getUpdatedSectionStates.default)(SECTIONS, {}, NON_SEQUENTIAL);
141
+ expect(RESULT[0][0].state).toEqual(_models.TaskStates.TYPES.NOT_STARTED); // Alpha
142
+
143
+ expect(RESULT[0][1].state).toEqual(_models.TaskStates.TYPES.NOT_STARTED); // Bravo
144
+
145
+ expect(RESULT[1][0].state).toEqual(_models.TaskStates.TYPES.CANNOT_START_YET); // Charlie
146
+
147
+ expect(RESULT[2][0].state).toEqual(_models.TaskStates.TYPES.NOT_STARTED); // Delta
148
+ });
149
+ it('should keep dependent state unable to start if any dependencies are not complete', function () {
150
+ var ALPHA_COMPLETE = (0, _getUpdatedSectionStates.default)(SECTIONS, {
151
+ Alpha: {
152
+ complete: true
153
+ }
154
+ }, NON_SEQUENTIAL);
155
+ expect(ALPHA_COMPLETE[1][0].state).toEqual(_models.TaskStates.TYPES.CANNOT_START_YET); // Charlie
156
+
157
+ var BRAVO_COMPLETE = (0, _getUpdatedSectionStates.default)(SECTIONS, {
158
+ Bravo: {
159
+ complete: true
160
+ }
161
+ }, NON_SEQUENTIAL);
162
+ expect(BRAVO_COMPLETE[1][0].state).toEqual(_models.TaskStates.TYPES.CANNOT_START_YET); // Charlie
163
+ });
164
+ it('should allow dependent state to start if all dependencies are complete', function () {
165
+ var TASKS = {
166
+ 'Alpha': {
167
+ complete: true
168
+ },
169
+ 'Bravo': {
170
+ complete: true
171
+ },
172
+ 'Delta': {}
173
+ };
174
+ var RESULT = (0, _getUpdatedSectionStates.default)(SECTIONS, TASKS, NON_SEQUENTIAL);
175
+ expect(RESULT[1][0].state).toEqual(_models.TaskStates.TYPES.NOT_STARTED); // Charlie
176
+
177
+ expect(RESULT[2][0].state).toEqual(_models.TaskStates.TYPES.NOT_STARTED); // Delta
120
178
  });
121
179
  });
122
180
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "3.17.2",
3
+ "version": "3.18.0",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",