LP#1673870: Add ebook API checkout functionality to OPAC
authorJeff Davis <jdavis@sitka.bclibraries.ca>
Fri, 17 Mar 2017 22:22:32 +0000 (15:22 -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_circs.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 5a53976..7af1a8d 100644 (file)
@@ -2,7 +2,13 @@
     PROCESS "opac/parts/misc_util.tt2";
     PROCESS "opac/parts/myopac/column_sort_support.tt2";
     WRAPPER "opac/parts/myopac/base.tt2";
-    myopac_page = "ebook_circs"  %]
+    myopac_page = "ebook_circs";
+    IF CGI.param("action") == 'checkout';
+        ebook_circs_title = l('Check Out E-Item');
+    ELSE;
+        ebook_circs_title = l('E-Items Currently Checked Out');
+    END;
+%]
 <h3 class="sr-only">[% l('E-Items Currently Checked Out') %]</h3>
 <div id='myopac_checked_div'>
 
@@ -11,7 +17,7 @@
             <a href="[% mkurl('circs',{},1) %]">[% l("Current Items Checked Out") %]</a>
         </div>
         <div class="align selected">
-            <a href="#">[% l("E-Items Currently Checked Out") %]</a>
+            <a href="[% mkurl('',{},1) %]">[% l("E-Items Currently Checked Out") %]</a>
         </div>
         <div class="align">
             <a href="[% mkurl('circ_history',{},1) %]">[% l("Check Out History") %]</a>
     </div>
 
     <div class="header_middle">
-        <span class="float-left">[% l('E-Items Currently Checked Out') %]</span>
+        <span class="float-left">[% ebook_circs_title %]</span>
     </div>
     <div class="clear-both"></div>
     <div id="no_ebook_circs" class="warning_box hidden">[% l('You have no e-items checked out.') %]</div>
+    <div id="ebook_checkout_failed" class="warning_box hidden">[% l('E-item could not be checked out.') %]</div>
+    <div id="ebook_checkout_succeeded" class="success hidden">[% l('E-item successfully checked out.') %]</div>
     <div id='ebook_circs_main' class="hidden">
         <table id="ebook_circs_main_table"
-            title="[% l('E-Items Currently Checked Out') %]"
+            title="[% ebook_circs_title %]"
             class="table_no_border_space table_no_cell_pad item_list_padding">
             <thead>
             <tr>
index 538de71..7479509 100644 (file)
@@ -12,6 +12,7 @@ var l_strings = {};
 l_strings.download = '[% l('Download') %]';
 l_strings.ready_for_checkout = '[% l('Ready for Checkout') %]';
 l_strings.suspended = '[% l('Suspended') %]';
+l_strings.checkout = '[% l('Checkout') %]';
 
 // give us cookies!
 dojo.require("dojo.cookie");
@@ -39,6 +40,18 @@ dojo.forEach(vendor_list, function(v) {
     cookie_registry.push(v);
 });
 
+// essential info for performing a transaction
+var ebook_action = {};
+[%- IF CGI.param("action").defined %]
+ebook_action.type = '[% CGI.param("action") %]';
+[%- END -%]
+[%- IF CGI.param("title").defined %]
+ebook_action.title_id = '[% CGI.param("title") %]';
+[%- END -%]
+[%- IF CGI.param("vendor").defined %]
+ebook_action.vendor = '[% CGI.param("vendor") %]';
+[%- END -%]
+
 [% IF ctx.user %]
 // user- or login-specific vars
 var authtoken = '[% ctx.authtoken %]';
index f933b69..46b223c 100644 (file)
@@ -8,8 +8,31 @@ function Ebook(vendor, id) {
     this.vendor = vendor;
     this.id = id; // external ID for this title
     this.rec_id;  // bre.id for this title's MARC record
+    this.title;   // title of ebook
+    this.author;  // author of ebook
     this.avail;   // availability info for this title
     this.holdings = {}; // holdings info
+    this.conns = {}; // references to Dojo event connection for performing actions with this ebook
+
+}
+
+Ebook.prototype.getDetails = function(callback) {
+    var ses = dojo.cookie(this.vendor);
+    var ebook = this;
+    new OpenSRF.ClientSession('open-ils.ebook_api').request({
+        method: 'open-ils.ebook_api.title.details',
+        params: [ ses, ebook.id ],
+        async: true,
+        oncomplete: function(r) {
+            var resp = r.recv();
+            if (resp) {
+                console.log('title details response: ' + resp.content());
+                ebook.title = resp.content().title;
+                ebook.author = resp.content().author;
+                return callback(ebook);
+            }
+        }
+    }).send();
 }
 
 Ebook.prototype.getAvailability = function(callback) {
@@ -46,3 +69,20 @@ Ebook.prototype.getHoldings = function(callback) {
     }).send();
 }
 
+Ebook.prototype.checkout = 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.checkout',
+        params: [ authtoken, ses, ebook.id, patron_id ],
+        async: true,
+        oncomplete: function(r) {
+            var resp = r.recv();
+            if (resp) {
+                console.log('checkout response: ' + resp.content());
+                return callback(resp.content());
+            }
+        }
+    }).send();
+}
+
index 0ba1827..fb79a4c 100644 (file)
@@ -21,6 +21,12 @@ var xacts = {
     holds_ready: []
 };
 
+// Ebook to perform actions on.
+var active_ebook;
+if (typeof ebook_action.title_id !== 'undefined') {
+    active_ebook = new Ebook(ebook_action.vendor, ebook_action.title_id);
+}
+
 dojo.addOnLoad(function() {
 
     dojo.forEach(vendor_list, function(v) {
@@ -98,7 +104,11 @@ function updateMyAccountSummary() {
 }
 
 function updateCheckoutView() {
-    if (xacts.checkouts.length < 1) {
+    if (typeof ebook_action.type !== 'undefined') {
+        if (ebook_action.type == 'checkout') {
+            getReadyForCheckout();
+        }
+    } else if (xacts.checkouts.length < 1) {
         dojo.removeClass('no_ebook_circs', "hidden");
     } else {
         dojo.forEach(xacts.checkouts, function(x) {
@@ -166,6 +176,61 @@ function updateHoldReadyView() {
     }
 }
 
+// set up page for user to perform a checkout
+function getReadyForCheckout() {
+    if (typeof active_ebook === 'undefined') {
+        console.log('No active ebook specified, cannot prepare for checkout');
+        dojo.removeClass('ebook_checkout_failed', "hidden");
+    } else {
+        active_ebook.getDetails( function(ebook) {
+            dojo.empty('ebook_circs_main_table_body');
+            var tr = dojo.create("tr", null, dojo.byId('ebook_circs_main_table_body'));
+            dojo.create("td", { innerHTML: ebook.title }, tr);
+            dojo.create("td", { innerHTML: ebook.author }, tr);
+            dojo.create("td", null, tr);
+            dojo.create("td", { id: "checkout-button-td" }, tr);
+            var button = dojo.create("input", { id: "checkout-button", type: "button", value: l_strings.checkout }, dojo.byId('checkout-button-td'));
+            ebook.conns.checkout = dojo.connect(button, 'onclick', "doCheckout");
+            dojo.removeClass('ebook_circs_main', "hidden");
+        });
+    }
+}
+
+// check out our active ebook
+function doCheckout() {
+    active_ebook.checkout(authtoken, patron_id, function(resp) {
+        if (resp.due_date) {
+            console.log('Checkout succeeded!');
+            dojo.destroy('checkout-button');
+            dojo.removeClass('ebook_checkout_succeeded', "hidden");
+            // add our successful checkout to top of transaction cache
+            var new_xact = {
+                title_id: active_ebook.id,
+                title: active_ebook.title,
+                author: active_ebook.author,
+                due_date: resp.due_date,
+                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'});
+        } else {
+            console.log('Checkout failed: ' + resp.error_msg);
+            dojo.removeClass('ebook_checkout_failed', "hidden");
+        }
+        // When we switch to jQuery, we can use .one() instead of .on(),
+        // obviating the need for an explicit disconnect here.
+        dojo.disconnect(active_ebook.conns.checkout);
+    });
+}
+
 // deserialize transactions from cache, returning them as a JS object
 function getCachedTransactions() {
     console.log('retrieving cached transaction details');