Skip to content

Commit a2e0a10

Browse files
committed
Move saveBlocks out of ModuleOutline
1 parent e5a7291 commit a2e0a10

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

src/App.tsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const App: React.FC = () => {
7373
if (generatorContext.current) {
7474
generatorContext.current.setModule(currentModule);
7575
}
76-
if(blocksEditor.current){
76+
if (blocksEditor.current) {
7777
blocksEditor.current.loadModuleBlocks(currentModule);
7878
}
7979
}, [currentModule]);
@@ -170,6 +170,28 @@ const App: React.FC = () => {
170170
setTriggerPythonRegeneration(Date.now());
171171
};
172172

173+
const saveBlocks = async (): Promise<boolean> => {
174+
return new Promise(async (resolve, reject) => {
175+
if (!blocksEditor.current) {
176+
reject(new Error());
177+
return;
178+
}
179+
try {
180+
await blocksEditor.current.saveBlocks();
181+
messageApi.open({
182+
type: 'success',
183+
content: 'Save completed successfully.',
184+
});
185+
resolve(true);
186+
} catch (e) {
187+
console.log('Failed to save the blocks. Caught the following error...');
188+
console.log(e);
189+
setAlertErrorMessage('Failed to save the blocks.');
190+
reject(new Error('Failed to save the blocks.'));
191+
}
192+
});
193+
};
194+
173195
const handleToolboxSettingsClicked = () => {
174196
setToolboxSettingsModalIsOpen(true);
175197
};
@@ -221,6 +243,7 @@ const App: React.FC = () => {
221243
storage={storage}
222244
messageApi={messageApi}
223245
setAlertErrorMessage={setAlertErrorMessage}
246+
saveBlocks={saveBlocks}
224247
blocksEditor={blocksEditor.current}
225248
initializeShownPythonToolboxCategories={initializeShownPythonToolboxCategories}
226249
currentModule={currentModule}

src/reactComponents/ModuleOutline.tsx

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ interface ModuleOutlineProps {
4242
blocksEditor: editor.Editor | null;
4343
currentModule: commonStorage.Module | null;
4444
setCurrentModule: (module: commonStorage.Module | null) => void;
45+
saveBlocks: () => Promise<boolean>;
4546
}
4647

4748
export default function ModuleOutline(props: ModuleOutlineProps) {
@@ -274,7 +275,7 @@ export default function ModuleOutline(props: ModuleOutlineProps) {
274275
// Set the function to be executed if the user clicks 'ok'.
275276
afterPopconfirmOk.current = async () => {
276277
setPopconfirmLoading(true);
277-
const success = await saveBlocks();
278+
const success = await props.saveBlocks();
278279
setOpenPopconfirm(false);
279280
setPopconfirmLoading(false);
280281
if (success) {
@@ -644,30 +645,7 @@ export default function ModuleOutline(props: ModuleOutlineProps) {
644645
}
645646
};
646647
const handleSaveClicked = async () => {
647-
saveBlocks();
648-
};
649-
650-
const saveBlocks = async (): Promise<boolean> => {
651-
return new Promise(async (resolve, reject) => {
652-
if (!props.blocksEditor || !currentModulePath) {
653-
reject(new Error());
654-
return;
655-
}
656-
try {
657-
await props.blocksEditor.saveBlocks();
658-
props.messageApi.open({
659-
type: 'success',
660-
content: 'Save completed successfully.',
661-
});
662-
resolve(true);
663-
} catch (e) {
664-
console.log('Failed to save the blocks. Caught the following error...');
665-
console.log(e);
666-
props.setAlertErrorMessage('Failed to save the blocks.');
667-
reject(new Error('Failed to save the blocks.'));
668-
}
669-
670-
});
648+
props.saveBlocks();
671649
};
672650

673651
const initializeModules = async () => {

0 commit comments

Comments
 (0)