From d9a692eaff3478822ea3401844029cdfbb32671c Mon Sep 17 00:00:00 2001
From: Robin Leber <rleber98@gmail.com>
Date: Wed, 3 Jul 2024 11:34:14 +0200
Subject: [PATCH] FEAT: editor, no saves when cancel

---
 src/functions/codeEditor.js                      | 13 +++++++++++--
 .../{pipeBinding.js => codeEditorHandler.js}     | 16 ++++++++++++++++
 src/functions/naming.js                          |  2 +-
 3 files changed, 28 insertions(+), 3 deletions(-)
 rename src/functions/{pipeBinding.js => codeEditorHandler.js} (94%)

diff --git a/src/functions/codeEditor.js b/src/functions/codeEditor.js
index a8b4832..e66ff7a 100644
--- a/src/functions/codeEditor.js
+++ b/src/functions/codeEditor.js
@@ -3,7 +3,11 @@ import { EditorState } from "@codemirror/state";
 import { javascript } from "@codemirror/lang-javascript";
 import { createCustomCode } from "./api";
 import { showCheck } from "./visualValidation";
-import { handlePipeBinding, getPipesForFilter } from "./pipeBinding";
+import {
+  handlePipeBinding,
+  getPipesForFilter,
+  deleteWrittenCode,
+} from "./codeEditorHandler";
 
 let editorValues = {
   doc: "// Please don't remove comments!\nconst amqp = require('amqplib/callback_api');\n\nconst rabbitmqUrl = 'amqp://mquser:mqpass@rabbit:5672';\n\namqp.connect(rabbitmqUrl, (error0, connection) => {\n    if (error0) {\n        throw error0;\n    }\n    connection.createChannel((error1, channel) => {\n        if (error1) {\n            throw error1;\n        }\n\n        // START PIPE-BINDING\n        // INCOMING PIPES\n        // OUTGOING PIPES\n        // END PIPE-BINDING\n\n        // TODO: Code logic here\n\n\n\t});\n});\n",
@@ -140,7 +144,12 @@ export const codeEditorElement = (instance) => {
 };
 
 const handleCancel = (node) => {
-  node.style.visibility = "hidden";
+  if (confirm("No changes will be saved!")) {
+    node.style.visibility = "hidden";
+    deleteWrittenCode(node.editor);
+  } else {
+    return;
+  }
 };
 const handleSubmit = (node, code) => {
   const requestBody = {
diff --git a/src/functions/pipeBinding.js b/src/functions/codeEditorHandler.js
similarity index 94%
rename from src/functions/pipeBinding.js
rename to src/functions/codeEditorHandler.js
index 4f82826..7cd563f 100644
--- a/src/functions/pipeBinding.js
+++ b/src/functions/codeEditorHandler.js
@@ -235,3 +235,19 @@ export const renamePipeNamesInCode = (editor, oldName, newName) => {
   });
   editor.dispatch(transaction);
 };
+
+export const deleteWrittenCode = (editor) => {
+  const codeString = editor.state.doc.toString();
+  const from = "        // TODO: Code logic here";
+  const fromIndex = codeString.indexOf(from);
+  const transaction = editor.state.update({
+    changes: [
+      {
+        from: fromIndex + from.length,
+        to: editor.state.doc.length,
+        insert: "\n\n\n\t});\n});\n",
+      },
+    ],
+  });
+  editor.dispatch(transaction);
+};
diff --git a/src/functions/naming.js b/src/functions/naming.js
index b11d732..d9ae899 100644
--- a/src/functions/naming.js
+++ b/src/functions/naming.js
@@ -1,6 +1,6 @@
 import { showCheck } from "./visualValidation";
 import { appState } from "./state";
-import { renamePipeNamesInCode } from "./pipeBinding";
+import { renamePipeNamesInCode } from "./codeEditorHandler";
 
 export const namePipe = (instance) => {
   let newPipeName;
-- 
GitLab