@@ -5,29 +5,44 @@ import {
5
5
SubmissionData ,
6
6
dateUtils ,
7
7
formSummaryUtil ,
8
- navFormUtils ,
9
8
} from '@navikt/skjemadigitalisering-shared-domain' ;
10
9
import { SendInnSoknadResponse } from '../../../api/sendinn/sendInnSoknad' ;
11
10
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 ;
14
21
} ;
15
-
16
- const filterOutIfNotInSummary = ( originalData : SubmissionData , formSummaryKeys : string [ ] [ ] ) => {
22
+ const filterOutIfNotInSummary = ( originalData : SubmissionData , formSummaryComponents ) => {
17
23
const filteredSubmissionEntries = Object . entries ( originalData )
18
24
. 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 ] ;
30
44
}
45
+
31
46
return [ key , value ] ;
32
47
} )
33
48
. filter ( ( entry ) => ! ! entry ) ;
@@ -45,12 +60,10 @@ const getSubmissionFromResponse = (response?: SendInnSoknadResponse, form?: NavF
45
60
return submissionFromResponse ;
46
61
}
47
62
48
- const formSummaryKeys : string [ ] [ ] = formSummaryUtil
63
+ const formSummaryComponents = formSummaryUtil
49
64
. 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 ) ;
54
67
return { ...submissionFromResponse , data : submissionData } ;
55
68
} ;
56
69
0 commit comments