@@ -12,7 +12,7 @@ import {
12
12
import React from 'react' ;
13
13
import FieldSelector from './schema-field-selector' ;
14
14
import FakerMappingSelector from './faker-mapping-selector' ;
15
- import type { FakerSchemaMapping , MockDataGeneratorState } from './types' ;
15
+ import type { FakerSchema , MockDataGeneratorState } from './types' ;
16
16
17
17
const containerStyles = css ( {
18
18
display : 'flex' ,
@@ -49,55 +49,49 @@ const schemaEditorLoaderStyles = css({
49
49
} ) ;
50
50
51
51
const FakerSchemaEditorContent = ( {
52
- fakerSchemaMappings ,
52
+ fakerSchema ,
53
53
onSchemaConfirmed,
54
54
} : {
55
- fakerSchemaMappings : FakerSchemaMapping [ ] ;
55
+ fakerSchema : FakerSchema ;
56
56
onSchemaConfirmed : ( isConfirmed : boolean ) => void ;
57
57
} ) => {
58
58
const [ fakerSchemaFormValues , setFakerSchemaFormValues ] =
59
- React . useState < Array < FakerSchemaMapping > > ( fakerSchemaMappings ) ;
60
- const [ activeField , setActiveField ] = React . useState < string > (
61
- fakerSchemaFormValues [ 0 ] . fieldPath
62
- ) ;
59
+ React . useState < FakerSchema > ( fakerSchema ) ;
60
+
61
+ const fieldPaths = Object . keys ( fakerSchemaFormValues ) ;
62
+ const [ activeField , setActiveField ] = React . useState < string > ( fieldPaths [ 0 ] ) ;
63
63
64
- const activeJsonType = fakerSchemaFormValues . find (
65
- ( mapping ) => mapping . fieldPath === activeField
66
- ) ?. mongoType ;
67
- const activeFakerFunction = fakerSchemaFormValues . find (
68
- ( mapping ) => mapping . fieldPath === activeField
69
- ) ?. fakerMethod ;
64
+ const activeJsonType = fakerSchemaFormValues [ activeField ] ?. mongoType ;
65
+ const activeFakerFunction = fakerSchemaFormValues [ activeField ] ?. fakerMethod ;
70
66
71
67
const resetIsSchemaConfirmed = ( ) => {
72
68
onSchemaConfirmed ( false ) ;
73
69
} ;
74
70
75
71
const onJsonTypeSelect = ( newJsonType : string ) => {
76
- const updatedFakerFieldMapping = fakerSchemaFormValues . find (
77
- ( mapping ) => mapping . fieldPath === activeField
78
- ) ;
79
- if ( updatedFakerFieldMapping ) {
80
- updatedFakerFieldMapping . mongoType = newJsonType ;
81
- setFakerSchemaFormValues (
82
- fakerSchemaFormValues . map ( ( mapping ) =>
83
- mapping . fieldPath === activeField ? updatedFakerFieldMapping : mapping
84
- )
85
- ) ;
72
+ const currentMapping = fakerSchemaFormValues [ activeField ] ;
73
+ if ( currentMapping ) {
74
+ setFakerSchemaFormValues ( {
75
+ ...fakerSchemaFormValues ,
76
+ [ activeField ] : {
77
+ ...currentMapping ,
78
+ mongoType : newJsonType ,
79
+ } ,
80
+ } ) ;
86
81
resetIsSchemaConfirmed ( ) ;
87
82
}
88
83
} ;
89
84
90
85
const onFakerFunctionSelect = ( newFakerFunction : string ) => {
91
- const updatedFakerFieldMapping = fakerSchemaFormValues . find (
92
- ( mapping ) => mapping . fieldPath === activeField
93
- ) ;
94
- if ( updatedFakerFieldMapping ) {
95
- updatedFakerFieldMapping . fakerMethod = newFakerFunction ;
96
- setFakerSchemaFormValues (
97
- fakerSchemaFormValues . map ( ( mapping ) =>
98
- mapping . fieldPath === activeField ? updatedFakerFieldMapping : mapping
99
- )
100
- ) ;
86
+ const currentMapping = fakerSchemaFormValues [ activeField ] ;
87
+ if ( currentMapping ) {
88
+ setFakerSchemaFormValues ( {
89
+ ...fakerSchemaFormValues ,
90
+ [ activeField ] : {
91
+ ...currentMapping ,
92
+ fakerMethod : newFakerFunction ,
93
+ } ,
94
+ } ) ;
101
95
resetIsSchemaConfirmed ( ) ;
102
96
}
103
97
} ;
@@ -107,7 +101,7 @@ const FakerSchemaEditorContent = ({
107
101
< div className = { innerEditorStyles } >
108
102
< FieldSelector
109
103
activeField = { activeField }
110
- fields = { fakerSchemaFormValues . map ( ( mapping ) => mapping . fieldPath ) }
104
+ fields = { fieldPaths }
111
105
onFieldSelect = { setActiveField }
112
106
/>
113
107
{ activeJsonType && activeFakerFunction && (
@@ -163,7 +157,7 @@ const FakerSchemaEditorScreen = ({
163
157
) }
164
158
{ fakerSchemaGenerationState . status === 'completed' && (
165
159
< FakerSchemaEditorContent
166
- fakerSchemaMappings = { fakerSchemaGenerationState . fakerSchema }
160
+ fakerSchema = { fakerSchemaGenerationState . fakerSchema }
167
161
onSchemaConfirmed = { onSchemaConfirmed }
168
162
/>
169
163
) }
0 commit comments