Skip to content

Commit 13788b0

Browse files
committed
WIP
1 parent 18dcb0b commit 13788b0

File tree

2 files changed

+112
-27
lines changed

2 files changed

+112
-27
lines changed

packages/compass-collection/src/components/mock-data-generator-modal/faker-schema-editor-screen.tsx

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@ import FieldSelector from './schema-field-selector';
1515
import FakerMappingSelector from './faker-mapping-selector';
1616
import type { FakerSchema, MockDataGeneratorState } from './types';
1717
import type { MongoDBFieldType } from '@mongodb-js/compass-generative-ai';
18-
import { updateEditedFakerSchema } from '../../modules/collection-tab';
18+
import {
19+
fakerFieldTypeChanged,
20+
fakerFieldMethodChanged,
21+
type FakerFieldTypeChangedAction,
22+
type FakerFieldMethodChangedAction,
23+
} from '../../modules/collection-tab';
24+
25+
type FakerSchemaDispatch = (
26+
action: FakerFieldTypeChangedAction | FakerFieldMethodChangedAction
27+
) => void;
1928

2029
const containerStyles = css({
2130
display: 'flex',
@@ -54,11 +63,11 @@ const schemaEditorLoaderStyles = css({
5463
const FakerSchemaEditorContent = ({
5564
fakerSchema,
5665
onSchemaConfirmed,
57-
onSchemaChange,
66+
dispatch,
5867
}: {
5968
fakerSchema: FakerSchema;
6069
onSchemaConfirmed: (isConfirmed: boolean) => void;
61-
onSchemaChange: (updatedSchema: FakerSchema) => void;
70+
dispatch: FakerSchemaDispatch;
6271
}) => {
6372
const [fakerSchemaFormValues, setFakerSchemaFormValues] =
6473
React.useState<FakerSchema>(fakerSchema);
@@ -77,31 +86,35 @@ const FakerSchemaEditorContent = ({
7786
const onJsonTypeSelect = (newJsonType: MongoDBFieldType) => {
7887
const currentMapping = fakerSchemaFormValues[activeField];
7988
if (currentMapping) {
80-
const updatedSchema = {
89+
// Update local form state
90+
setFakerSchemaFormValues({
8191
...fakerSchemaFormValues,
8292
[activeField]: {
8393
...currentMapping,
8494
mongoType: newJsonType,
8595
},
86-
};
87-
setFakerSchemaFormValues(updatedSchema);
88-
onSchemaChange(updatedSchema);
96+
});
97+
98+
// Dispatch event to Redux
99+
dispatch(fakerFieldTypeChanged(activeField, newJsonType));
89100
resetIsSchemaConfirmed();
90101
}
91102
};
92103

93104
const onFakerFunctionSelect = (newFakerFunction: string) => {
94105
const currentMapping = fakerSchemaFormValues[activeField];
95106
if (currentMapping) {
96-
const updatedSchema = {
107+
// Update local form state
108+
setFakerSchemaFormValues({
97109
...fakerSchemaFormValues,
98110
[activeField]: {
99111
...currentMapping,
100112
fakerMethod: newFakerFunction,
101113
},
102-
};
103-
setFakerSchemaFormValues(updatedSchema);
104-
onSchemaChange(updatedSchema);
114+
});
115+
116+
// Dispatch event to Redux
117+
dispatch(fakerFieldMethodChanged(activeField, newFakerFunction));
105118
resetIsSchemaConfirmed();
106119
}
107120
};
@@ -144,7 +157,7 @@ const FakerSchemaEditorScreen = ({
144157
isSchemaConfirmed: boolean;
145158
onSchemaConfirmed: (isConfirmed: boolean) => void;
146159
fakerSchemaGenerationState: MockDataGeneratorState;
147-
dispatch: (action: any) => void;
160+
dispatch: FakerSchemaDispatch;
148161
}) => {
149162
return (
150163
<div data-testid="faker-schema-editor" className={containerStyles}>
@@ -172,9 +185,7 @@ const FakerSchemaEditorScreen = ({
172185
<FakerSchemaEditorContent
173186
fakerSchema={fakerSchemaGenerationState.editedFakerSchema}
174187
onSchemaConfirmed={onSchemaConfirmed}
175-
onSchemaChange={(updatedSchema) =>
176-
dispatch(updateEditedFakerSchema(updatedSchema))
177-
}
188+
dispatch={dispatch}
178189
/>
179190
)}
180191
</div>

packages/compass-collection/src/modules/collection-tab.ts

Lines changed: 86 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ import type { AtlasAiService } from '@mongodb-js/compass-generative-ai/provider'
1616
import type { experimentationServiceLocator } from '@mongodb-js/compass-telemetry/provider';
1717
import { type Logger, mongoLogId } from '@mongodb-js/compass-logging/provider';
1818
import { type PreferencesAccess } from 'compass-preferences-model/provider';
19-
import type { MockDataSchemaRequest } from '@mongodb-js/compass-generative-ai';
19+
import type {
20+
MockDataSchemaRequest,
21+
MongoDBFieldType,
22+
} from '@mongodb-js/compass-generative-ai';
2023
import { isInternalFieldPath } from 'hadron-document';
2124
import toNS from 'mongodb-ns';
2225
import {
@@ -127,7 +130,8 @@ export enum CollectionActions {
127130
FakerMappingGenerationStarted = 'compass-collection/FakerMappingGenerationStarted',
128131
FakerMappingGenerationCompleted = 'compass-collection/FakerMappingGenerationCompleted',
129132
FakerMappingGenerationFailed = 'compass-collection/FakerMappingGenerationFailed',
130-
FakerSchemaEdited = 'compass-collection/FakerSchemaEdited',
133+
FakerFieldTypeChanged = 'compass-collection/FakerFieldTypeChanged',
134+
FakerFieldMethodChanged = 'compass-collection/FakerFieldMethodChanged',
131135
}
132136

133137
interface CollectionMetadataFetchedAction {
@@ -197,9 +201,16 @@ export interface FakerMappingGenerationFailedAction {
197201
requestId: string;
198202
}
199203

200-
export interface FakerSchemaEditedAction {
201-
type: CollectionActions.FakerSchemaEdited;
202-
editedFakerSchema: FakerSchema;
204+
export interface FakerFieldTypeChangedAction {
205+
type: CollectionActions.FakerFieldTypeChanged;
206+
fieldPath: string;
207+
mongoType: MongoDBFieldType;
208+
}
209+
210+
export interface FakerFieldMethodChangedAction {
211+
type: CollectionActions.FakerFieldMethodChanged;
212+
fieldPath: string;
213+
fakerMethod: string;
203214
}
204215

205216
const reducer: Reducer<CollectionState, Action> = (
@@ -495,20 +506,67 @@ const reducer: Reducer<CollectionState, Action> = (
495506
}
496507

497508
if (
498-
isAction<FakerSchemaEditedAction>(
509+
isAction<FakerFieldTypeChangedAction>(
499510
action,
500-
CollectionActions.FakerSchemaEdited
511+
CollectionActions.FakerFieldTypeChanged
501512
)
502513
) {
503514
if (state.fakerSchemaGeneration.status !== 'completed') {
504515
return state;
505516
}
506517

518+
const { fieldPath, mongoType } = action;
519+
const currentMapping =
520+
state.fakerSchemaGeneration.editedFakerSchema[fieldPath];
521+
522+
if (!currentMapping) {
523+
return state;
524+
}
525+
507526
return {
508527
...state,
509528
fakerSchemaGeneration: {
510529
...state.fakerSchemaGeneration,
511-
editedFakerSchema: action.editedFakerSchema,
530+
editedFakerSchema: {
531+
...state.fakerSchemaGeneration.editedFakerSchema,
532+
[fieldPath]: {
533+
...currentMapping,
534+
mongoType,
535+
},
536+
},
537+
},
538+
};
539+
}
540+
541+
if (
542+
isAction<FakerFieldMethodChangedAction>(
543+
action,
544+
CollectionActions.FakerFieldMethodChanged
545+
)
546+
) {
547+
if (state.fakerSchemaGeneration.status !== 'completed') {
548+
return state;
549+
}
550+
551+
const { fieldPath, fakerMethod } = action;
552+
const currentMapping =
553+
state.fakerSchemaGeneration.editedFakerSchema[fieldPath];
554+
555+
if (!currentMapping) {
556+
return state;
557+
}
558+
559+
return {
560+
...state,
561+
fakerSchemaGeneration: {
562+
...state.fakerSchemaGeneration,
563+
editedFakerSchema: {
564+
...state.fakerSchemaGeneration.editedFakerSchema,
565+
[fieldPath]: {
566+
...currentMapping,
567+
fakerMethod,
568+
},
569+
},
512570
},
513571
};
514572
}
@@ -554,10 +612,26 @@ export const mockDataGeneratorPreviousButtonClicked = (): CollectionThunkAction<
554612
};
555613
};
556614

557-
export const updateEditedFakerSchema = (
558-
editedFakerSchema: FakerSchema
559-
): FakerSchemaEditedAction => {
560-
return { type: CollectionActions.FakerSchemaEdited, editedFakerSchema };
615+
export const fakerFieldTypeChanged = (
616+
fieldPath: string,
617+
mongoType: MongoDBFieldType
618+
): FakerFieldTypeChangedAction => {
619+
return {
620+
type: CollectionActions.FakerFieldTypeChanged,
621+
fieldPath,
622+
mongoType,
623+
};
624+
};
625+
626+
export const fakerFieldMethodChanged = (
627+
fieldPath: string,
628+
fakerMethod: string
629+
): FakerFieldMethodChangedAction => {
630+
return {
631+
type: CollectionActions.FakerFieldMethodChanged,
632+
fieldPath,
633+
fakerMethod,
634+
};
561635
};
562636

563637
export const selectTab = (

0 commit comments

Comments
 (0)