@ukhomeoffice/cop-react-form-renderer 6.16.1-alpha → 6.16.2-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.
@@ -61,6 +61,22 @@ const onPageAction = (action, patch, patchLabel, hooks, data, formState, validat
61
61
  setSubmitting(false);
62
62
  } else {
63
63
  let pageUpdate = next => onPageChange(_helpers.default.getNextPageId(type, pages, pageId, action, next));
64
+ let collectionName;
65
+ let addAnotherName;
66
+ if (action.collection) {
67
+ collectionName = action.collection;
68
+ const collectionNameCapitalised = collectionName.charAt(0).toUpperCase() + collectionName.slice(1);
69
+ addAnotherName = "addAnother".concat(collectionNameCapitalised).slice(0, -1);
70
+ }
71
+ let collectionArray;
72
+ let index;
73
+ if (collectionName) {
74
+ collectionArray = form.page.formData[collectionName];
75
+ if (collectionArray && collectionArray.length > 0 && form.page.formData["".concat(collectionName, "ActiveId")]) {
76
+ index = collectionArray.findIndex(obj => obj.id === form.page.formData["".concat(collectionName, "ActiveId")]);
77
+ }
78
+ }
79
+
64
80
  /* eslint-disable no-case-declarations */
65
81
  switch (action.type) {
66
82
  case _models.PageAction.TYPES.SUBMIT:
@@ -68,10 +84,37 @@ const onPageAction = (action, patch, patchLabel, hooks, data, formState, validat
68
84
  break;
69
85
  case _models.PageAction.TYPES.SAVE_AND_NAVIGATE:
70
86
  const state = _objectSpread({}, currentTask);
87
+ // If resetActiveId is true then point active ID to first entry in collection
88
+ if (collectionArray && collectionArray.length > 0 && action.resetActiveId) {
89
+ form.page.formData["".concat(collectionName, "ActiveId")] = collectionArray[0].id;
90
+ }
91
+ // If resetActiveId is false
92
+ if (collectionArray && collectionArray.length > 0 && !action.resetActiveId) {
93
+ // Truncate collection if user has selected no to more entries
94
+ if (index !== undefined && action.truncateCollection && form.page.formData[addAnotherName] === 'No') {
95
+ form.page.formData[collectionName] = collectionArray.filter((_data, idx) => idx <= index);
96
+ }
97
+ // Set addAnother field to yes if not reached last entry in collection
98
+ if (index !== undefined && index + 1 < collectionArray.length) {
99
+ form.page.formData[addAnotherName] = 'Yes';
100
+ }
101
+ // Set addAnother field to no if reached last entry
102
+ if (index !== undefined && index + 1 === collectionArray.length && form.page.formData[addAnotherName]) {
103
+ form.page.formData[addAnotherName] = 'No';
104
+ }
105
+ }
71
106
  pageUpdate = () => _handlers.default.navigate(action, pageId, onPageChange, state);
72
107
  break;
73
108
  case _models.PageAction.TYPES.COLLECTION_ADD:
74
- _utils.default.CollectionPage.addEntry(action.collection, form.page.formData);
109
+ // Only add item if the index is undefined,
110
+ // the array is empty
111
+ // or the active ID is the last one in the collection,
112
+ // otherwise move to next item.
113
+ if (index === undefined || collectionArray.length === 0 || index + 1 === form.page.formData[collectionName].length) {
114
+ _utils.default.CollectionPage.addEntry(action.collection, form.page.formData);
115
+ } else {
116
+ form.page.formData["".concat(action.collection, "ActiveId")] = collectionArray[index + 1].id;
117
+ }
75
118
 
76
119
  // We need to delete the collection entry fields from formData as it will be holding previous values.
77
120
  if (form.page.formData["".concat(action.collection)] && form.page.formData["".concat(action.collection)].length > 0) {
@@ -105,16 +148,13 @@ const onPageAction = (action, patch, patchLabel, hooks, data, formState, validat
105
148
  removedEntryFieldsToDelete.forEach(i => delete form.page.formData[i]);
106
149
  }
107
150
 
108
- // 3. Retrieve the collection array from formData.
109
- const collectionArray = form.page.formData[action.collection];
110
-
111
- // 4. Assign the fields in the last collection entry to formData.
151
+ // 3. Assign the fields in the last collection entry to formData.
112
152
  Object.assign(form.page.formData, collectionArray[collectionArray.length - 1]);
113
153
 
114
- // 5. Assign the formData ID back to the formData.
154
+ // 4. Assign the formData ID back to the formData.
115
155
  form.page.formData.id = formDataId;
116
156
 
117
- // 6. If the collection array has entries set the correct ActiveId
157
+ // 5. If the collection array has entries set the correct ActiveId
118
158
  // otherwise delete it as well as the collection array.
119
159
  if (collectionArray.length > 0) {
120
160
  form.page.formData["".concat(action.collection, "ActiveId")] = collectionArray[collectionArray.length - 1].id;
@@ -28,6 +28,8 @@ const removeCollectionPageEntry = (collectionName, formData, id) => {
28
28
  }
29
29
  const deletedEntry = collectionData[indexToDelete];
30
30
  collectionData.splice(indexToDelete, 1);
31
+ const data = formData;
32
+ data["".concat(collectionName, "ActiveId")] = collectionData.length > 0 ? collectionData[0].id : null;
31
33
  return deletedEntry;
32
34
  };
33
35
  var _default = exports.default = removeCollectionPageEntry;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "6.16.1-alpha",
3
+ "version": "6.16.2-alpha",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",