@@ -12,7 +12,8 @@ import {
1212import React from 'react' ;
1313import FieldSelector from './schema-field-selector' ;
1414import FakerMappingSelector from './faker-mapping-selector' ;
15- import type { FakerSchemaMapping , MockDataGeneratorState } from './types' ;
15+ import type { FakerSchema , MockDataGeneratorState } from './types' ;
16+ import type { MongoDBFieldType } from '@mongodb-js/compass-generative-ai' ;
1617
1718const containerStyles = css ( {
1819 display : 'flex' ,
@@ -49,55 +50,49 @@ const schemaEditorLoaderStyles = css({
4950} ) ;
5051
5152const FakerSchemaEditorContent = ( {
52- fakerSchemaMappings ,
53+ fakerSchema ,
5354 onSchemaConfirmed,
5455} : {
55- fakerSchemaMappings : FakerSchemaMapping [ ] ;
56+ fakerSchema : FakerSchema ;
5657 onSchemaConfirmed : ( isConfirmed : boolean ) => void ;
5758} ) => {
5859 const [ fakerSchemaFormValues , setFakerSchemaFormValues ] =
59- React . useState < Array < FakerSchemaMapping > > ( fakerSchemaMappings ) ;
60- const [ activeField , setActiveField ] = React . useState < string > (
61- fakerSchemaFormValues [ 0 ] . fieldPath
62- ) ;
60+ React . useState < FakerSchema > ( fakerSchema ) ;
61+
62+ const fieldPaths = Object . keys ( fakerSchemaFormValues ) ;
63+ const [ activeField , setActiveField ] = React . useState < string > ( fieldPaths [ 0 ] ) ;
6364
64- const activeJsonType = fakerSchemaFormValues . find (
65- ( mapping ) => mapping . fieldPath === activeField
66- ) ?. mongoType ;
67- const activeFakerFunction = fakerSchemaFormValues . find (
68- ( mapping ) => mapping . fieldPath === activeField
69- ) ?. fakerMethod ;
65+ const activeJsonType = fakerSchemaFormValues [ activeField ] ?. mongoType ;
66+ const activeFakerFunction = fakerSchemaFormValues [ activeField ] ?. fakerMethod ;
7067
7168 const resetIsSchemaConfirmed = ( ) => {
7269 onSchemaConfirmed ( false ) ;
7370 } ;
7471
75- 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 onJsonTypeSelect = ( newJsonType : MongoDBFieldType ) => {
73+ const currentMapping = fakerSchemaFormValues [ activeField ] ;
74+ if ( currentMapping ) {
75+ setFakerSchemaFormValues ( {
76+ ...fakerSchemaFormValues ,
77+ [ activeField ] : {
78+ ...currentMapping ,
79+ mongoType : newJsonType ,
80+ } ,
81+ } ) ;
8682 resetIsSchemaConfirmed ( ) ;
8783 }
8884 } ;
8985
9086 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- ) ;
87+ const currentMapping = fakerSchemaFormValues [ activeField ] ;
88+ if ( currentMapping ) {
89+ setFakerSchemaFormValues ( {
90+ ...fakerSchemaFormValues ,
91+ [ activeField ] : {
92+ ...currentMapping ,
93+ fakerMethod : newFakerFunction ,
94+ } ,
95+ } ) ;
10196 resetIsSchemaConfirmed ( ) ;
10297 }
10398 } ;
@@ -107,7 +102,7 @@ const FakerSchemaEditorContent = ({
107102 < div className = { innerEditorStyles } >
108103 < FieldSelector
109104 activeField = { activeField }
110- fields = { fakerSchemaFormValues . map ( ( mapping ) => mapping . fieldPath ) }
105+ fields = { fieldPaths }
111106 onFieldSelect = { setActiveField }
112107 />
113108 { activeJsonType && activeFakerFunction && (
@@ -163,7 +158,7 @@ const FakerSchemaEditorScreen = ({
163158 ) }
164159 { fakerSchemaGenerationState . status === 'completed' && (
165160 < FakerSchemaEditorContent
166- fakerSchemaMappings = { fakerSchemaGenerationState . fakerSchema }
161+ fakerSchema = { fakerSchemaGenerationState . fakerSchema }
167162 onSchemaConfirmed = { onSchemaConfirmed }
168163 />
169164 ) }
0 commit comments