Skip to content

Commit

Permalink
Feat(Data Mapper): Able to save data map (#4950)
Browse files Browse the repository at this point in the history
* save with new service works

* cleanup

* getting schema options for dropdown with new service

* cleanump
  • Loading branch information
DanielleCogs committed Jun 10, 2024
1 parent 8d5133e commit 9bfc3f0
Show file tree
Hide file tree
Showing 22 changed files with 1,009 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DataMapperDesigner as DataMapperDesignerV2,
DataMapDataProvider as DataMapDataProviderV2,
DataMapperDesignerProvider as DataMapperDesignerProviderV2,
IDataMapperFileService,
} from '@microsoft/logic-apps-data-mapper-v2';
import type { AppDispatch, RootState } from '../state/Store';
import { AzureThemeDark } from '@fluentui/azure-themes/lib/azure/AzureThemeDark';
Expand Down Expand Up @@ -41,6 +42,30 @@ const mockFileItems: IFileSysTreeItem[] = [
},
];

class DataMapperFileService implements IDataMapperFileService {
private verbose: boolean;

constructor(verbose: boolean) {
this.verbose = verbose
}

public saveMapDefinitionCall = (
dataMapDefinition: string,
mapMetadata: string
) => {
if (this.verbose) {
console.log('Saved definition: ' + dataMapDefinition)
console.log('Saved metadata: ' + mapMetadata)
}
};

public readCurrentSchemaOptions = () => {
return;
};
}



const customXsltPath = ['folder/file.xslt', 'file2.xslt'];

export const DataMapperStandaloneDesignerV2 = () => {
Expand All @@ -62,13 +87,7 @@ export const DataMapperStandaloneDesignerV2 = () => {
accessToken: armToken,
});

const saveMapDefinitionCall = (dataMapDefinition: string, mapMetadata: string) => {
console.log('Map Definition\n===============');
console.log(dataMapDefinition);

console.log('Map Metadata\n===============');
console.log(mapMetadata);
};
const dataMapperFileService = new DataMapperFileService(true);

const saveXsltCall = (dataMapXslt: string) => {
console.log('\nXSLT\n===============');
Expand Down Expand Up @@ -117,11 +136,7 @@ export const DataMapperStandaloneDesignerV2 = () => {
fetchedFunctions={fetchedFunctions}
theme={theme}
>
<DataMapperDesignerV2
saveMapDefinitionCall={saveMapDefinitionCall}
saveXsltCall={saveXsltCall}
readCurrentSchemaOptions={() => null}
/>
<DataMapperDesignerV2 fileService={dataMapperFileService} saveXsltCall={saveXsltCall} />
</DataMapDataProviderV2>
</DataMapperDesignerProviderV2>
</div>
Expand Down
206 changes: 205 additions & 1 deletion apps/vs-code-react/src/app/dataMapper/appV2.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,207 @@
import { changeFetchedFunctions, changeSourceSchema, changeTargetSchema } from '../../state/DataMapSlice';
import type { AppDispatch, RootState } from '../../state/store';
import { VSCodeContext } from '../../webviewCommunication';
import { DataMapperFileService} from './services/dataMapperFileService';
import {
getFileNameAndPath,
DataMapDataProvider,
DataMapperDesigner,
DataMapperDesignerProvider,
InitDataMapperApiService,
defaultDataMapperApiServiceOptions,
getFunctions,
getSelectedSchema,
} from '@microsoft/logic-apps-data-mapper-v2';
import { getTheme, useThemeObserver } from '@microsoft/logic-apps-designer';
import type { Theme } from '@microsoft/logic-apps-shared';
import type { MessageToVsix } from '@microsoft/vscode-extension-logic-apps';
import { ExtensionCommand } from '@microsoft/vscode-extension-logic-apps';
import { useCallback, useContext, useEffect, useMemo, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';

// interface SchemaFile {
// path: string;
// type: SchemaType;
// }

export const DataMapperAppV2 = () => {
return <></>;
const dispatch = useDispatch<AppDispatch>();
const vscode = useContext(VSCodeContext);
const [theme, setTheme] = useState<Theme>(getTheme(document.body));
const xsltFilename = useSelector((state: RootState) => state.dataMapDataLoader.xsltFilename);
const xsltContent = useSelector((state: RootState) => state.dataMapDataLoader.xsltContent);
const mapDefinition = useSelector((state: RootState) => state.dataMapDataLoader.mapDefinition);
const mapMetadata = useSelector((state: RootState) => state.dataMapDataLoader.dataMapMetadata);
const sourceSchemaFilename = useSelector((state: RootState) => state.dataMapDataLoader.sourceSchemaFilename);
const sourceSchema = useSelector((state: RootState) => state.dataMapDataLoader.sourceSchema);
const targetSchemaFilename = useSelector((state: RootState) => state.dataMapDataLoader.targetSchemaFilename);
const targetSchema = useSelector((state: RootState) => state.dataMapDataLoader.targetSchema);
// const schemaFileList = useSelector((state: RootState) => state.dataMapDataLoader.schemaFileList);
const customXsltPathsList = useSelector((state: RootState) => state.dataMapDataLoader.customXsltPathsList);
const fetchedFunctions = useSelector((state: RootState) => state.dataMapDataLoader.fetchedFunctions);

const runtimePort = useSelector((state: RootState) => state.dataMapDataLoader.runtimePort);

if (runtimePort) {
InitDataMapperApiService({
...defaultDataMapperApiServiceOptions,
port: runtimePort ?? defaultDataMapperApiServiceOptions.port,
});
}

const sendMsgToVsix = useCallback(
(msg: MessageToVsix) => {
vscode.postMessage(msg);
},
[vscode]
);

const dataMapperFileService = useMemo(() => {
return new DataMapperFileService(sendMsgToVsix);
} , [sendMsgToVsix]);

// const addSchemaFromFile = (selectedSchemaFile: SchemaFile) => {
// sendMsgToVsix({
// command: ExtensionCommand.addSchemaFromFile,
// data: { path: selectedSchemaFile.path, type: selectedSchemaFile.type as SchemaType },
// });
// };

const readLocalxsltFileOptions = useCallback(() => {
sendMsgToVsix({
command: ExtensionCommand.readLocalCustomXsltFileOptions,
});
}, [sendMsgToVsix]);

const saveXsltCall = (dataMapXslt: string) => {
sendMsgToVsix({
command: ExtensionCommand.saveDataMapXslt,
data: dataMapXslt,
});
};

const saveDraftDataMapDefinition = (dataMapDefinition: string) => {
sendMsgToVsix({
command: ExtensionCommand.saveDraftDataMapDefinition,
data: dataMapDefinition,
});
};

const setIsMapStateDirty = (isMapStateDirty: boolean) => {
sendMsgToVsix({
command: ExtensionCommand.setIsMapStateDirty,
data: isMapStateDirty,
});
};

const handleRscLoadError = useCallback(
(error: unknown) => {
let errorMsg: string;

if (error instanceof Error) {
errorMsg = error.message;
} else if (typeof error === 'string') {
errorMsg = error;
} else {
errorMsg = 'Unknown error';
}

sendMsgToVsix({
command: ExtensionCommand.webviewRscLoadError,
data: errorMsg,
});
},
[sendMsgToVsix]
);

useEffect(() => {
sendMsgToVsix({
command: ExtensionCommand.getFunctionDisplayExpanded,
});
}, [sendMsgToVsix]);

// Notify VS Code that webview is loaded
useEffect(() => {
sendMsgToVsix({
command: ExtensionCommand.webviewLoaded,
});
}, [sendMsgToVsix]);

// Monitor document.body for VS Code theme changes
useThemeObserver(document.body, theme, setTheme, {
attributes: true,
});

// Init runtime API service and make calls
useEffect(() => {
const fetchFunctionList = async () => {
try {
const fnManifest = await getFunctions();

if (typeof fnManifest !== 'string') {
dispatch(changeFetchedFunctions(fnManifest));
} else {
const errorMessage = `Failed to fetch Function manifest: ${fnManifest}}`;

throw new Error(errorMessage);
}
} catch (error) {
handleRscLoadError(error);
}
};

const getSelectedSchemaTrees = async () => {
try {
if (sourceSchemaFilename) {
const [fileName, filePath] = getFileNameAndPath(sourceSchemaFilename);
dispatch(changeSourceSchema(await getSelectedSchema(fileName, filePath)));
}

if (targetSchemaFilename) {
const [fileName, filePath] = getFileNameAndPath(targetSchemaFilename);
dispatch(changeTargetSchema(await getSelectedSchema(fileName, filePath)));
}
} catch (error) {
handleRscLoadError(error);
}
};

if (runtimePort) {
InitDataMapperApiService({
...defaultDataMapperApiServiceOptions,
port: runtimePort ?? defaultDataMapperApiServiceOptions.port,
});

fetchFunctionList();
getSelectedSchemaTrees();
}
}, [dispatch, runtimePort, sourceSchemaFilename, targetSchemaFilename, handleRscLoadError]);

return (
<DataMapperDesignerProvider locale="en-US" theme={theme} options={{}}>
<DataMapDataProvider
dataMapMetadata={mapMetadata}
xsltFilename={xsltFilename}
xsltContent={xsltContent}
mapDefinition={mapDefinition}
sourceSchema={sourceSchema}
targetSchema={targetSchema}
availableSchemas={undefined}
customXsltPaths={customXsltPathsList}
fetchedFunctions={fetchedFunctions}
// Passed in here too so it can be managed in the Redux store so components can track the current theme
theme={theme}
>
<div style={{ height: '100vh', overflow: 'hidden' }}>
<DataMapperDesigner
fileService={dataMapperFileService}
saveXsltCall={saveXsltCall}
saveDraftStateCall={saveDraftDataMapDefinition}
readCurrentCustomXsltPathOptions={readLocalxsltFileOptions}
setIsMapStateDirty={setIsMapStateDirty}
/>
</div>
</DataMapDataProvider>
</DataMapperDesignerProvider>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { IDataMapperFileService } from "@microsoft/logic-apps-data-mapper-v2";
import type {
MessageToVsix} from "@microsoft/vscode-extension-logic-apps";
import {
ExtensionCommand
} from "@microsoft/vscode-extension-logic-apps";

export class DataMapperFileService implements IDataMapperFileService {
private sendMsgToVsix: (msg: MessageToVsix) => void;

constructor(sendMsgToVsix: (msg: MessageToVsix) => void) {
this.sendMsgToVsix = sendMsgToVsix;
}

public saveMapDefinitionCall = (
dataMapDefinition: string,
mapMetadata: string
) => {
this.sendMsgToVsix({
command: ExtensionCommand.saveDataMapDefinition,
data: dataMapDefinition,
});
this.sendMsgToVsix({
command: ExtensionCommand.saveDataMapMetadata,
data: mapMetadata,
});
};

public readCurrentSchemaOptions = () => {
this.sendMsgToVsix({
command: ExtensionCommand.readLocalSchemaFileOptions,
});
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export interface AddOrUpdateSchemaViewProps {
selectedSchema?: string;
selectedSchemaFile?: SchemaFile;
setSelectedSchemaFile: (item?: SchemaFile) => void;
getUpdatedSchemaFiles: () => void;
errorMessage: string;
uploadType: UploadSchemaTypes;
setUploadType: (newUploadType: UploadSchemaTypes) => void;
Expand All @@ -43,7 +42,6 @@ export const AddOrUpdateSchemaView = ({
schemaType,
selectedSchemaFile,
setSelectedSchemaFile,
getUpdatedSchemaFiles,
errorMessage,
uploadType,
setUploadType,
Expand Down Expand Up @@ -145,7 +143,6 @@ export const AddOrUpdateSchemaView = ({
setSelectSchemaVisible(false);
setSelectedSchemaFile(schema);
}}
getUpdatedSchemaFiles={getUpdatedSchemaFiles}
/>
)}
</div>
Expand Down
13 changes: 6 additions & 7 deletions libs/data-mapper-v2/src/components/addSchema/AddSchemaPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { getSelectedSchema } from '../../core';
import { DataMapperFileService, getSelectedSchema } from '../../core';
import { setInitialSchema } from '../../core/state/DataMapSlice';
import { closePanel, ConfigPanelView, openDefaultConfigPanelView } from '../../core/state/PanelSlice';
import type { AppDispatch, RootState } from '../../core/state/Store';
Expand All @@ -23,14 +23,14 @@ const schemaFileQuerySettings = {

export interface ConfigPanelProps {
onSubmitSchemaFileSelection: (schemaFile: SchemaFile) => void;
readCurrentSchemaOptions: () => void;
schemaType: SchemaType;
}

export const AddSchemaDrawer = ({ readCurrentSchemaOptions, onSubmitSchemaFileSelection, schemaType }: ConfigPanelProps) => {
export const AddSchemaDrawer = ({ onSubmitSchemaFileSelection, schemaType }: ConfigPanelProps) => {
const dispatch = useDispatch<AppDispatch>();
const intl = useIntl();
const styles = useStyles();
const fileService = DataMapperFileService();

const curDataMapOperation = useSelector((state: RootState) => state.dataMap.present.curDataMapOperation);
const currentPanelView = useSelector((state: RootState) => {
Expand Down Expand Up @@ -172,10 +172,10 @@ export const AddSchemaDrawer = ({ readCurrentSchemaOptions, onSubmitSchemaFileSe

// Read current schema file options if method exists
useEffect(() => {
if (readCurrentSchemaOptions) {
readCurrentSchemaOptions();
if (fileService && fileService.readCurrentSchemaOptions) {
fileService.readCurrentSchemaOptions();
}
}, [readCurrentSchemaOptions]);
}, [fileService]);

const onRenderFooterContent = useCallback(() => {
if (currentPanelView === ConfigPanelView.DefaultConfig) {
Expand Down Expand Up @@ -232,7 +232,6 @@ export const AddSchemaDrawer = ({ readCurrentSchemaOptions, onSubmitSchemaFileSe
errorMessage={errorMessage}
uploadType={uploadType}
setUploadType={setUploadType}
getUpdatedSchemaFiles={readCurrentSchemaOptions}
/>
</InlineDrawer>
</div>
Expand Down
Loading

0 comments on commit 9bfc3f0

Please sign in to comment.