Skip to content

Commit 0045e58

Browse files
committed
Rewrite submission filtering to support datagrid
1 parent ab214e9 commit 0045e58

File tree

5 files changed

+219
-22
lines changed

5 files changed

+219
-22
lines changed

mocks/mocks/data/formio-api/test-mellomlagring.json

+89
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,95 @@
11011101
"id": "e3vwglb",
11021102
"defaultValue": null,
11031103
"navId": "edqy1v"
1104+
},
1105+
{
1106+
"label": "Ønskeliste",
1107+
"key": "datagrid",
1108+
"input": false,
1109+
"tableView": false,
1110+
"validateOn": "blur",
1111+
"validate": {
1112+
"required": false,
1113+
"custom": "",
1114+
"customPrivate": false,
1115+
"strictDateValidation": false,
1116+
"multiple": false,
1117+
"unique": false
1118+
},
1119+
"type": "datagrid",
1120+
"isNavDataGrid": true,
1121+
"components": [
1122+
{
1123+
"validate": {
1124+
"required": false,
1125+
"custom": "",
1126+
"customPrivate": false,
1127+
"strictDateValidation": false,
1128+
"multiple": false,
1129+
"unique": false
1130+
},
1131+
"label": "Gaveønske",
1132+
"key": "tekstfelt",
1133+
"input": true,
1134+
"tableView": false,
1135+
"type": "textfield",
1136+
"navId": "e0tpgrm"
1137+
}
1138+
],
1139+
"navId": "e5z29dy",
1140+
"placeholder": "",
1141+
"prefix": "",
1142+
"customClass": "",
1143+
"suffix": "",
1144+
"multiple": false,
1145+
"defaultValue": null,
1146+
"protected": false,
1147+
"unique": false,
1148+
"persistent": true,
1149+
"hidden": false,
1150+
"clearOnHide": true,
1151+
"refreshOn": "",
1152+
"redrawOn": "",
1153+
"modalEdit": false,
1154+
"dataGridLabel": false,
1155+
"labelPosition": "top",
1156+
"description": "",
1157+
"errorLabel": "",
1158+
"tooltip": "",
1159+
"hideLabel": false,
1160+
"tabindex": "",
1161+
"disabled": false,
1162+
"autofocus": false,
1163+
"dbIndex": false,
1164+
"customDefaultValue": "",
1165+
"calculateValue": "",
1166+
"calculateServer": false,
1167+
"widget": null,
1168+
"attributes": {},
1169+
"conditional": {
1170+
"show": null,
1171+
"when": null,
1172+
"eq": ""
1173+
},
1174+
"overlay": {
1175+
"style": "",
1176+
"left": "",
1177+
"top": "",
1178+
"width": "",
1179+
"height": ""
1180+
},
1181+
"allowCalculateOverride": false,
1182+
"encrypted": false,
1183+
"showCharCount": false,
1184+
"showWordCount": false,
1185+
"properties": {},
1186+
"allowMultipleMasks": false,
1187+
"addons": [],
1188+
"tree": true,
1189+
"lazyLoad": false,
1190+
"disableAddingRemovingRows": false,
1191+
"id": "euimv9",
1192+
"keyModified": true
11041193
}
11051194
],
11061195
"placeholder": "",

mocks/mocks/data/innsending-api/mellomlagring/getTestMellomlagring-valid-extra-values.json

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
"hvaSpisteDuTilFrokost": "",
3131
"hvaSyntesDuOmFrokosten": "Helt ok",
3232
"slettetTekstfelt": "Verdi som skal fjernes",
33+
"datagrid": [{"tekstfelt": "Hoppeslott", "tekstfelt1": "Grønn"}, {"tekstfelt": "Hund", "tekstfelt1": "Svart"}],
34+
"datagrid1": [{"tekstfelt": "Katt", "tekstfelt1": "Blå"}, {"tekstfelt": "Kanarifugl", "tekstfelt1": "Gul"}],
3335
"container.slettetTekstFelt": "Verdi fra container som skal fjernes",
3436
"onskerDuAFaGavenInnpakket": "nei",
3537
"hemmeligKode": "",

packages/fyllut/cypress/e2e/mellomlagring.cy.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ describe('Mellomlagring', () => {
197197
cy.intercept('PUT', '/fyllut/api/send-inn/utfyltsoknad', (req) => {
198198
const { submission: bodySubmission, ...bodyRest } = req.body;
199199
const { submission: fixtureSubmission, ...fixtureRest } = fixture;
200-
expect(bodySubmission.data).to.deep.contain(fixtureSubmission.data);
200+
expect(bodySubmission.data).to.deep.eq(fixtureSubmission.data);
201201
expect(bodyRest).to.deep.eq(fixtureRest);
202202
}).as('submitMellomlagring');
203203
});
@@ -269,7 +269,11 @@ describe('Mellomlagring', () => {
269269
cy.intercept('PUT', '/fyllut/api/send-inn/utfyltsoknad', (req) => {
270270
const { submission } = req.body;
271271
expect(submission.data['slettetTekstfelt']).to.be.undefined;
272+
// Container
272273
expect(submission.data['container.slettetTekstFelt']).to.be.undefined;
274+
// Datagrid
275+
expect(submission.data['datagrid']).to.deep.eq([{ tekstfelt: 'Hoppeslott' }, { tekstfelt: 'Hund' }]);
276+
expect(submission.data['datagrid1']).to.be.undefined;
273277
// value should be removed if the corresponding field is conditionally hidden
274278
expect(submission.data['hvaSyntesDuOmFrokosten']).to.be.undefined;
275279
}).as('submitMellomlagring');

packages/fyllut/cypress/fixtures/mellomlagring/submitTestMellomlagring.json

+89
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,95 @@
11031103
"id": "e3vwglb",
11041104
"defaultValue": null,
11051105
"navId": "edqy1v"
1106+
},
1107+
{
1108+
"label": "Ønskeliste",
1109+
"key": "datagrid",
1110+
"input": false,
1111+
"tableView": false,
1112+
"validateOn": "blur",
1113+
"validate": {
1114+
"required": false,
1115+
"custom": "",
1116+
"customPrivate": false,
1117+
"strictDateValidation": false,
1118+
"multiple": false,
1119+
"unique": false
1120+
},
1121+
"type": "datagrid",
1122+
"isNavDataGrid": true,
1123+
"components": [
1124+
{
1125+
"validate": {
1126+
"required": false,
1127+
"custom": "",
1128+
"customPrivate": false,
1129+
"strictDateValidation": false,
1130+
"multiple": false,
1131+
"unique": false
1132+
},
1133+
"label": "Gaveønske",
1134+
"key": "tekstfelt",
1135+
"input": true,
1136+
"tableView": false,
1137+
"type": "textfield",
1138+
"navId": "e0tpgrm"
1139+
}
1140+
],
1141+
"navId": "e5z29dy",
1142+
"placeholder": "",
1143+
"prefix": "",
1144+
"customClass": "",
1145+
"suffix": "",
1146+
"multiple": false,
1147+
"defaultValue": null,
1148+
"protected": false,
1149+
"unique": false,
1150+
"persistent": true,
1151+
"hidden": false,
1152+
"clearOnHide": true,
1153+
"refreshOn": "",
1154+
"redrawOn": "",
1155+
"modalEdit": false,
1156+
"dataGridLabel": false,
1157+
"labelPosition": "top",
1158+
"description": "",
1159+
"errorLabel": "",
1160+
"tooltip": "",
1161+
"hideLabel": false,
1162+
"tabindex": "",
1163+
"disabled": false,
1164+
"autofocus": false,
1165+
"dbIndex": false,
1166+
"customDefaultValue": "",
1167+
"calculateValue": "",
1168+
"calculateServer": false,
1169+
"widget": null,
1170+
"attributes": {},
1171+
"conditional": {
1172+
"show": null,
1173+
"when": null,
1174+
"eq": ""
1175+
},
1176+
"overlay": {
1177+
"style": "",
1178+
"left": "",
1179+
"top": "",
1180+
"width": "",
1181+
"height": ""
1182+
},
1183+
"allowCalculateOverride": false,
1184+
"encrypted": false,
1185+
"showCharCount": false,
1186+
"showWordCount": false,
1187+
"properties": {},
1188+
"allowMultipleMasks": false,
1189+
"addons": [],
1190+
"tree": true,
1191+
"lazyLoad": false,
1192+
"disableAddingRemovingRows": false,
1193+
"id": "euimv9",
1194+
"keyModified": true
11061195
}
11071196
],
11081197
"placeholder": "",

packages/shared-components/src/context/sendInn/utils/utils.ts

+34-21
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,44 @@ import {
55
SubmissionData,
66
dateUtils,
77
formSummaryUtil,
8-
navFormUtils,
98
} from '@navikt/skjemadigitalisering-shared-domain';
109
import { SendInnSoknadResponse } from '../../../api/sendinn/sendInnSoknad';
1110

12-
const findComponentKeys = (formSummaryKeys: string[][], key): string[][] => {
13-
return formSummaryKeys.filter((componentKeys) => componentKeys[0] === key);
11+
const findComponent = (formSummaryComponents, key: string) => {
12+
let result: any[] = [];
13+
formSummaryComponents.forEach((summaryComponent) => {
14+
if (summaryComponent.type === 'navSkjemagruppe') {
15+
result = [...result, ...findComponent(summaryComponent.components, key)];
16+
} else if (summaryComponent.key.split('.')[0] === key) {
17+
result = [...result, summaryComponent];
18+
}
19+
});
20+
return result;
1421
};
15-
16-
const filterOutIfNotInSummary = (originalData: SubmissionData, formSummaryKeys: string[][]) => {
22+
const filterOutIfNotInSummary = (originalData: SubmissionData, formSummaryComponents) => {
1723
const filteredSubmissionEntries = Object.entries(originalData)
1824
.map(([key, value]) => {
19-
const componentKeys = findComponentKeys(formSummaryKeys, key);
20-
if (componentKeys.length === 0) return undefined;
21-
if (typeof value === 'object' && componentKeys[0].length > 1) {
22-
const nestedData = filterOutIfNotInSummary(
23-
value as SubmissionData,
24-
componentKeys.map(([_first, ...rest]) => rest),
25-
);
26-
if (nestedData) {
27-
return [key, nestedData];
28-
}
29-
return undefined;
25+
const matchingComponents = findComponent(formSummaryComponents, key);
26+
// Remove value from submission
27+
if (matchingComponents.length === 0) return undefined;
28+
// Container
29+
if (matchingComponents[0].key.split('.').length > 1) {
30+
const containerComponents = matchingComponents.map((component) => {
31+
const [_containerKey, ...newKey] = component.key.split('.');
32+
return { ...component, key: newKey.join('.') };
33+
});
34+
const nestedData = filterOutIfNotInSummary(value as SubmissionData, containerComponents);
35+
return nestedData ? [key, nestedData] : undefined;
36+
}
37+
const [matchingComponent] = matchingComponents;
38+
// Datagrid
39+
if (matchingComponent.type === 'datagrid') {
40+
const nestedData = matchingComponent.components.map((row, index) => {
41+
return filterOutIfNotInSummary(value[index], row.components);
42+
});
43+
return [key, nestedData];
3044
}
45+
3146
return [key, value];
3247
})
3348
.filter((entry) => !!entry);
@@ -45,12 +60,10 @@ const getSubmissionFromResponse = (response?: SendInnSoknadResponse, form?: NavF
4560
return submissionFromResponse;
4661
}
4762

48-
const formSummaryKeys: string[][] = formSummaryUtil
63+
const formSummaryComponents = formSummaryUtil
4964
.createFormSummaryPanels(form, submissionFromResponse)
50-
.flatMap((panel) => navFormUtils.flattenComponents(panel.components))
51-
.map((component) => component.key.split('.'));
52-
const submissionData = filterOutIfNotInSummary({ ...submissionFromResponse.data }, formSummaryKeys);
53-
65+
.flatMap((panel) => panel.components);
66+
const submissionData = filterOutIfNotInSummary({ ...submissionFromResponse.data }, formSummaryComponents);
5467
return { ...submissionFromResponse, data: submissionData };
5568
};
5669

0 commit comments

Comments
 (0)