From 6ae79ff0543247102280038918d2c0c5b6a340ef Mon Sep 17 00:00:00 2001
From: bill bittner <bittner.w@gmail.com>
Date: Tue, 5 Dec 2017 11:11:32 -0800
Subject: [PATCH] updated names of client asset functions

---
 public/assets/js/assetConstructor.js | 86 +++++++++++++++++-----------
 views/partials/asset.handlebars      |  2 +-
 2 files changed, 53 insertions(+), 35 deletions(-)

diff --git a/public/assets/js/assetConstructor.js b/public/assets/js/assetConstructor.js
index 4fe153f1..81290526 100644
--- a/public/assets/js/assetConstructor.js
+++ b/public/assets/js/assetConstructor.js
@@ -76,49 +76,67 @@ const Asset = function () {
         failureMessage.hidden = false;
         errorMessage.innerText = msg;
     };
-    this.checkClaimAvailability = function () {
+    this.checkFileAndRenderAsset = function () {
         const that = this;
+        this.isFileAvailable()
+            .then(isAvailable => {
+                if (!isAvailable) {
+                    console.log('file is not yet available on spee.ch');
+                    that.showSearchMessage();
+                    return that.getAssetOnSpeech();
+                }
+            })
+            .then(() => {
+                that.showAsset();
+            })
+            .catch(error => {
+                that.showFailureMessage(error);
+            })
+    };
+    this.isFileAvailable = function () {
+        console.log(`checking if file is available for ${this.state.claimName}#${this.state.claimId}`)
         const uri = `/api/file-is-available/${this.state.claimName}/${this.state.claimId}`;
         const xhr = new XMLHttpRequest();
-        console.log(`checking local availability for ${this.state.claimName}#${this.state.claimId}`)
-        xhr.open("GET", uri, true);
-        xhr.onreadystatechange = function() {
-            if (xhr.readyState == 4) {
-                const response = JSON.parse(xhr.response);
-                if (xhr.status == 200) {
-                    if (response.message === true) {
-                        console.log('local asset is available on spee.ch')
-                        that.showAsset();
+        return new Promise((resolve, reject) => {
+            xhr.open("GET", uri, true);
+            xhr.onreadystatechange = function() {
+                if (xhr.readyState == 4) {
+                    const response = JSON.parse(xhr.response);
+                    if (xhr.status == 200) {
+                        console.log('isFileAvailable succeeded:', response);
+                        if (response.message === true) {
+                            resolve(true);
+                        } else {
+                            resolve(false);
+                        }
                     } else {
-                        that.showSearchMessage();
-                        that.getAsset()
+                        console.log('isFileAvailable failed:', response);
+                        reject('Well this sucks, but we can\'t seem to phone home');
                     }
-                } else {
-                    console.log('get failed:', response);
-                    that.showFailureMessage('Well this sucks, but we can\'t seem to phone home');
                 }
-            }
-        };
-        xhr.send();
+            };
+            xhr.send();
+        })
     };
-    this.getAsset = function() {
-        const that = this;
+    this.getAssetOnSpeech = function() {
+        console.log(`getting claim for ${this.state.claimName}#${this.state.claimId}`)
         const uri = `/api/claim-get/${this.state.claimName}/${this.state.claimId}`;
         const xhr = new XMLHttpRequest();
-        console.log(`getting ${this.state.claimName}#${this.state.claimId}`)
-        xhr.open("GET", uri, true);
-        xhr.onreadystatechange = function() {
-            if (xhr.readyState == 4) {
-                const response = JSON.parse(xhr.response);
-                if (xhr.status == 200) {
-                    console.log('get returned successfully', response);
-                    that.showAsset();
-                } else {
-                    console.log('get failed:', response);
-                    that.showFailureMessage(`${response.message}`);
+        return new Promise((resolve, reject) => {
+            xhr.open("GET", uri, true);
+            xhr.onreadystatechange = function() {
+                if (xhr.readyState == 4) {
+                    const response = JSON.parse(xhr.response);
+                    if (xhr.status == 200) {
+                        console.log('getAssetOnSpeech succeeded:', response)
+                        resolve(true);
+                    } else {
+                        console.log('getAssetOnSpeech failed:', response);
+                        reject(response.message);
+                    }
                 }
-            }
-        };
-        xhr.send();
+            };
+            xhr.send();
+        })
     };
 };
\ No newline at end of file
diff --git a/views/partials/asset.handlebars b/views/partials/asset.handlebars
index 0299cea3..0e8db94e 100644
--- a/views/partials/asset.handlebars
+++ b/views/partials/asset.handlebars
@@ -32,6 +32,6 @@
     asset.setState('fileExt', '{{claimInfo.fileExt}}');
     asset.setState('contentType', '{{claimInfo.contentType}}');
     console.log('asset state:', asset.state);
-    asset.checkClaimAvailability();
+    asset.checkFileAndRenderAsset();
 
 </script>
\ No newline at end of file