@internetarchive/ia-book-actions 0.0.29-alpha23 → 0.0.29-alpha24

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,7 +129,8 @@
129
129
  iaBookActions.loanRenewConfig = {
130
130
  totalTime: 20, // 60 second = 1 min
131
131
  autoCheckAt: 15, // totalTime - 10th minute
132
- pageChangedInLast: 15, // within last 15 second/minute
132
+ pageChangedInLast: 15, // within last 15 second/minute
133
+ env: 'dev', // dev/prod
133
134
  };
134
135
 
135
136
  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-alpha23",
3
+ "version": "0.0.29-alpha24",
4
4
  "description": "Bookreader actions for borrowable book",
5
5
  "author": "Internet Archive",
6
6
  "license": "AGPL-3.0-only",
@@ -3,14 +3,14 @@ import { html, css, LitElement } from 'lit';
3
3
  export default class TimerCountdown extends LitElement {
4
4
  static get properties() {
5
5
  return {
6
- time: { type: Number },
6
+ timeLeftOnLoan: { type: Number },
7
7
  autoCheckAt: { type: Number },
8
8
  };
9
9
  }
10
10
 
11
11
  constructor() {
12
12
  super();
13
- this.time = 0;
13
+ this.timeLeftOnLoan = 0;
14
14
  this.autoCheckAt = 0;
15
15
 
16
16
  // private props
@@ -22,7 +22,7 @@ export default class TimerCountdown extends LitElement {
22
22
  }
23
23
 
24
24
  updated(changed) {
25
- if (changed.has('time') && this.time > 0) {
25
+ if (changed.has('timeLeftOnLoan') && this.timeLeftOnLoan > 0) {
26
26
  clearInterval(this.timerInterval);
27
27
  this.timerCountdown();
28
28
  }
@@ -30,17 +30,17 @@ export default class TimerCountdown extends LitElement {
30
30
 
31
31
  timerCountdown() {
32
32
  this.timerInterval = setInterval(() => {
33
- this.time -= 1;
33
+ this.timeLeftOnLoan -= 1;
34
34
 
35
35
  // execute from last 10th minute to 0th minute
36
36
  // - 10th - to check if user has viewed
37
37
  // - till 0th - to show warning msg with remaining time to auto returned
38
- if (Math.round(this.time) <= this.autoCheckAt) {
38
+ if (Math.round(this.timeLeftOnLoan) <= this.autoCheckAt) {
39
39
  this.dispatchEvent(
40
40
  new CustomEvent('IABookActions:loanRenew', {
41
41
  detail: {
42
42
  hasPageChanged: false,
43
- timeLeft: Math.round(this.time),
43
+ timeLeft: Math.round(this.timeLeftOnLoan),
44
44
  },
45
45
  bubbles: true,
46
46
  composed: true,
@@ -49,7 +49,7 @@ export default class TimerCountdown extends LitElement {
49
49
  }
50
50
 
51
51
  // clear interval if timer is < this.autoCheckAt
52
- if (this.time < this.autoCheckAt) {
52
+ if (this.timeLeftOnLoan < this.autoCheckAt) {
53
53
  clearInterval(this.timerInterval);
54
54
  }
55
55
  }, 1000);
@@ -65,7 +65,7 @@ export default class TimerCountdown extends LitElement {
65
65
  >
66
66
  <circle class="circle" cx="50" cy="50" r="50" />
67
67
  </svg>
68
- <span>${Math.round(this.time)}</span>
68
+ <span>${Math.round(this.timeLeftOnLoan)}</span>
69
69
  `;
70
70
  }
71
71
 
@@ -5,9 +5,8 @@ import { ToastConfig } from '@internetarchive/toast-manager';
5
5
  * This class is used to determine if use is eligible for auto renew loan.
6
6
  */
7
7
  export class LoanRenewHelper {
8
- constructor(hasPageChanged, hideToast, identifier, localCache, loanRenewConfig) {
8
+ constructor(hasPageChanged, identifier, localCache, loanRenewConfig) {
9
9
  this.hasPageChanged = hasPageChanged;
10
- this.hideToast = hideToast;
11
10
  this.identifier = identifier;
12
11
  this.localCache = localCache;
13
12
  this.loanRenewConfig = loanRenewConfig;
@@ -21,15 +20,12 @@ export class LoanRenewHelper {
21
20
  this.result = {
22
21
  texts: null,
23
22
  renewNow: false,
24
- timeLeft: 0,
25
23
  };
26
24
  }
27
25
 
28
26
  handleLoanRenew() {
29
27
  try {
30
28
  if (this.hasPageChanged) {
31
- // when user click/flip on book page
32
- this.showToastMessage();
33
29
  return this.pageChanged(); // user clicked on page
34
30
  }
35
31
  return this.autoChecker(); // auto checker at 50th minute
@@ -98,7 +94,6 @@ export class LoanRenewHelper {
98
94
  ) {
99
95
  this.result.texts = this.loanReturnWarning;
100
96
  this.result.renewNow = false; // not viewed
101
- this.showToastMessage();
102
97
  console.log('not viewed!');
103
98
  } else if (lastPageFlipTime >= lastFlipTimeFrame) {
104
99
  this.result.texts = '';
@@ -3,6 +3,7 @@ import { html, css, LitElement, nothing } from 'lit';
3
3
 
4
4
  import { SharedResizeObserver } from '@internetarchive/shared-resize-observer';
5
5
  import { ModalConfig } from '@internetarchive/modal-manager';
6
+ import { ToastConfig } from '@internetarchive/toast-manager';
6
7
  import { LocalCache } from '@internetarchive/local-cache';
7
8
 
8
9
  import './components/collapsible-action-group.js';
@@ -73,7 +74,7 @@ export default class IABookActions extends LitElement {
73
74
  // loan auto renew
74
75
  this.browserTimer = undefined;
75
76
  this.loanRenewConfig = {};
76
- this.hideToast = false;
77
+ this.suppressToast = false;
77
78
 
78
79
  /**
79
80
  * contains loan auto-renew response
@@ -83,6 +84,7 @@ export default class IABookActions extends LitElement {
83
84
  this.loanRenewResult = {
84
85
  texts: null,
85
86
  renewNow: false,
87
+ timeLeft: 0
86
88
  };
87
89
 
88
90
  this._shadowRoot = this.attachShadow({ mode: 'open' });
@@ -235,6 +237,7 @@ export default class IABookActions extends LitElement {
235
237
  // dispatched this event from bookreader page changed
236
238
  window.addEventListener('BookReader:pageChanged', event => {
237
239
  if (this.borrowType === 'browsed' && event.detail.hasPageChanged) {
240
+ console.log('actual page changed executed!')
238
241
  this.autoLoanRenewChecker(true);
239
242
  }
240
243
 
@@ -243,7 +246,7 @@ export default class IABookActions extends LitElement {
243
246
 
244
247
  document.addEventListener('click', () => {
245
248
  if (this.loanRenewHelper && this.loanRenewHelper.result.renewNow === false) {
246
- this.hideToast = true;
249
+ this.suppressToast = true;
247
250
  }
248
251
  });
249
252
 
@@ -259,8 +262,8 @@ export default class IABookActions extends LitElement {
259
262
 
260
263
  // show warning message with remaining time to auto returned it.
261
264
  if (this.loanRenewHelper.result.renewNow === false) {
262
- this.loanRenewHelper.result.timeLeft = eventDetails.timeLeft;
263
- this.loanRenewHelper.showToastMessage();
265
+ this.loanRenewResult.timeLeft = eventDetails.timeLeft;
266
+ this.showToastMessage();
264
267
  }
265
268
  });
266
269
  }
@@ -268,7 +271,6 @@ export default class IABookActions extends LitElement {
268
271
  async autoLoanRenewChecker(hasPageChanged = false) {
269
272
  this.loanRenewHelper = new LoanRenewHelper(
270
273
  hasPageChanged,
271
- this.hideToast,
272
274
  this.identifier,
273
275
  this.localCache,
274
276
  this.loanRenewConfig
@@ -279,6 +281,25 @@ export default class IABookActions extends LitElement {
279
281
  this.loanRenewResult = this.loanRenewHelper.result;
280
282
  }
281
283
 
284
+ async showToastMessage() {
285
+ if (this.suppressToast) return false;
286
+
287
+ // this.result = this.loanRenewHelper.result;
288
+ const iaBookActions = document.querySelector('ia-book-actions').shadowRoot;
289
+ let toastTemplate = iaBookActions.querySelector('toast-template');
290
+ if (!toastTemplate) {
291
+ toastTemplate = document.createElement('toast-template');
292
+ }
293
+ await iaBookActions.appendChild(toastTemplate);
294
+
295
+ const config = new ToastConfig();
296
+ config.texts = this.loanRenewResult.texts?.replace(/#time/, this.loanRenewResult.timeLeft);
297
+ config.dismisOnClick = true;
298
+ toastTemplate.showToast({
299
+ config,
300
+ });
301
+ }
302
+
282
303
  async browseHasRenew() {
283
304
  const loanTime = await this.localCache.get(`${this.identifier}-loanTime`);
284
305
  const secondsLeft = (loanTime - new Date()) / 1000; // different in seconds
@@ -328,7 +349,8 @@ export default class IABookActions extends LitElement {
328
349
  this.loanRenewResult.texts =
329
350
  'This book has been automatically returned due to inactivity.';
330
351
 
331
- this.loanRenewHelper.showToastMessage();
352
+ this.suppressToast = false;
353
+ this.showToastMessage();
332
354
  }
333
355
 
334
356
  /**
@@ -385,7 +407,7 @@ export default class IABookActions extends LitElement {
385
407
  get timerCountdownTemplate() {
386
408
  return this.borrowType === 'browsed'
387
409
  ? html`<timer-countdown
388
- .time=${Number(this.lendingStatus.secondsLeftOnLoan)}
410
+ .timeLeftOnLoan=${Number(this.lendingStatus.secondsLeftOnLoan)}
389
411
  .autoCheckAt=${this.loanRenewConfig.autoCheckAt}
390
412
  ></timer-countdown>`
391
413
  : nothing;
@@ -425,9 +447,10 @@ export default class IABookActions extends LitElement {
425
447
  */
426
448
  async handleLoanAutoRenewed() {
427
449
  if (this.loanRenewResult.renewNow) {
428
- this.hideToast = false;
429
450
  this.tokenPoller.disconnectedInterval();
430
- await this.loanRenewHelper.showToastMessage();
451
+
452
+ this.suppressToast = false;
453
+ await this.showToastMessage();
431
454
  await this.browseHasRenew();
432
455
  await this.resetTimerCountState();
433
456
  }