From fb213f5c2fe5be8da335e4dcafeb3a99f93d083e Mon Sep 17 00:00:00 2001
From: Pithon Kabiro <pithon.kabiro@hft-stuttgart.de>
Date: Thu, 28 Oct 2021 10:57:56 +0200
Subject: [PATCH] New function: create error popup message

Use a third-party library 'SweetAlert' to create popup messages when
errors are thrown as a result of attempting to draw charts
---
 public/index.html                             |  3 +++
 public/js/appChart.js                         |  5 +++--
 public/js/src_modules/dropDownListHelpers.mjs | 15 +++++++++++++++
 3 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/public/index.html b/public/index.html
index 10807a7..789d4ec 100644
--- a/public/index.html
+++ b/public/index.html
@@ -65,6 +65,9 @@
     <!-- vanillaSelectBox -->
     <link href="./css/thirdparty/vanillaSelectBox.css" rel="stylesheet" />
 
+    <!-- SweetAlert -->
+    <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
+
     <!--     
     Custom JS -->
     <script defer type="module" src="./js/appCesium.js"></script>
diff --git a/public/js/appChart.js b/public/js/appChart.js
index de1a5cf..c3a7f6a 100644
--- a/public/js/appChart.js
+++ b/public/js/appChart.js
@@ -40,6 +40,7 @@ import {
   checkIfSelectedBuildingDataPointsOptionsAreValid,
   checkIfSelectedAggregationOptionsAreValid,
   getAbbreviationsForSelectedOptionsFromAllDropDownLists,
+  createErrorPopupMessage,
 } from "./src_modules/dropDownListHelpers.mjs";
 
 import { drawColumnChartBasedOnSelectedOptions } from "./src_modules/dropDownListChartColumn.mjs";
@@ -256,8 +257,8 @@ const drawChartUsingSelectedOptions = async function () {
   } catch (err) {
     console.error(err);
 
-    // Display a dialog window with the error message
-    alert(err);
+    // Display a popup that displays the error message
+    createErrorPopupMessage(err);
   } finally {
     // Enable the 'draw chart' button
     enableDrawChartButton();
diff --git a/public/js/src_modules/dropDownListHelpers.mjs b/public/js/src_modules/dropDownListHelpers.mjs
index bb37a61..1231b4e 100644
--- a/public/js/src_modules/dropDownListHelpers.mjs
+++ b/public/js/src_modules/dropDownListHelpers.mjs
@@ -471,6 +471,20 @@ const getAbbreviationsForSelectedOptionsFromAllDropDownLists = function (
   }
 };
 
+/**
+ * Create a pop up message (when an error is thrown) using the SweetAlert library
+ *
+ * @param {Object} errorObj An error object
+ * @returns {undefined} undefined
+ */
+const createErrorPopupMessage = function (errorObj) {
+  swal({
+    title: "Something went wrong!",
+    text: `${errorObj.message}`,
+    icon: "error",
+  });
+};
+
 export {
   splitMultipleOptionsTextDelimitedBySlash,
   getSelectedOptionsFromAllDropDownLists,
@@ -481,4 +495,5 @@ export {
   checkIfSelectedBuildingDataPointsOptionsAreValid,
   checkIfSelectedAggregationOptionsAreValid,
   getAbbreviationsForSelectedOptionsFromAllDropDownLists,
+  createErrorPopupMessage,
 };
-- 
GitLab