commit 4a772c43fc879bdf5909cc8bdeddf0c412c34179
Author: Jan Pašek <janpasek97@gmail.com>
Date:   Tue Apr 20 15:31:17 2021 +0200

    Re #8583 - certificate.js and utilities.js using await

diff --git a/static/js/certificate.js b/static/js/certificate.js
index 78066d7..1a9b8f2 100644
--- a/static/js/certificate.js
+++ b/static/js/certificate.js
@@ -52,59 +52,54 @@ var certificateDetailsApp = new Vue({
     },
     watch: {},
     methods: {
-        onCertificateDownload: function () {
-            onCertificateDownload(this.id);
+        onCertificateDownload: async function () {
+            await onCertificateDownload(this.id);
         },
-        onRootDownload: function () {
-            onCertificateRootDownload(this.id);
+        onRootDownload: async function () {
+            await onCertificateRootDownload(this.id);
         },
-        onChainDownload: function () {
-            onCertificateChainDownload(this.id);
+        onChainDownload: async function () {
+            await onCertificateChainDownload(this.id);
         },
-        onPublicKeyDownload: function () {
-            onPublicKeyDownload(this.id);
+        onPublicKeyDownload: async function () {
+            await onPublicKeyDownload(this.id);
         },
-        onPrivateKeyDownload: function () {
-            onPrivateKeyDownload(this.id);
+        onPrivateKeyDownload: async function () {
+            await onPrivateKeyDownload(this.id);
         },
-        onRevoke: function () {
+        onRevoke: async function () {
             document.body.scrollTop = 0;
             document.documentElement.scrollTop = 0;
-            axios.patch(API_URL + "certificates/" + this.id, {
-                status: "revoked",
-                reason: this.revocationReason
-            })
-                .then(function (response) {
-                    if (response.data["success"]) {
-                        certificateDetailsApp.successMessage = "Certificate revocation successful.";
-                    } else {
-                        certificateDetailsApp.errorMessage = "Certificate cannot be revoked - " + response.data["data"] + "!";
-                    }
-                })
-                .catch(function (error) {
-                    certificateDetailsApp.errorMessage = "An error occurred while revoking certificate";
+            try {
+                const response = await axios.patch(API_URL + "certificates/" + this.id, {
+                    status: "revoked",
+                    reason: this.revocationReason
                 });
+                if (response.data["success"]) {
+                    certificateDetailsApp.successMessage = "Certificate revocation successful.";
+                } else {
+                    certificateDetailsApp.errorMessage = "Certificate cannot be revoked - " + response.data["data"] + "!";
+                }
+            } catch (error) {
+                certificateDetailsApp.errorMessage = "An error occurred while revoking certificate";
+            }
         },
-        onDelete: function () {
+        onDelete: async function () {
             document.body.scrollTop = 0;
             document.documentElement.scrollTop = 0;
-            axios.delete(API_URL + "certificates/" + this.id)
-                .then(function (response) {
-                    if (response.data["success"]) {
-                        window.location.href = "/static/index.html?success=Certificate+successfully+deleted";
-                    } else {
-                        certificateDetailsApp.errorMessage = "Certificate cannot be deleted - " + response.data["data"] + "!";
-                    }
-                })
-                .catch(function (error) {
-                    certificateDetailsApp.errorMessage = "An error occurred while deleting certificate";
-                });
-        },
-        loadIssuedCertificates: function () {
-
+            try {
+                const response = await axios.delete(API_URL + "certificates/" + this.id);
+                if (response.data["success"]) {
+                    window.location.href = "/static/index.html?success=Certificate+successfully+deleted";
+                } else {
+                    certificateDetailsApp.errorMessage = "Certificate cannot be deleted - " + response.data["data"] + "!";
+                }
+            } catch (error) {
+                certificateDetailsApp.errorMessage = "An error occurred while deleting certificate";
+            }
         }
     },
-    mounted: function () {
+    mounted: async function () {
         // get details of the selected certificate
         const params = window.location.search;
         if (params !== "") {
@@ -113,46 +108,25 @@ var certificateDetailsApp = new Vue({
             if (urlParams.get("id") != null) {
                 const id = urlParams.get("id");
                 this.id = id;
+                var loadedDetails = false;
                 // query certificate details
-                axios.get(API_URL + "certificates/" + id + "/details")
-                    .then(function (response) {
-                        if (response.data["success"]) {
-                            // display certificate
-                            certificateDetailsApp.certificate = response.data["data"];
-                            axios.get(API_URL + "certificates", {
-                                params: {
-                                    filtering: {
-                                        issuedby: parseInt(certificateDetailsApp.id)
-                                    }
-                                }
-                            })
-                                .then(function (response) {
-                                    if (response.data["success"]) {
-                                        response.data["data"].forEach(item => {
-                                            if (item.id != certificateDetailsApp.id) certificateDetailsApp.issuedCertificates.push(item)
-                                        })
-                                    } else {
-                                        certificateDetailsApp.issuedCertificates = [];
-                                    }
-                                })
-                                .catch(function (error) {
-                                    console.log(error);
-                                });
-                            certificateDetailsApp.loading = false;
-                            certificateDetailsApp.error = false;
-                        } else {
-                            // certificate does not exists
-                            console.error("Required certificate does not exists");
-                            certificateDetailsApp.loading = false;
-                            certificateDetailsApp.error = true;
-                        }
-                    })
-                    .catch(function (error) {
-                        // server error
-                        console.error(error);
+                try {
+                    const response = await axios.get(API_URL + "certificates/" + id + "/details");
+                    if (response.data["success"]) {
+                        // display certificate
+                        certificateDetailsApp.certificate = response.data["data"];
+                        loadedDetails = true;
+                    } else {
+                        // certificate does not exists
+                        console.error("Required certificate does not exists");
                         certificateDetailsApp.loading = false;
                         certificateDetailsApp.error = true;
-                    });
+                    }
+                } catch (error) {
+                    console.error(error);
+                    certificateDetailsApp.loading = false;
+                    certificateDetailsApp.error = true;
+                }
             } else {
                 // id was not provided as a URL parameter
                 console.error("URL does not contain 'id' parameter")
@@ -164,6 +138,28 @@ var certificateDetailsApp = new Vue({
             certificateDetailsApp.loading = false;
             certificateDetailsApp.error = true;
         }
+
+        if (!loadedDetails) return;
+        try {
+            const response = await axios.get(API_URL + "certificates", {
+                params: {
+                    filtering: {
+                        issuedby: parseInt(certificateDetailsApp.id)
+                    }
+                }
+            });
+            if (response.data["success"]) {
+                response.data["data"].forEach(item => {
+                    if (item.id != certificateDetailsApp.id) certificateDetailsApp.issuedCertificates.push(item)
+                })
+            } else {
+                certificateDetailsApp.issuedCertificates = [];
+            }
+        } catch (error) {
+            console.log(error);
+        }
+        this.loading = false;
+        this.error = false;
     }
 });
 
diff --git a/static/js/utilities.js b/static/js/utilities.js
index d6576d8..1c93a31 100644
--- a/static/js/utilities.js
+++ b/static/js/utilities.js
@@ -21,95 +21,88 @@ function download(filename, text) {
 
 // Adds toDateInputValue() method to Date object
 // toDateInputValue() produces date in YYYY-MM-DD format
-Date.prototype.toDateInputValue = (function() {
+Date.prototype.toDateInputValue = (function () {
     var local = new Date(this);
     local.setMinutes(this.getMinutes() - this.getTimezoneOffset());
-    return local.toJSON().slice(0,10);
+    return local.toJSON().slice(0, 10);
 });
 
 /**
  * Download certificate given by id
  * @param id identifier of certificate to be downloaded
  */
-function onCertificateDownload(id) {
-    axios.get(API_URL + "certificates/" + id)
-        .then(function (response) {
-            if (response.data["success"]) {
-                download(id + ".pem", response.data["data"])
-            } else
-                console.log("Error occurred while downloading the certificate") // TODO more action may be required
-        })
-        .catch(function (error) {
-            console.log(error);
-        });
+async function onCertificateDownload(id) {
+    try {
+        const response = await axios.get(API_URL + "certificates/" + id);
+        if (response.data["success"]) {
+            download(id + ".pem", response.data["data"])
+        } else
+            console.log("Error occurred while downloading the certificate") // TODO more action may be required
+    } catch (error) {
+        console.log(error);
+    }
 }
 
 /**
  * Download root of chain of trust excluding root of given certificate
  * @param id identifier of certificate whose chain shall be downloaded
  */
-function onCertificateChainDownload(id) {
-    axios.get(API_URL + "certificates/" + id + "/chain")
-        .then(function (response) {
-            if(response.data["success"]) {
-                download(id + "_chain.pem", response.data["data"])
-            }
-            else
-                console.log("Error occurred while downloading the certificate's chain of trust") // TODO more action may be required
-        })
-        .catch(function (error) {
-            console.log(error);
-        });
+async function onCertificateChainDownload(id) {
+    try {
+        const response = await axios.get(API_URL + "certificates/" + id + "/chain");
+        if (response.data["success"]) {
+            download(id + "_chain.pem", response.data["data"])
+        } else
+            console.log("Error occurred while downloading the certificate's chain of trust") // TODO more action may be required
+    } catch (error) {
+        console.log(error);
+    }
 }
 
 /**
  * Download root of chain of trust corresponding to the given certificate
  * @param id identifier of certificate whose root shall be downloaded
  */
-function onCertificateRootDownload(id) {
-    axios.get(API_URL + "certificates/" + id + "/root")
-        .then(function (response) {
-            if(response.data["success"]) {
-                download(id + "_root.pem", response.data["data"])
-            }
-            else
-                console.log("Error occurred while downloading the certificate's root CA") // TODO more action may be required
-        })
-        .catch(function (error) {
-            console.log(error);
-        });
+async function onCertificateRootDownload(id) {
+    try {
+        const response = await axios.get(API_URL + "certificates/" + id + "/root");
+        if (response.data["success"]) {
+            download(id + "_root.pem", response.data["data"])
+        } else
+            console.log("Error occurred while downloading the certificate's root CA") // TODO more action may be required
+    } catch (error) {
+        console.log(error);
+    }
 }
 
 /**
  * Download public key associated with the selected certificate
  * @param id identifier of certificate whose pub. key shall be downloaded
  */
-function onPublicKeyDownload(id) {
-    axios.get(API_URL + "certificates/" + id + "/publickey")
-        .then(function (response) {
-            if (response.data["success"]) {
-                download(id + "_public_key.pem", response.data["data"])
-            } else
-                console.log("Error occurred while downloading the certificate's public key") // TODO more action may be required
-        })
-        .catch(function (error) {
-            console.log(error);
-        });
+async function onPublicKeyDownload(id) {
+    try {
+        const response = await axios.get(API_URL + "certificates/" + id + "/publickey");
+        if (response.data["success"]) {
+            download(id + "_public_key.pem", response.data["data"])
+        } else
+            console.log("Error occurred while downloading the certificate's public key") // TODO more action may be required
+    } catch (error) {
+        console.log(error);
+    }
 }
 
 /**
  * Download public key associated with the selected certificate
  * @param id identifier of certificate whose pub. key shall be downloaded
  */
-function onPrivateKeyDownload(id) {
-    axios.get(API_URL + "certificates/" + id + "/privatekey")
-        .then(function (response) {
-            if (response.data["success"]) {
-                download(id + "_private_key.pem", response.data["data"])
-            } else
-                console.log("Error occurred while downloading the certificate's private key") // TODO more action may be required
-        })
-        .catch(function (error) {
-            console.log(error);
-        });
+async function onPrivateKeyDownload(id) {
+    try {
+        const response = await axios.get(API_URL + "certificates/" + id + "/privatekey");
+        if (response.data["success"]) {
+            download(id + "_private_key.pem", response.data["data"])
+        } else
+            console.log("Error occurred while downloading the certificate's private key") // TODO more action may be required
+    } catch (error) {
+        console.log(error);
+    }
 }
\ No newline at end of file
