@internetarchive/ia-book-actions 0.1.6 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/demo/index.html CHANGED
@@ -131,9 +131,9 @@
131
131
  iaBookActions.returnUrl = '';
132
132
  iaBookActions.localCache = localCache;
133
133
 
134
- let loanTotalTime2Min = { loanTotalTime: 10, loanRenewAtLast: 5, pageChangedInLast: 5 };
134
+ let loanTotalTime2Min = { loanTotalTime: 65, loanRenewAtLast: 60, pageChangedInLast: 15 };
135
135
  let loanTotalTime5Min = { loanTotalTime: 300, loanRenewAtLast: 180, pageChangedInLast: 15 };
136
- let loanTotalTime1Hour = { loanTotalTime: 3600, loanRenewAtLast: 600, pageChangedInLast: 900 };
136
+ let loanTotalTime1Hour = { loanTotalTime: 3600, loanRenewAtLast: 660, pageChangedInLast: 900 };
137
137
 
138
138
  iaBookActions.loanRenewTimeConfig = loanTotalTime2Min;
139
139
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@internetarchive/ia-book-actions",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "Bookreader actions for borrowable book",
5
5
  "author": "Internet Archive",
6
6
  "license": "AGPL-3.0-only",
@@ -9,10 +9,6 @@ import CollapsibleActionGroupStyle from '../assets/styles/collapsible-action-gro
9
9
 
10
10
  import { tabletContainerWidth } from '../core/config/constants.js';
11
11
  import { purchaseIcon } from '../assets/data/purchase.js';
12
- import {
13
- analyticsCategories,
14
- analyticsActions,
15
- } from '../core/config/analytics-event-and-category.js';
16
12
  import {
17
13
  dropdownOpened,
18
14
  dropdownClosed,
@@ -76,27 +72,14 @@ export class CollapsibleActionGroup extends ActionsHandler {
76
72
 
77
73
  /**
78
74
  * dispatch event when book is auto renewed / returned
79
- * listen these events in action-handler.js
75
+ * listen these events in action-handler.js to execute ajax call on petabox.
80
76
  * @see ActionsHandler
81
77
  *
82
78
  * @param {string} event - autoRenew|autoReturn
83
79
  * @memberof CollapsibleActionGroup
84
80
  */
85
81
  dispatchLoanEvent(event) {
86
- const category = analyticsCategories.browse;
87
- const action =
88
- event === 'autoRenew'
89
- ? analyticsActions.browseAutoRenew
90
- : analyticsActions.browseAutoReturn;
91
-
92
- // listen in action-handler.js to execute ajax call on petabox.
93
- this.dispatchEvent(
94
- new CustomEvent(event, {
95
- detail: {
96
- event: { category, action },
97
- },
98
- })
99
- );
82
+ this.dispatchEvent(new Event(event));
100
83
  }
101
84
 
102
85
  /**
@@ -74,7 +74,7 @@ export default class TimerCountdown extends LitElement {
74
74
  secondsLeft -= this.timerExecutionSeconds;
75
75
  secondsLeft = Math.round(secondsLeft); // round number
76
76
 
77
- // re-sync timer is gone off
77
+ // re-sync timer if gone off because of background window
78
78
  await this.reSyncTimerIfGoneOff(secondsLeft);
79
79
 
80
80
  /**
@@ -1,2 +1,9 @@
1
- export const mobileContainerWidth = 550;
2
- export const tabletContainerWidth = 767;
1
+ /**
2
+ * hide the primary texts below this resolution
3
+ */
4
+ export const mobileContainerWidth = 700;
5
+
6
+ /**
7
+ * shift the actions buttons in dropdown
8
+ */
9
+ export const tabletContainerWidth = 895;
@@ -5,6 +5,7 @@ export const sentryLogs = {
5
5
  disconnectedCallback: 'IABookActions:disconnectedCallback',
6
6
  bookHasRenewed:
7
7
  'IABookActions:handleLoanAutoRenewed - book has renewed for next one hour',
8
+ bookRenewFailed: 'IABookActions:handleLoanRenewNow - failed to renew',
8
9
  browseHasExpired:
9
10
  'IABookActions:browseHasExpired - one-hour loan has been expired',
10
11
  bookWasExpired:
@@ -47,6 +47,12 @@ export default async function ActionsHandlerService(options) {
47
47
 
48
48
  // return success response for /demo/ server...
49
49
  if (baseHost == '/demo/1' || baseHost == '/demo/') {
50
+ if (option?.action == 'renew_loan') {
51
+ return {
52
+ success: true,
53
+ loan: { renewal: true },
54
+ };
55
+ }
50
56
  return {
51
57
  success: true,
52
58
  message: 'operation executed successfully!',
@@ -1,6 +1,7 @@
1
1
  import { LitElement } from 'lit';
2
2
 
3
3
  import { URLHelper } from '../../config/url-helper.js';
4
+ import { sentryLogs } from '../../config/sentry-events.js';
4
5
 
5
6
  import ActionsHandlerService from './actions-handler-service.js';
6
7
  import LoanAnanlytics from '../loan-analytics.js';
@@ -53,7 +54,7 @@ export default class ActionsHandler extends LitElement {
53
54
  });
54
55
 
55
56
  this.addEventListener('autoReturn', () => {
56
- this.deleteLoanCookies();
57
+ this.handleReturnIt();
57
58
  this.loanAnanlytics?.storeLoanStatsCount(this.identifier, 'autoreturn');
58
59
  });
59
60
 
@@ -63,7 +64,7 @@ export default class ActionsHandler extends LitElement {
63
64
  this.loanAnanlytics?.storeLoanStatsCount(this.identifier, 'return');
64
65
  }
65
66
 
66
- this.handleReturnIt();
67
+ this.handleReturnIt('returnNow');
67
68
 
68
69
  // send these events if 14-day borrow return
69
70
  if (detail?.borrowType === 'borrow') {
@@ -141,12 +142,18 @@ export default class ActionsHandler extends LitElement {
141
142
  action,
142
143
  identifier: this.identifier,
143
144
  success: data => {
144
- this.setBrowseTimeSession();
145
- this.dispatchEvent(
146
- new CustomEvent('loanAutoRenewed', {
147
- detail: { action, data },
148
- })
149
- );
145
+ if (data?.success === true && data?.loan?.renewal === true) {
146
+ this.setBrowseTimeSession();
147
+ this.dispatchEvent(
148
+ new CustomEvent('loanAutoRenewed', {
149
+ detail: { action, data },
150
+ })
151
+ );
152
+ } else {
153
+ window?.Sentry?.captureMessage(
154
+ `${sentryLogs.bookRenewFailed} - Error: ${JSON.stringify(data)}`
155
+ );
156
+ }
150
157
  },
151
158
  error: data => {
152
159
  this.dispatchActionError(action, data);
@@ -154,16 +161,23 @@ export default class ActionsHandler extends LitElement {
154
161
  });
155
162
  }
156
163
 
157
- handleReturnIt() {
164
+ /**
165
+ * excute function when loan is returning
166
+ *
167
+ * @param {string} type - loan return type returnNow|''
168
+ */
169
+ handleReturnIt(type = '') {
158
170
  const action = 'return_loan';
159
- this.dispatchToggleActionGroup();
171
+
172
+ if (type === 'returnNow') this.dispatchToggleActionGroup();
160
173
 
161
174
  ActionsHandlerService({
162
175
  action,
163
176
  identifier: this.identifier,
164
177
  success: () => {
165
178
  this.deleteLoanCookies();
166
- URLHelper.goToUrl(this.returnUrl, true);
179
+
180
+ if (type === 'returnNow') URLHelper.goToUrl(this.returnUrl, true);
167
181
  },
168
182
  error: data => {
169
183
  this.dispatchActionError(action, data);
@@ -40,7 +40,7 @@ import ActionsConfig from './actions-config.js';
40
40
  * @enum {string}
41
41
  */
42
42
  export const bookTitles = {
43
- available_1hr: '',
43
+ available_1hr: 'Renews each hour as long as pages are turned.',
44
44
  available_14d: 'This book can be borrowed for 14 days.',
45
45
  available_pd: 'Book available to patrons with print disabilities.',
46
46
  available_waitlist: 'A waitlist is available.',
@@ -98,7 +98,8 @@ export class GetLendingActions {
98
98
  let isBrowsing =
99
99
  lendingStatus.user_has_browsed && !lendingStatus.browsingExpired;
100
100
  if (isBrowsing) {
101
- primaryTitleText = '';
101
+ primaryTitleText =
102
+ 'Returns automatically after 25 minutes of inactivity.';
102
103
  } else {
103
104
  primaryTitleText = `Your loan of this book has ${lendingStatus.daysLeftOnLoan} days left.`;
104
105
  }
@@ -301,16 +301,28 @@ export default class IABookActions extends LitElement {
301
301
  console.log('** IABookActions:loanRenew', event.detail);
302
302
  await this.autoLoanRenewChecker(false);
303
303
 
304
- // renew in last seconds (let say 50 second) not possible because,
305
- // - very short to renew on datanodes by hitting petabox API
306
- // - loading images by create loan token
307
- if (event?.detail?.secondsLeft < 50) {
304
+ /**
305
+ * auto-renew is not possible in last seconds (let say 50 second) because,
306
+ * 1. less time to execute ajax call
307
+ * 2. less time to write loan on datanodes
308
+ * 3. less time to load images by create_token api
309
+ * so if seconds left is < 50, just expire the loan
310
+ */
311
+ let secondsLeft = event?.detail?.secondsLeft;
312
+ if (secondsLeft < 50) {
308
313
  this.suppressAutoRenew = true;
314
+ this.browseHasExpired();
309
315
  }
310
316
 
311
317
  // show warning message with remaining time to auto returned it.
312
318
  if (this.loanRenewResult.renewNow === false) {
313
- this.loanRenewResult.secondsLeft = event.detail.secondsLeft;
319
+ /**
320
+ * so compensate for the 50 second buffer to handle above race conditions
321
+ * let's reduce 1 min from warning texts and early return the book when 1 min left.
322
+ */
323
+ secondsLeft -= 60;
324
+
325
+ this.loanRenewResult.secondsLeft = secondsLeft;
314
326
  this.showToastMessage();
315
327
  }
316
328
  });
@@ -530,7 +542,6 @@ export default class IABookActions extends LitElement {
530
542
  * @param {object} event
531
543
  */
532
544
  async handleLoanAutoRenewed(event) {
533
- // TODO: just read DATA variable here to apply security { action, data }
534
545
  if (this.loanRenewResult.renewNow) {
535
546
  window?.IALendingIntervals?.clearAll();
536
547
 
@@ -539,7 +550,7 @@ export default class IABookActions extends LitElement {
539
550
  await this.showToastMessage();
540
551
  await this.resetTimerCountState();
541
552
 
542
- window?.Sentry?.captureMessage(`${sentryLogs.bookHasRenewed}`);
553
+ window?.Sentry?.captureMessage(sentryLogs.bookHasRenewed);
543
554
 
544
555
  // testing console....
545
556
  console.log('IABookActions: AutoRenewed:- ', {