LP#1673870: Add ebook API place hold functionality to OPAC
authorJeff Davis <jdavis@sitka.bclibraries.ca>
Wed, 22 Mar 2017 20:00:47 +0000 (13:00 -0700)
committerBill Erickson <berickxx@gmail.com>
Fri, 1 Sep 2017 20:06:50 +0000 (16:06 -0400)
Signed-off-by: Jeff Davis <jdavis@sitka.bclibraries.ca>
Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/templates/opac/myopac/ebook_holds.tt2
Open-ILS/src/templates/opac/parts/ebook_api/base_js.tt2
Open-ILS/web/js/ui/default/opac/ebook_api/ebook.js
Open-ILS/web/js/ui/default/opac/ebook_api/loggedin.js

index 76bde68..eeefea3 100644 (file)
@@ -4,6 +4,13 @@
     PROCESS "opac/parts/myopac/column_sort_support.tt2";
     WRAPPER "opac/parts/myopac/base.tt2";
     myopac_page = "ebook_holds";
+    IF CGI.param("action") == 'place_hold';
+        ebook_holds_title = l('Place Hold on E-Item');
+    ELSIF CGI.param("action") == 'cancel_hold';
+        ebook_holds_title = l('Cancel Hold on E-Item');
+    ELSE;
+        ebook_holds_title = l('E-Items on Hold');
+    END;
     limit = (ctx.holds_limit.defined) ? ctx.holds_limit : 0;
     offset = (ctx.holds_offset.defined) ? ctx.holds_offset : 0;
     count = (ctx.holds_ids.size.defined) ? ctx.holds_ids.size : 0;
     </div>
 
     <div class="header_middle">
-        <span class="float-left">[% l('E-Items on Hold') %]</span>
+        <span class="float-left">[% ebook_holds_title %]</span>
     </div>
     <div class="clear-both"></div>
     <div id="no_ebook_holds" class="warning_box hidden">[% l('You have no e-item holds.') %]</div>
+    <div id="ebook_place_hold_failed" class="warning_box hidden">[% l('Hold could not be placed.') %]</div>
+    <div id="ebook_place_hold_succeeded" class="success hidden">[% l('E-item is now on hold.') %]</div>
+    <div id="ebook_cancel_hold_failed" class="warning_box hidden">[% l('Hold could not be canceled.') %]</div>
+    <div id="ebook_cancel_hold_succeeded" class="success hidden">[% l('Your hold has been canceled.') %]</div>
     <div id='ebook_holds_main' class="hidden">
         <table id="ebook_holds_main_table"
-            title="[% l('E-Items on Hold') %]"
+            title="[% ebook_holds_title %]"
             class="table_no_border_space table_no_cell_pad item_list_padding">
             <thead>
             <tr>
index 7479509..9d8b06f 100644 (file)
@@ -13,6 +13,8 @@ l_strings.download = '[% l('Download') %]';
 l_strings.ready_for_checkout = '[% l('Ready for Checkout') %]';
 l_strings.suspended = '[% l('Suspended') %]';
 l_strings.checkout = '[% l('Checkout') %]';
+l_strings.place_hold = '[% l('Place Hold') %]';
+l_strings.cancel_hold = '[% l('Cancel Hold') %]';
 
 // give us cookies!
 dojo.require("dojo.cookie");
index 46b223c..bb90bb0 100644 (file)
@@ -86,3 +86,20 @@ Ebook.prototype.checkout = function(authtoken, patron_id, callback) {
     }).send();
 }
 
+Ebook.prototype.placeHold = function(authtoken, patron_id, callback) {
+    var ses = dojo.cookie(this.vendor);
+    var ebook = this;
+    new OpenSRF.ClientSession('open-ils.ebook_api').request({
+        method: 'open-ils.ebook_api.place_hold',
+        params: [ authtoken, ses, ebook.id, patron_id ],
+        async: true,
+        oncomplete: function(r) {
+            var resp = r.recv();
+            if (resp) {
+                console.log('place hold response: ' + resp.content());
+                return callback(resp.content());
+            }
+        }
+    }).send();
+}
+
index fb79a4c..b03dcb6 100644 (file)
@@ -127,6 +127,13 @@ function updateCheckoutView() {
 }
 
 function updateHoldView() {
+    // handle hold actions
+    if (typeof ebook_action.type !== 'undefined') {
+        getReadyForHold();
+        return;
+    }
+
+    // no hold action, just displaying holds
     var holds_pending = xacts.holds_pending;
     var holds_ready = xacts.holds_ready;
 
@@ -196,6 +203,43 @@ function getReadyForCheckout() {
     }
 }
 
+// set up page for user to place a hold
+function getReadyForHold() {
+    if (typeof active_ebook === 'undefined') {
+        console.log('No active ebook specified, cannot prepare for hold');
+        dojo.removeClass('ebook_hold_failed', "hidden");
+    } else {
+        active_ebook.getDetails( function(ebook) {
+            dojo.empty('ebook_holds_main_table_body');
+            var tr = dojo.create("tr", null, dojo.byId('ebook_holds_main_table_body'));
+            dojo.create("td", { innerHTML: ebook.title }, tr);
+            dojo.create("td", { innerHTML: ebook.author }, tr);
+            dojo.create("td", null, tr); // Expire Date
+            dojo.create("td", null, tr); // Status
+            dojo.create("td", { id: "hold-button-td" }, tr);
+            if (ebook_action.type == 'place_hold') {
+                var button = dojo.create("input", { id: "hold-button", type: "button", value: l_strings.place_hold }, dojo.byId('hold-button-td'));
+                ebook.conns.checkout = dojo.connect(button, 'onclick', "doPlaceHold");
+            } else if (ebook_action.type == 'cancel_hold') {
+                var button = dojo.create("input", { id: "hold-button", type: "button", value: l_strings.cancel_hold }, dojo.byId('hold-button-td'));
+                ebook.conns.checkout = dojo.connect(button, 'onclick', "doCancelHold");
+            }
+            dojo.removeClass('ebook_holds_main', "hidden");
+        });
+    }
+}
+
+function cleanupAfterAction() {
+    // unset variables related to the transaction we have performed,
+    // to avoid any weirdness on page reload
+    ebook_action = {};
+    // update page to account for successful checkout
+    addTotalsToPage();
+    addTransactionsToPage();
+    // clear transaction cache to force a refresh on next page load
+    dojo.cookie('ebook_xact_cache', '', {path: '/', expires: '-1h'});
+}
+
 // check out our active ebook
 function doCheckout() {
     active_ebook.checkout(authtoken, patron_id, function(resp) {
@@ -212,15 +256,7 @@ function doCheckout() {
                 download_url: '' // TODO - for OverDrive, user must "lock in" a format first!
             };
             xacts.checkouts.unshift(new_xact);
-            // unset variables related to the transaction we have performed,
-            // to avoid any weirdness on page reload
-            ebook_action = {};
-            active_ebook = undefined;
-            // update page to account for successful checkout
-            addTotalsToPage();
-            addTransactionsToPage();
-            // clear transaction cache to force a refresh on next page load
-            dojo.cookie('ebook_xact_cache', '', {path: '/', expires: '-1h'});
+            cleanupAfterAction();
         } else {
             console.log('Checkout failed: ' + resp.error_msg);
             dojo.removeClass('ebook_checkout_failed', "hidden");
@@ -231,6 +267,34 @@ function doCheckout() {
     });
 }
 
+// place hold on our active ebook
+function doPlaceHold() {
+    active_ebook.placeHold(authtoken, patron_id, function(resp) {
+        if (resp.error_msg) {
+            console.log('Place hold failed: ' . resp.error_msg);
+            dojo.removeClass('ebook_place_hold_failed', "hidden");
+        } else {
+            console.log('Place hold succeeded!');
+            dojo.destroy('hold-button');
+            dojo.removeClass('ebook_place_hold_succeeded', "hidden");
+            var new_hold = {
+                title_id: active_ebook.id,
+                title: active_ebook.title,
+                author: active_ebook.author,
+                queue_position: resp.queue_position,
+                queue_size: resp.queue_size,
+                expire_date: resp.expire_date
+            };
+            if ( resp.is_ready || (resp.queue_position === 1 && resp.queue_size === 1) ) {
+                xacts.holds_ready.unshift(new_hold);
+            } else {
+                xacts.holds_pending.unshift(new_hold);
+            }
+            cleanupAfterAction();
+        }
+    });
+}
+
 // deserialize transactions from cache, returning them as a JS object
 function getCachedTransactions() {
     console.log('retrieving cached transaction details');