@internetarchive/ia-book-actions 0.1.3-alpha1 → 0.1.4-alpha1

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
@@ -135,7 +135,7 @@
135
135
  let loanTotalTime5Min = { loanTotalTime: 300, loanRenewAtLast: 180, pageChangedInLast: 15 };
136
136
  let loanTotalTime1Hour = { loanTotalTime: 3600, loanRenewAtLast: 600, pageChangedInLast: 900 };
137
137
 
138
- iaBookActions.loanRenewTimeConfig = loanTotalTime1Hour;
138
+ iaBookActions.loanRenewTimeConfig = loanTotalTime2Min;
139
139
 
140
140
  let lendingStatus1 = iaBookActions.lendingStatus;
141
141
  let secondsLeftOnLoan = iaBookActions.loanRenewTimeConfig.loanTotalTime;
@@ -150,6 +150,23 @@
150
150
  });
151
151
  });
152
152
 
153
+ window.addEventListener('BookReader:userAction', () => {
154
+ if (this.borrowType === 'browsed') {
155
+ this.suppressToast = false;
156
+ this.autoLoanRenewChecker(true);
157
+ }
158
+
159
+ return nothing;
160
+ });
161
+
162
+ document.querySelector('.brControlClick').addEventListener('click', () => {
163
+ window.dispatchEvent(new CustomEvent('BookReader:userAction', {
164
+ bubbles: true,
165
+ composed: true,
166
+ detail: { props },
167
+ }));
168
+ });
169
+
153
170
  document.querySelectorAll('.errorEnable input[type=checkbox]').forEach((item) => {
154
171
  if (window.location.href.indexOf('?error=true') !== -1) {
155
172
  item.checked = true;
@@ -311,6 +328,9 @@
311
328
  <td>
312
329
  <button class="pageChangedEvent">Page Change Event</button>
313
330
  </td>
331
+ <td>
332
+ <button class="brControlClick">Br Controls</button>
333
+ </td>
314
334
  </tr>
315
335
  </table>
316
336
  </fieldset>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@internetarchive/ia-book-actions",
3
- "version": "0.1.3-alpha1",
3
+ "version": "0.1.4-alpha1",
4
4
  "description": "Bookreader actions for borrowable book",
5
5
  "author": "Internet Archive",
6
6
  "license": "AGPL-3.0-only",
@@ -36,7 +36,7 @@ export default css`
36
36
  align-items: center;
37
37
  transition: all 0.1s ease 0s;
38
38
  vertical-align: middle;
39
- padding: 0 1rem;
39
+ padding: 0 3rem;
40
40
  outline-color: ${whiteColor};
41
41
  outline-offset: -4px;
42
42
  user-select: none;
@@ -31,12 +31,6 @@ export default class TimerCountdown extends LitElement {
31
31
  */
32
32
  this.loanRenewAtLast = 0;
33
33
 
34
- /**
35
- * setInterval executed interval
36
- * @type {number}
37
- */
38
- this.timerInterval = undefined;
39
-
40
34
  /**
41
35
  * delay seconds in setInterval function
42
36
  * @type {number}
@@ -45,30 +39,37 @@ export default class TimerCountdown extends LitElement {
45
39
  }
46
40
 
47
41
  disconnectedCallback() {
48
- clearInterval(this.timerInterval);
42
+ window?.IALendingIntervals?.clearTimerCountdown();
49
43
  }
50
44
 
51
45
  updated(changed) {
52
46
  if (changed.has('secondsLeftOnLoan') && this.secondsLeftOnLoan > 0) {
53
- clearInterval(this.timerInterval);
47
+ this.disconnectedCallback();
48
+
54
49
  this.timerCountdown();
55
50
  }
56
51
  }
57
52
 
58
53
  timerCountdown() {
59
54
  // - let just execute setInterval in every 1 minute
60
- this.timerExecutionSeconds = 60;
55
+ this.timerExecutionSeconds = 1;
61
56
 
62
- this.timerInterval = setInterval(() => {
57
+ /**
58
+ * set interval in window object
59
+ * @see ia-lending-intervals.js
60
+ */
61
+ window.IALendingIntervals.timerCountdown = setInterval(() => {
63
62
  this.secondsLeftOnLoan -= this.timerExecutionSeconds;
64
63
  const secondsLeft = Math.round(this.secondsLeftOnLoan);
65
64
 
66
65
  console.log(
66
+ new Date().toLocaleTimeString(),
67
67
  'timeLeftInSec',
68
68
  secondsLeft,
69
69
  ' ||| timeLeftInMin',
70
70
  Math.ceil(secondsLeft / 60)
71
71
  );
72
+
72
73
  /**
73
74
  * execute from last 10th minute to 0th minute
74
75
  * - 10th - to check if user has viewed
@@ -90,7 +91,7 @@ export default class TimerCountdown extends LitElement {
90
91
 
91
92
  // clear interval
92
93
  if (secondsLeft <= 60) {
93
- clearInterval(this.timerInterval);
94
+ this.disconnectedCallback();
94
95
  window?.Sentry?.captureMessage(sentryLogs.clearOneHourTimer);
95
96
  }
96
97
  }, this.timerExecutionSeconds * 1000);
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Collection of methods for managing intervals for lending system
3
+ *
4
+ * There are mainly two intervals being used
5
+ * - tokenPoller :- used to create_token for borrowed book
6
+ * - timerCountdown :- reponsible for loan-auto-renew and auto returned warning message
7
+ *
8
+ * @see TimerCountdown::timerCountdown function
9
+ * @see LoanTokenPoller::bookAccessed function
10
+ *
11
+ * @type {Object}
12
+ */
13
+ window.IALendingIntervals = {
14
+ /**
15
+ * clear interval for create_token api
16
+ */
17
+ clearTokenPoller: () => {
18
+ window.clearInterval(window?.IALendingIntervals?.tokenPoller);
19
+ },
20
+
21
+ /**
22
+ * clear interval for graphic timer for one-hour loan renew
23
+ */
24
+ clearTimerCountdown: () => {
25
+ window.clearInterval(window.IALendingIntervals.timerCountdown);
26
+ },
27
+
28
+ /**
29
+ * clear all intervals being used acros lending system
30
+ */
31
+ clearAll: () => {
32
+ window.clearInterval(window?.IALendingIntervals?.tokenPoller);
33
+ window.clearInterval(window?.IALendingIntervals?.timerCountdown);
34
+ },
35
+ };
@@ -166,7 +166,7 @@ export default class ActionsConfig {
166
166
 
167
167
  return {
168
168
  id: 'purchaseBook',
169
- text: 'Purchase at',
169
+ text: 'Purchase at ',
170
170
  subText: 'Better World Books',
171
171
  title: 'Purchase',
172
172
  url: this.bwbPurchaseUrl,
@@ -126,8 +126,8 @@ export class LoanRenewHelper {
126
126
  // convert time from second to minute
127
127
  timeLeft = Math.ceil(timeLeft / 60);
128
128
 
129
- // convert minute to hour
130
- if (timeLeft === 60) {
129
+ // convert time from minute to hour
130
+ if (timeLeft > 59) {
131
131
  timeLeft = 1; // 1 hour
132
132
  unitOfTime = 'hour';
133
133
  }
@@ -20,17 +20,8 @@ export class LoanTokenPoller {
20
20
  this.bookAccessed();
21
21
  }
22
22
 
23
- disconnectedInterval() {
24
- clearInterval(this.loanTokenInterval);
25
- this.loanTokenInterval = undefined;
26
-
27
- // temporary disabling sentry notice
28
- // as message is informational, not critical nor an error.
29
- // also, as of 11/28/2022, JS build maps are not working properly on petabox
30
- // and as a result, this comment is not properly grouped.
31
- // if (window.Sentry) {
32
- // window?.Sentry?.captureMessage('loan token interval cleared');
33
- // }
23
+ disconnectedCallback() {
24
+ window?.IALendingIntervals?.clearTokenPoller();
34
25
  }
35
26
 
36
27
  async bookAccessed() {
@@ -59,7 +50,11 @@ export class LoanTokenPoller {
59
50
  // - we don't want to fetch token on interval
60
51
  // - the initial token is enough to set cookies for reading book and readaloud features
61
52
  if (this.borrowType !== 'adminBorrowed') {
62
- this.loanTokenInterval = setInterval(() => {
53
+ /**
54
+ * set interval in window object
55
+ * @see ia-lending-intervals.js
56
+ */
57
+ window.IALendingIntervals.tokenPoller = setInterval(() => {
63
58
  this.handleLoanTokenPoller();
64
59
  }, this.pollerDelay * 1000);
65
60
  }
@@ -77,7 +72,7 @@ export class LoanTokenPoller {
77
72
  );
78
73
 
79
74
  // if book is not browsed, just clear token polling interval
80
- this.disconnectedInterval(); // stop token fetch api
75
+ this.disconnectedCallback();
81
76
  }
82
77
  }
83
78
 
@@ -95,7 +90,6 @@ export class LoanTokenPoller {
95
90
  identifier: this.identifier,
96
91
  action,
97
92
  error: data => {
98
- this.disconnectedInterval(); // stop token fetch api
99
93
  this.errorCallback({ detail: { action, data } });
100
94
 
101
95
  // send error to Sentry
@@ -11,6 +11,7 @@ import './components/book-title-bar.js';
11
11
  import './components/text-group.js';
12
12
  import './components/info-icon.js';
13
13
  import './components/timer-countdown.js';
14
+ import './core/config/ia-lending-intervals.js';
14
15
 
15
16
  import { GetLendingActions } from './core/services/get-lending-actions.js';
16
17
  import { mobileContainerWidth } from './core/config/constants.js';
@@ -102,7 +103,9 @@ export default class IABookActions extends LitElement {
102
103
  }
103
104
 
104
105
  disconnectedCallback() {
105
- this.tokenPoller?.disconnectedInterval();
106
+ // clear all intervals for lending system
107
+ window?.IALendingIntervals?.clearAll();
108
+
106
109
  window?.Sentry?.captureMessage(sentryLogs.disconnectedCallback);
107
110
  this.disconnectResizeObserver();
108
111
  }
@@ -197,7 +200,8 @@ export default class IABookActions extends LitElement {
197
200
  if (!this.tokenPoller) {
198
201
  window?.Sentry?.captureMessage(sentryLogs.bookWasExpired);
199
202
  }
200
- this.tokenPoller?.disconnectedInterval();
203
+ window?.IALendingIntervals?.clearAll();
204
+
201
205
  /** Global event - always fire */
202
206
  this.dispatchEvent(
203
207
  new Event(events.browseExpired, {
@@ -267,7 +271,7 @@ export default class IABookActions extends LitElement {
267
271
  /**
268
272
  * detect click-event on document to close toast-template
269
273
  */
270
- document.addEventListener('click', e => {
274
+ window.addEventListener('click', e => {
271
275
  if (this.loanRenewHelper && this.loanRenewResult.secondsLeft > 0) {
272
276
  this.suppressToast = true;
273
277
  }
@@ -389,7 +393,7 @@ export default class IABookActions extends LitElement {
389
393
  * Execute when loan is expired
390
394
  */
391
395
  async browseHasExpired() {
392
- this.tokenPoller?.disconnectedInterval();
396
+ window?.IALendingIntervals?.clearAll();
393
397
 
394
398
  const currStatus = { ...this.lendingStatus, browsingExpired: true };
395
399
  this.lendingStatus = currStatus;
@@ -499,7 +503,7 @@ export default class IABookActions extends LitElement {
499
503
  */
500
504
  async handleLoanAutoRenewed() {
501
505
  if (this.loanRenewResult.renewNow) {
502
- this.tokenPoller?.disconnectedInterval();
506
+ window?.IALendingIntervals?.clearAll();
503
507
 
504
508
  this.suppressToast = false;
505
509
  await this.browseHasRenew();
@@ -523,10 +527,6 @@ export default class IABookActions extends LitElement {
523
527
  * @see LoanTokenPoller
524
528
  */
525
529
  startLoanTokenPoller() {
526
- if (this.tokenPoller) {
527
- window?.Sentry?.captureMessage(sentryLogs.clearTokenPoller);
528
- this.tokenPoller?.disconnectedInterval();
529
- }
530
530
  const successCallback = () => {
531
531
  this.lendingBarPostInit();
532
532
  };
@@ -576,6 +576,9 @@ export default class IABookActions extends LitElement {
576
576
  // toggle activity loader
577
577
  this.handleToggleActionGroup();
578
578
 
579
+ // clear all intervals for lending system when error occured
580
+ window?.IALendingIntervals?.clearAll();
581
+
579
582
  const action = event?.detail?.action;
580
583
  const errorMsg = event?.detail?.data?.error;
581
584
 
@@ -585,8 +588,6 @@ export default class IABookActions extends LitElement {
585
588
  // - clear tokenPoller interval
586
589
  // - set user_has_browsed to `false`
587
590
  if (action === 'create_token') {
588
- this.tokenPoller?.disconnectedInterval();
589
-
590
591
  const currStatus = {
591
592
  ...this.lendingStatus,
592
593
  user_has_browsed: false,