@internetarchive/ia-book-actions 0.1.11-b5 → 0.1.11-b7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@internetarchive/ia-book-actions",
3
- "version": "0.1.11-b5",
3
+ "version": "0.1.11-b7",
4
4
  "description": "Bookreader actions for borrowable book",
5
5
  "author": "Internet Archive",
6
6
  "license": "AGPL-3.0-only",
@@ -21,7 +21,7 @@ export default async function ActionsHandlerService(options) {
21
21
  const tokenError = 'loan token not found. please try again later.';
22
22
  const borrowError =
23
23
  'This book is not available to borrow at this time. Please try again later.';
24
- const erroneousActions = ['browse_book', 'borrow_book', 'create_token'];
24
+ const erroneousActions = ['browse_book', 'borrow_book', 'create_token', 'renew_loan'];
25
25
  const shouldReturnError =
26
26
  location?.href?.indexOf('?error=true') !== -1 &&
27
27
  location?.hostname !== 'archive.org';
@@ -142,7 +142,13 @@ export default class ActionsHandler extends LitElement {
142
142
  action,
143
143
  identifier: this.identifier,
144
144
  success: data => {
145
- console.log("RENEW_LOAN --- ", data, action, data.loan, this.identifier);
145
+ console.log(
146
+ 'RENEW_LOAN --- ',
147
+ data,
148
+ action,
149
+ data.loan,
150
+ this.identifier
151
+ );
146
152
  if (data?.success === true && data?.loan?.renewal === true) {
147
153
  this.setBrowseTimeSession();
148
154
  this.dispatchEvent(
@@ -105,7 +105,7 @@ export default class IABookActions extends LitElement {
105
105
  * loanTotalTime: number, - total seconds a loan does have
106
106
  * loanRenewAtLast: number, - check for loan renew at last
107
107
  * pageChangedInLast: number, - consider loan renew eligible if viewed new page
108
- */
108
+ */
109
109
  this.loanRenewTimeConfig = {
110
110
  loanTotalTime: 3600, // 1 hour
111
111
  loanRenewAtLast: 660, // 11 minutes
@@ -257,7 +257,9 @@ export default class IABookActions extends LitElement {
257
257
  // initial timer state for 1-hour loan
258
258
  if (this.borrowType === 'browsed' && !this.loanRenewResult.renewNow) {
259
259
  setTimeout(() => {
260
- console.log('IN TIMEOUT OF --- setupLendingToolbarActions, firing resetTimerCountState');
260
+ console.log(
261
+ 'IN TIMEOUT OF --- setupLendingToolbarActions, firing resetTimerCountState'
262
+ );
261
263
  this.resetTimerCountState();
262
264
  }, 100);
263
265
  }
@@ -306,19 +308,18 @@ export default class IABookActions extends LitElement {
306
308
  this.loanRenewResult = this.loanRenewHelper.result;
307
309
  }
308
310
 
309
- /**
310
- * create / select the modal-manager component on DOM
311
- * @memberof IABookActions
312
- */
313
- async useModalManager() {
314
- if (!this.modal) {
315
- this.modal = document.querySelector('modal-manager');
311
+ /** @returns HTMLElement */
312
+ get modal() {
313
+ let modalOnDom = document.querySelector('modal-manager');
316
314
 
317
- if (!this.modal) this.modal = document.createElement('modal-manager');
315
+ if (!modalOnDom) {
316
+ const newModal = document.createElement('modal-manager');
317
+ document.body.appendChild(newModal);
318
+ modalOnDom = newModal;
318
319
  }
319
320
 
320
321
  this.modal.id = 'action-bar-modal';
321
- await document.body.appendChild(this.modal);
322
+ return modalOnDom;
322
323
  }
323
324
 
324
325
  /**
@@ -326,7 +327,6 @@ export default class IABookActions extends LitElement {
326
327
  */
327
328
  async showWarningModal() {
328
329
  console.log('****** showWarningModal ******');
329
- await this.useModalManager();
330
330
 
331
331
  // if secondsLeft < 60, consider it 1 minute
332
332
  let { secondsLeft } = this.loanRenewResult;
@@ -336,23 +336,21 @@ export default class IABookActions extends LitElement {
336
336
  secondsLeft = secondsLeft > 60 ? secondsLeft : 60;
337
337
  }
338
338
 
339
- const config = new ModalConfig();
340
- config.headline = 'Are you still here?';
341
- config.showCloseButton = false;
342
- config.closeOnBackdropClick = false;
343
- config.message = this.loanRenewHelper?.getMessageTexts(
344
- this.loanRenewResult.texts,
345
- secondsLeft
346
- );
339
+ const config = new ModalConfig({
340
+ headline: 'Are you still here?',
341
+ showCloseButton: false,
342
+ closeOnBackdropClick: false,
343
+ message: this.loanRenewHelper?.getMessageTexts(
344
+ this.loanRenewResult.texts,
345
+ secondsLeft
346
+ )
347
+ });
347
348
 
348
- const customContent = html`<br />
349
+ const customModalContent = html`<br />
349
350
  <div id="custom-buttons" style="display:flex;justify-content:center;">
350
351
  <button
351
352
  style="${modalButtonStyle.iaButton} ${modalButtonStyle.renew}"
352
- @click=${async event => {
353
- await this.changeModalState(event);
354
- this.loanRenewResult = { texts: '', renewNow: true };
355
- }}
353
+ @click=${event => this.patronWantsToRenewBook(event)}
356
354
  >
357
355
  Keep reading
358
356
  </button>
@@ -368,7 +366,12 @@ export default class IABookActions extends LitElement {
368
366
  </button>
369
367
  </div> `;
370
368
 
371
- this.modal?.showModal({ config, customModalContent: customContent });
369
+ this.modal?.showModal({ config, customModalContent });
370
+ }
371
+
372
+ async patronWantsToRenewBook(event) {
373
+ await this.changeModalState(event);
374
+ this.loanRenewResult = { texts: '', renewNow: true };
372
375
  }
373
376
 
374
377
  /**
@@ -379,9 +382,11 @@ export default class IABookActions extends LitElement {
379
382
  * @param {Event} event
380
383
  */
381
384
  changeModalState(event) {
385
+ // this doesn't really work? shows empty button
382
386
  if (event.target === undefined) return;
383
387
 
384
388
  const button = event.target;
389
+ button.disabled = true;
385
390
  button.innerHTML = `<ia-activity-indicator
386
391
  mode="processing"
387
392
  style=${modalButtonStyle.loaderIcon}
@@ -397,15 +402,15 @@ export default class IABookActions extends LitElement {
397
402
  */
398
403
  async showExpiredModal() {
399
404
  console.log('showExpiredModal()');
400
- await this.useModalManager();
401
405
 
402
- const config = new ModalConfig();
403
- config.headline = '';
404
- config.showCloseButton = false;
405
- config.closeOnBackdropClick = false;
406
- config.message = 'This book has been returned due to inactivity.';
406
+ const config = new ModalConfig({
407
+ headline: '',
408
+ showCloseButton: false,
409
+ closeOnBackdropClick: false,
410
+ message: 'This book has been returned due to inactivity.'
411
+ });
407
412
 
408
- const customContent = html`<br />
413
+ const customModalContent = html`<br />
409
414
  <div style="text-align: center">
410
415
  <button
411
416
  style="${modalButtonStyle.iaButton} ${modalButtonStyle.renew}"
@@ -417,7 +422,7 @@ export default class IABookActions extends LitElement {
417
422
  </button>
418
423
  </div> `;
419
424
 
420
- this.modal?.showModal({ config, customModalContent: customContent });
425
+ this.modal?.showModal({ config, customModalContent });
421
426
  }
422
427
 
423
428
  /**
@@ -508,7 +513,11 @@ export default class IABookActions extends LitElement {
508
513
 
509
514
  async startBrowseTimer() {
510
515
  clearTimeout(this.browseTimer);
511
- console.log('startBrowseTimerstartBrowseTimer just cleared timer', this.browseTimer, this.lendingStatus);
516
+ console.log(
517
+ 'startBrowseTimerstartBrowseTimer just cleared timer',
518
+ this.browseTimer,
519
+ this.lendingStatus
520
+ );
512
521
 
513
522
  const {
514
523
  browsingExpired,
@@ -521,7 +530,10 @@ export default class IABookActions extends LitElement {
521
530
  }
522
531
 
523
532
  this.browseTimer = setTimeout(() => {
524
- console.log('start `this.browseTimer will fire his.browseHasExpired() - secondsLeftOnLoan', secondsLeftOnLoan);
533
+ console.log(
534
+ 'start `this.browseTimer will fire his.browseHasExpired() - secondsLeftOnLoan',
535
+ secondsLeftOnLoan
536
+ );
525
537
  this.browseHasExpired();
526
538
  }, secondsLeftOnLoan * 1000);
527
539
  }
@@ -533,12 +545,22 @@ export default class IABookActions extends LitElement {
533
545
  const timerCountdown = this._shadowRoot.querySelector('timer-countdown');
534
546
  if (!timerCountdown) return;
535
547
 
536
- const secondsLeft = Number.parseInt((Number(this.lendingStatus.secondsLeftOnLoan)), 10);
537
- const firstStrokeDashOffset = Number.parseInt(((secondsLeft / this.loanRenewTimeConfig.loanTotalTime) * 315) * 1.5, 10);
538
- const actualStrokeDashOffset = firstStrokeDashOffset > 315 ? 315 : firstStrokeDashOffset;
539
-
548
+ const secondsLeft = Number.parseInt(
549
+ Number(this.lendingStatus.secondsLeftOnLoan),
550
+ 10
551
+ );
552
+ const firstStrokeDashOffset = Number.parseInt(
553
+ (secondsLeft / this.loanRenewTimeConfig.loanTotalTime) * 315 * 1.5,
554
+ 10
555
+ );
556
+ const actualStrokeDashOffset =
557
+ firstStrokeDashOffset > 315 ? 315 : firstStrokeDashOffset;
540
558
 
541
- console.log('resetTimerCountState === ', { secondsLeft, firstStrokeDashOffset, actualStrokeDashOffset});
559
+ console.log('resetTimerCountState === ', {
560
+ secondsLeft,
561
+ firstStrokeDashOffset,
562
+ actualStrokeDashOffset,
563
+ });
542
564
 
543
565
  // set seconds left in loan expire
544
566
  timerCountdown.style?.setProperty('--secondsLeftOnLoan', `${secondsLeft}s`);
@@ -624,11 +646,10 @@ export default class IABookActions extends LitElement {
624
646
  secondsLeftOnLoan: Math.round(this.lendingStatus.secondsLeftOnLoan), // stale? how to fix
625
647
  });
626
648
 
627
-
628
649
  // Now, let's reset loan duration & this.lendingStatus
629
650
  const loanTime = await this.localCache.get(`${this.identifier}-loanTime`);
630
651
  const secondsLeft = (loanTime - new Date()) / 1000; // different in seconds
631
-
652
+
632
653
  const currStatus = {
633
654
  ...this.lendingStatus,
634
655
  user_has_browsed: true,
@@ -638,15 +659,16 @@ export default class IABookActions extends LitElement {
638
659
  this.lendingStatus = currStatus;
639
660
 
640
661
  console.log('~~~ handleLoanAutoRenewed - new this.lendingStatus', {
641
- secondsLeftOnLoan: this.lendingStatus.secondsLeftOnLoan
662
+ secondsLeftOnLoan: this.lendingStatus.secondsLeftOnLoan,
642
663
  });
643
-
664
+
644
665
  /**
645
666
  * reset timer countdown states
646
667
  */
647
668
  await this.resetTimerCountState();
648
669
 
649
670
  // close the modal
671
+ this.modal.removeAttribute('id');
650
672
  this.modal?.remove();
651
673
  this.modal?.showModal({ config: {}, customModalContent: `` });
652
674
 
@@ -675,7 +697,10 @@ export default class IABookActions extends LitElement {
675
697
  // if really updated, escape from here
676
698
  const resync = await this.reSyncTimerIfGoneOff(secondsLeft);
677
699
 
678
- console.log("*** startTimerCountdown = reSyncTimerIfGoneOff ***", { secondsLeft, resync });
700
+ console.log('*** startTimerCountdown = reSyncTimerIfGoneOff ***', {
701
+ secondsLeft,
702
+ resync,
703
+ });
679
704
 
680
705
  if (resync) {
681
706
  console.log('have resyncd, will end startTimerCountdown here - ');
@@ -883,7 +908,6 @@ export default class IABookActions extends LitElement {
883
908
 
884
909
  /* show error message if something went wrong */
885
910
  async showErrorModal(errorMsg, action) {
886
- await this.useModalManager();
887
911
 
888
912
  const modalConfig = new ModalConfig({
889
913
  title: 'Lending error',