@internetarchive/ia-book-actions 0.0.29-alpha34 → 0.0.29-alpha36

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
@@ -129,10 +129,10 @@
129
129
 
130
130
  let isDevBox = true;
131
131
  iaBookActions.loanRenewConfig = {
132
- totalTime: isDevBox ? 120 : 3600, // 60 second = 1 min
133
- autoCheckAt: isDevBox ? 115 : 3540, // at last 10th minute
132
+ totalTime: isDevBox ? 20 : 3600, // 60 second = 1 min
133
+ autoCheckAt: isDevBox ? 15 : 3540, // at last 10th minute
134
134
  pageChangedInLast: isDevBox ? 80 : 900, // within last 15 second/minute
135
- env: isDevBox ? 'dev' : 'prod',
135
+ isDevBox: isDevBox,
136
136
  };
137
137
 
138
138
  let lendingStatus1 = iaBookActions.lendingStatus;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@internetarchive/ia-book-actions",
3
- "version": "0.0.29-alpha34",
3
+ "version": "0.0.29-alpha36",
4
4
  "description": "Bookreader actions for borrowable book",
5
5
  "author": "Internet Archive",
6
6
  "license": "AGPL-3.0-only",
@@ -20,7 +20,7 @@
20
20
  "dependencies": {
21
21
  "@internetarchive/icon-info": "^1.2.6",
22
22
  "@internetarchive/local-cache": "^0.2.0",
23
- "@internetarchive/modal-manager": "^0.2.1",
23
+ "@internetarchive/modal-manager": "^0.2.6",
24
24
  "@internetarchive/shared-resize-observer": "^0.2.0",
25
25
  "@internetarchive/toast-manager": "0.0.1-alpha9",
26
26
  "lit": "^2.5.0"
@@ -32,19 +32,20 @@ export default class TimerCountdown extends LitElement {
32
32
  // execute interval in each second in-case of dev environment
33
33
  this.timerInterval = setInterval(
34
34
  () => {
35
- this.timeLeftOnLoan -= this.loanRenewConfig.env === 'dev' ? 1 : 60;
35
+ this.timeLeftOnLoan -= this.loanRenewConfig.isDevBox ? 1 : 60;
36
+ const timeLeft = Math.round(this.timeLeftOnLoan);
36
37
 
37
38
  // execute from last 10th minute to 0th minute
38
39
  // - 10th - to check if user has viewed
39
40
  // - till 0th - to show warning msg with remaining time to auto returned
40
41
  if (
41
- Math.round(this.timeLeftOnLoan) <= this.loanRenewConfig.autoCheckAt
42
+ timeLeft <= this.loanRenewConfig.autoCheckAt
42
43
  ) {
43
44
  this.dispatchEvent(
44
45
  new CustomEvent('IABookActions:loanRenew', {
45
46
  detail: {
46
47
  hasPageChanged: false,
47
- timeLeft: Math.round(this.timeLeftOnLoan),
48
+ timeLeft: timeLeft,
48
49
  },
49
50
  bubbles: true,
50
51
  composed: true,
@@ -52,12 +53,13 @@ export default class TimerCountdown extends LitElement {
52
53
  );
53
54
  }
54
55
 
55
- // clear interval if timer is < this.loanRenewConfig.autoCheckAt
56
- if (this.timeLeftOnLoan < this.loanRenewConfig.autoCheckAt) {
57
- clearInterval(this.timerInterval);
56
+ // clear interval
57
+ if (timeLeft <= 1) {
58
+ // clearInterval(this.timerInterval);
59
+ console.log('cleared timer interval');
58
60
  }
59
61
  },
60
- this.loanRenewConfig.env === 'dev' ? 1000 : 60000
62
+ this.loanRenewConfig.isDevBox ? 1000 : 60000
61
63
  );
62
64
  }
63
65
 
@@ -103,12 +103,12 @@ export default class ActionsHandler extends LitElement {
103
103
  handleBrowseIt() {
104
104
  const action = 'browse_book';
105
105
  this.dispatchToggleActionGroup();
106
- this.setBrowseTimeSession();
107
106
 
108
107
  ActionsHandlerService({
109
108
  action,
110
109
  identifier: this.identifier,
111
110
  success: () => {
111
+ this.setBrowseTimeSession();
112
112
  this.handleReadItNow();
113
113
  },
114
114
  error: data => {
@@ -26,8 +26,9 @@ export class LoanRenewHelper {
26
26
  try {
27
27
  if (this.hasPageChanged) {
28
28
  return this.pageChanged(); // user clicked on page
29
+ } else {
30
+ return this.autoChecker(); // auto checker at last 10th minute
29
31
  }
30
- return this.autoChecker(); // auto checker at 50th minute
31
32
  } catch (error) {
32
33
  console.log(error);
33
34
  }
@@ -57,7 +58,7 @@ export class LoanRenewHelper {
57
58
  );
58
59
 
59
60
  // if user viewed new page in last 10 minutes, renew immediately
60
- if (currentTime >= lastTimeFrame) {
61
+ if (lastTimeFrame !== null && currentTime >= lastTimeFrame) {
61
62
  this.result.texts = this.loanRenewMessage;
62
63
  this.result.renewNow = true;
63
64
  console.log('manually renewed by page click!');
@@ -68,33 +69,31 @@ export class LoanRenewHelper {
68
69
  }
69
70
 
70
71
  /**
71
- * Trigger this function when countdown hits last 10 minutes
72
- * - if user is open new page, just renew the loan at last 10th minute
72
+ * Trigger this function when countdown hits at last 10th minute
73
+ * - if user is active, just renew the loan at last 10th minute
73
74
  *
74
75
  * @returns {this.result}
75
76
  */
76
77
  async autoChecker() {
77
- const currentTime = new Date();
78
78
  const { pageChangedInLast } = this.loanRenewConfig;
79
- const lastPageFlipTime = await this.localCache.get(
79
+ const pageChangedTime = await this.localCache.get(
80
80
  `${this.identifier}-pageChangedTime`
81
81
  );
82
82
 
83
- // last 15 min if used flipped a page.
84
- const lastFlipTimeFrame = this.changeTime(
85
- currentTime,
83
+ // in last 15 min if user make any activity.
84
+ const pageChangeTimeFrame = this.changeTime(
85
+ new Date(),
86
86
  pageChangedInLast,
87
87
  'sub'
88
88
  ); // 15 seconds
89
89
 
90
90
  if (
91
- lastPageFlipTime === undefined ||
92
- lastPageFlipTime <= lastFlipTimeFrame
91
+ pageChangedTime === undefined ||
92
+ pageChangedTime <= pageChangeTimeFrame
93
93
  ) {
94
94
  this.result.texts = this.loanReturnWarning;
95
95
  this.result.renewNow = false; // not viewed
96
- // console.log('not viewed!');
97
- } else if (lastPageFlipTime >= lastFlipTimeFrame) {
96
+ } else if (pageChangedTime >= pageChangeTimeFrame) {
98
97
  this.result.texts = '';
99
98
  this.result.renewNow = true; // viewed in last time frame
100
99
  console.log('silently renewed!');
@@ -253,7 +253,7 @@ export default class IABookActions extends LitElement {
253
253
  */
254
254
  document.addEventListener('click', () => {
255
255
  if (this.loanRenewHelper && this.loanRenewResult.timeLeft > 0) {
256
- console.log('1');
256
+ console.log('clicked-outside');
257
257
  this.suppressToast = true;
258
258
  }
259
259
  });
@@ -283,7 +283,6 @@ export default class IABookActions extends LitElement {
283
283
  );
284
284
 
285
285
  await this.loanRenewHelper.handleLoanRenew();
286
- // console.log(this.loanRenewResult.timeLeft);
287
286
  this.loanRenewResult = this.loanRenewHelper.result;
288
287
  }
289
288
 
@@ -350,6 +349,8 @@ export default class IABookActions extends LitElement {
350
349
  * Execute when loan is expired
351
350
  */
352
351
  async browseHasExpired() {
352
+ clearTimeout(this.browserTimer);
353
+
353
354
  const currStatus = { ...this.lendingStatus, browsingExpired: true };
354
355
  this.lendingStatus = currStatus;
355
356
 
@@ -461,7 +462,6 @@ export default class IABookActions extends LitElement {
461
462
  await this.showToastMessage();
462
463
  await this.browseHasRenew();
463
464
  await this.resetTimerCountState();
464
- this.startLoanTokenPoller();
465
465
  }
466
466
  }
467
467
 
@@ -527,6 +527,20 @@ export default class IABookActions extends LitElement {
527
527
 
528
528
  if (errorMsg) this.showErrorModal(errorMsg, action);
529
529
 
530
+ // if error related to loan token
531
+ // - clear tokenPoller interval
532
+ // - set user_has_browsed to `false`
533
+ if (action === 'create_token') {
534
+ this.tokenPoller.disconnectedInterval();
535
+
536
+ const currStatus = {
537
+ ...this.lendingStatus,
538
+ user_has_browsed: false,
539
+ available_to_browse: true,
540
+ };
541
+ this.lendingStatus = currStatus;
542
+ }
543
+
530
544
  // update action bar state if book is not available to browse or borrow.
531
545
  if (errorMsg && errorMsg.match(/not available to borrow/gm)) {
532
546
  let currStatus = this.lendingStatus;
@@ -582,7 +596,8 @@ export default class IABookActions extends LitElement {
582
596
  href="mailto:info@archive.org?subject=Help: cannot access my borrowed book: ${this
583
597
  .identifier}"
584
598
  >info@archive.org</a
585
- >`;
599
+ ><br /><br />
600
+ errorLog: ${errorMsg}`;
586
601
  }
587
602
 
588
603
  this.modal.showModal({