@internetarchive/ia-book-actions 0.1.11-b2 → 0.1.11-b21
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
package/package.json
CHANGED
|
@@ -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(
|
|
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(
|
|
@@ -300,6 +306,7 @@ export default class ActionsHandler extends LitElement {
|
|
|
300
306
|
* set browse time in indexedDB
|
|
301
307
|
*/
|
|
302
308
|
async setBrowseTimeSession() {
|
|
309
|
+
// TODO: USE loan info to determine what we have left
|
|
303
310
|
try {
|
|
304
311
|
const expireDate = new Date(
|
|
305
312
|
new Date().getTime() + this.loanTotalTime * 1000
|
package/src/ia-book-actions.js
CHANGED
|
@@ -77,7 +77,6 @@ export default class IABookActions extends LitElement {
|
|
|
77
77
|
this.barType = 'action'; // 'title'|'action'
|
|
78
78
|
this.sharedObserver = undefined;
|
|
79
79
|
this.disableActionGroup = false;
|
|
80
|
-
this.modal = undefined;
|
|
81
80
|
this.tokenDelay = 120; // in seconds
|
|
82
81
|
|
|
83
82
|
// private props
|
|
@@ -100,13 +99,17 @@ export default class IABookActions extends LitElement {
|
|
|
100
99
|
|
|
101
100
|
/**
|
|
102
101
|
* contains one hour auto-loan-renew time configuration
|
|
103
|
-
*
|
|
104
|
-
* @type {
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
102
|
+
* defaults to 1 hour config
|
|
103
|
+
* @type {{
|
|
104
|
+
* loanTotalTime: number, - total seconds a loan does have
|
|
105
|
+
* loanRenewAtLast: number, - check for loan renew at last
|
|
106
|
+
* pageChangedInLast: number, - consider loan renew eligible if viewed new page
|
|
108
107
|
*/
|
|
109
|
-
this.loanRenewTimeConfig = {
|
|
108
|
+
this.loanRenewTimeConfig = {
|
|
109
|
+
loanTotalTime: 3600, // 1 hour
|
|
110
|
+
loanRenewAtLast: 660, // 11 minutes
|
|
111
|
+
pageChangedInLast: 900, // 15 minutes
|
|
112
|
+
};
|
|
110
113
|
|
|
111
114
|
/**
|
|
112
115
|
* contains one hour auto-loan-renew response
|
|
@@ -253,6 +256,9 @@ export default class IABookActions extends LitElement {
|
|
|
253
256
|
// initial timer state for 1-hour loan
|
|
254
257
|
if (this.borrowType === 'browsed' && !this.loanRenewResult.renewNow) {
|
|
255
258
|
setTimeout(() => {
|
|
259
|
+
console.log(
|
|
260
|
+
'IN TIMEOUT OF --- setupLendingToolbarActions, firing resetTimerCountState'
|
|
261
|
+
);
|
|
256
262
|
this.resetTimerCountState();
|
|
257
263
|
}, 100);
|
|
258
264
|
}
|
|
@@ -301,19 +307,12 @@ export default class IABookActions extends LitElement {
|
|
|
301
307
|
this.loanRenewResult = this.loanRenewHelper.result;
|
|
302
308
|
}
|
|
303
309
|
|
|
304
|
-
/**
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
*/
|
|
308
|
-
async useModalManager() {
|
|
309
|
-
if (!this.modal) {
|
|
310
|
-
this.modal = document.querySelector('modal-manager');
|
|
311
|
-
|
|
312
|
-
if (!this.modal) this.modal = document.createElement('modal-manager');
|
|
313
|
-
}
|
|
310
|
+
/** @returns HTMLElement */
|
|
311
|
+
get modal() {
|
|
312
|
+
const modalOnDom = document.querySelector('modal-manager');
|
|
314
313
|
|
|
315
|
-
|
|
316
|
-
|
|
314
|
+
modalOnDom.id = 'action-bar-modal';
|
|
315
|
+
return modalOnDom;
|
|
317
316
|
}
|
|
318
317
|
|
|
319
318
|
/**
|
|
@@ -321,7 +320,9 @@ export default class IABookActions extends LitElement {
|
|
|
321
320
|
*/
|
|
322
321
|
async showWarningModal() {
|
|
323
322
|
console.log('****** showWarningModal ******');
|
|
324
|
-
|
|
323
|
+
// clear modal
|
|
324
|
+
this.modal?.showModal(new ModalConfig({ customModalContent: nothing }));
|
|
325
|
+
this.modal?.closeModal();
|
|
325
326
|
|
|
326
327
|
// if secondsLeft < 60, consider it 1 minute
|
|
327
328
|
let { secondsLeft } = this.loanRenewResult;
|
|
@@ -331,30 +332,28 @@ export default class IABookActions extends LitElement {
|
|
|
331
332
|
secondsLeft = secondsLeft > 60 ? secondsLeft : 60;
|
|
332
333
|
}
|
|
333
334
|
|
|
334
|
-
const config = new ModalConfig(
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
335
|
+
const config = new ModalConfig({
|
|
336
|
+
headline: 'Are you still here?',
|
|
337
|
+
showCloseButton: false,
|
|
338
|
+
closeOnBackdropClick: false,
|
|
339
|
+
message: this.loanRenewHelper?.getMessageTexts(
|
|
340
|
+
this.loanRenewResult.texts,
|
|
341
|
+
secondsLeft
|
|
342
|
+
)
|
|
343
|
+
});
|
|
342
344
|
|
|
343
|
-
const
|
|
344
|
-
<div id="custom-buttons" style="display:flex;justify-content:center;">
|
|
345
|
+
const customModalContent = html`<br />
|
|
346
|
+
<div id="book-action-bar-custom-buttons" style="display:flex;justify-content:center;">
|
|
345
347
|
<button
|
|
346
348
|
style="${modalButtonStyle.iaButton} ${modalButtonStyle.renew}"
|
|
347
|
-
@click=${
|
|
348
|
-
await this.changeModalState(event);
|
|
349
|
-
this.loanRenewResult = { texts: '', renewNow: true };
|
|
350
|
-
}}
|
|
349
|
+
@click=${event => this.patronWantsToRenewBook(event)}
|
|
351
350
|
>
|
|
352
351
|
Keep reading
|
|
353
352
|
</button>
|
|
354
353
|
<button
|
|
355
354
|
style="${modalButtonStyle.iaButton} ${modalButtonStyle.return}"
|
|
356
355
|
@click=${event => {
|
|
357
|
-
this.changeModalState(event);
|
|
356
|
+
// this.changeModalState(event);
|
|
358
357
|
document.querySelector('ia-book-actions').disableActionGroup = true;
|
|
359
358
|
this.returnNow = true;
|
|
360
359
|
}}
|
|
@@ -363,7 +362,12 @@ export default class IABookActions extends LitElement {
|
|
|
363
362
|
</button>
|
|
364
363
|
</div> `;
|
|
365
364
|
|
|
366
|
-
this.modal?.showModal({ config, customModalContent
|
|
365
|
+
this.modal?.showModal({ config, customModalContent });
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
async patronWantsToRenewBook(event) {
|
|
369
|
+
// this.changeModalState(event);
|
|
370
|
+
this.loanRenewResult = { texts: '', renewNow: true };
|
|
367
371
|
}
|
|
368
372
|
|
|
369
373
|
/**
|
|
@@ -374,9 +378,12 @@ export default class IABookActions extends LitElement {
|
|
|
374
378
|
* @param {Event} event
|
|
375
379
|
*/
|
|
376
380
|
changeModalState(event) {
|
|
381
|
+
// this doesn't really work? shows empty button
|
|
382
|
+
|
|
377
383
|
if (event.target === undefined) return;
|
|
378
384
|
|
|
379
385
|
const button = event.target;
|
|
386
|
+
button.disabled = true;
|
|
380
387
|
button.innerHTML = `<ia-activity-indicator
|
|
381
388
|
mode="processing"
|
|
382
389
|
style=${modalButtonStyle.loaderIcon}
|
|
@@ -392,15 +399,15 @@ export default class IABookActions extends LitElement {
|
|
|
392
399
|
*/
|
|
393
400
|
async showExpiredModal() {
|
|
394
401
|
console.log('showExpiredModal()');
|
|
395
|
-
await this.useModalManager();
|
|
396
402
|
|
|
397
|
-
const config = new ModalConfig(
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
403
|
+
const config = new ModalConfig({
|
|
404
|
+
headline: '',
|
|
405
|
+
showCloseButton: false,
|
|
406
|
+
closeOnBackdropClick: false,
|
|
407
|
+
message: 'This book has been returned due to inactivity.'
|
|
408
|
+
});
|
|
402
409
|
|
|
403
|
-
const
|
|
410
|
+
const customModalContent = html`<br />
|
|
404
411
|
<div style="text-align: center">
|
|
405
412
|
<button
|
|
406
413
|
style="${modalButtonStyle.iaButton} ${modalButtonStyle.renew}"
|
|
@@ -412,15 +419,7 @@ export default class IABookActions extends LitElement {
|
|
|
412
419
|
</button>
|
|
413
420
|
</div> `;
|
|
414
421
|
|
|
415
|
-
this.modal?.showModal({ config, customModalContent
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
/**
|
|
419
|
-
* close/hide modal-manager
|
|
420
|
-
*/
|
|
421
|
-
async closeModalManager() {
|
|
422
|
-
this.modal = document.querySelector('modal-manager');
|
|
423
|
-
this.modal?.closeModal();
|
|
422
|
+
this.modal?.showModal({ config, customModalContent });
|
|
424
423
|
}
|
|
425
424
|
|
|
426
425
|
/**
|
|
@@ -478,21 +477,6 @@ export default class IABookActions extends LitElement {
|
|
|
478
477
|
});
|
|
479
478
|
}
|
|
480
479
|
|
|
481
|
-
async browseHasRenew() {
|
|
482
|
-
window?.IALendingIntervals?.clearAll();
|
|
483
|
-
|
|
484
|
-
const loanTime = await this.localCache.get(`${this.identifier}-loanTime`);
|
|
485
|
-
const secondsLeft = (loanTime - new Date()) / 1000; // different in seconds
|
|
486
|
-
|
|
487
|
-
const currStatus = {
|
|
488
|
-
...this.lendingStatus,
|
|
489
|
-
user_has_browsed: true,
|
|
490
|
-
browsingExpired: false,
|
|
491
|
-
secondsLeftOnLoan: secondsLeft,
|
|
492
|
-
};
|
|
493
|
-
this.lendingStatus = currStatus;
|
|
494
|
-
}
|
|
495
|
-
|
|
496
480
|
/**
|
|
497
481
|
* Execute when loan is expired
|
|
498
482
|
*/
|
|
@@ -518,7 +502,11 @@ export default class IABookActions extends LitElement {
|
|
|
518
502
|
|
|
519
503
|
async startBrowseTimer() {
|
|
520
504
|
clearTimeout(this.browseTimer);
|
|
521
|
-
console.log(
|
|
505
|
+
console.log(
|
|
506
|
+
'startBrowseTimerstartBrowseTimer just cleared timer',
|
|
507
|
+
this.browseTimer,
|
|
508
|
+
this.lendingStatus
|
|
509
|
+
);
|
|
522
510
|
|
|
523
511
|
const {
|
|
524
512
|
browsingExpired,
|
|
@@ -531,7 +519,10 @@ export default class IABookActions extends LitElement {
|
|
|
531
519
|
}
|
|
532
520
|
|
|
533
521
|
this.browseTimer = setTimeout(() => {
|
|
534
|
-
console.log(
|
|
522
|
+
console.log(
|
|
523
|
+
'start `this.browseTimer will fire his.browseHasExpired() - secondsLeftOnLoan',
|
|
524
|
+
secondsLeftOnLoan
|
|
525
|
+
);
|
|
535
526
|
this.browseHasExpired();
|
|
536
527
|
}, secondsLeftOnLoan * 1000);
|
|
537
528
|
}
|
|
@@ -543,11 +534,22 @@ export default class IABookActions extends LitElement {
|
|
|
543
534
|
const timerCountdown = this._shadowRoot.querySelector('timer-countdown');
|
|
544
535
|
if (!timerCountdown) return;
|
|
545
536
|
|
|
546
|
-
const secondsLeft = Number(
|
|
547
|
-
|
|
548
|
-
|
|
537
|
+
const secondsLeft = Number.parseInt(
|
|
538
|
+
Number(this.lendingStatus.secondsLeftOnLoan),
|
|
539
|
+
10
|
|
540
|
+
);
|
|
541
|
+
const firstStrokeDashOffset = Number.parseInt(
|
|
542
|
+
(secondsLeft / this.loanRenewTimeConfig.loanTotalTime) * 315 * 1.5,
|
|
543
|
+
10
|
|
544
|
+
);
|
|
545
|
+
const actualStrokeDashOffset =
|
|
546
|
+
firstStrokeDashOffset > 315 ? 315 : firstStrokeDashOffset;
|
|
549
547
|
|
|
550
|
-
|
|
548
|
+
console.log('resetTimerCountState === ', {
|
|
549
|
+
secondsLeft,
|
|
550
|
+
firstStrokeDashOffset,
|
|
551
|
+
actualStrokeDashOffset,
|
|
552
|
+
});
|
|
551
553
|
|
|
552
554
|
// set seconds left in loan expire
|
|
553
555
|
timerCountdown.style?.setProperty('--secondsLeftOnLoan', `${secondsLeft}s`);
|
|
@@ -555,7 +557,7 @@ export default class IABookActions extends LitElement {
|
|
|
555
557
|
// set circle stroke offset left in loan expire
|
|
556
558
|
timerCountdown.style?.setProperty(
|
|
557
559
|
'--strokeLeftOnLoan',
|
|
558
|
-
`${
|
|
560
|
+
`${actualStrokeDashOffset}` // the perimeter of the circle = (π * 2 * radius)
|
|
559
561
|
);
|
|
560
562
|
|
|
561
563
|
const animationCircle = timerCountdown.shadowRoot.querySelector('.circle');
|
|
@@ -584,14 +586,6 @@ export default class IABookActions extends LitElement {
|
|
|
584
586
|
></book-title-bar>`;
|
|
585
587
|
}
|
|
586
588
|
|
|
587
|
-
get timerCountdownTemplate() {
|
|
588
|
-
return this.borrowType === 'browsed'
|
|
589
|
-
? html`<timer-countdown
|
|
590
|
-
.secondsLeftOnLoan=${Number(this.lendingStatus.secondsLeftOnLoan)}
|
|
591
|
-
></timer-countdown>`
|
|
592
|
-
: nothing;
|
|
593
|
-
}
|
|
594
|
-
|
|
595
589
|
get bookActionBar() {
|
|
596
590
|
return html`
|
|
597
591
|
<collapsible-action-group
|
|
@@ -616,7 +610,10 @@ export default class IABookActions extends LitElement {
|
|
|
616
610
|
>
|
|
617
611
|
</collapsible-action-group>
|
|
618
612
|
${this.textGroupTemplate} ${this.infoIconTemplate}
|
|
619
|
-
|
|
613
|
+
<timer-countdown
|
|
614
|
+
.secondsLeftOnLoan=${Number(this.lendingStatus.secondsLeftOnLoan)}
|
|
615
|
+
class=${this.borrowType === 'browsed' ? '' : 'hide'}
|
|
616
|
+
></timer-countdown>
|
|
620
617
|
`;
|
|
621
618
|
}
|
|
622
619
|
|
|
@@ -635,13 +632,24 @@ export default class IABookActions extends LitElement {
|
|
|
635
632
|
console.log('IABookActions: AutoRenewed:- ', {
|
|
636
633
|
ajaxResponse: event?.detail?.data,
|
|
637
634
|
loanRenewResult: this.loanRenewResult,
|
|
638
|
-
secondsLeftOnLoan: Math.round(this.lendingStatus.secondsLeftOnLoan),
|
|
635
|
+
secondsLeftOnLoan: Math.round(this.lendingStatus.secondsLeftOnLoan), // stale? how to fix
|
|
639
636
|
});
|
|
640
637
|
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
638
|
+
// Now, let's reset loan duration & this.lendingStatus
|
|
639
|
+
const loanTime = await this.localCache.get(`${this.identifier}-loanTime`);
|
|
640
|
+
const secondsLeft = (loanTime - new Date()) / 1000; // different in seconds
|
|
641
|
+
|
|
642
|
+
const currStatus = {
|
|
643
|
+
...this.lendingStatus,
|
|
644
|
+
user_has_browsed: true,
|
|
645
|
+
browsingExpired: false,
|
|
646
|
+
secondsLeftOnLoan: secondsLeft,
|
|
647
|
+
};
|
|
648
|
+
this.lendingStatus = currStatus;
|
|
649
|
+
|
|
650
|
+
console.log('~~~ handleLoanAutoRenewed - new this.lendingStatus', {
|
|
651
|
+
secondsLeftOnLoan: this.lendingStatus.secondsLeftOnLoan,
|
|
652
|
+
});
|
|
645
653
|
|
|
646
654
|
/**
|
|
647
655
|
* reset timer countdown states
|
|
@@ -649,8 +657,12 @@ export default class IABookActions extends LitElement {
|
|
|
649
657
|
await this.resetTimerCountState();
|
|
650
658
|
|
|
651
659
|
// close the modal
|
|
652
|
-
this.modal
|
|
653
|
-
this.modal
|
|
660
|
+
this.modal.removeAttribute('id');
|
|
661
|
+
const editedActionButtons = this.modal.shadowRoot.querySelector('#book-action-bar-custom-buttons');
|
|
662
|
+
if (editedActionButtons) {
|
|
663
|
+
editedActionButtons.parentElement.removeChild(editedActionButtons);
|
|
664
|
+
}
|
|
665
|
+
this.modal?.closeModal();
|
|
654
666
|
|
|
655
667
|
window?.Sentry?.captureMessage(sentryLogs.bookHasRenewed);
|
|
656
668
|
}
|
|
@@ -673,9 +685,14 @@ export default class IABookActions extends LitElement {
|
|
|
673
685
|
secondsLeft = Math.round(secondsLeft); // round number
|
|
674
686
|
|
|
675
687
|
// re-sync timer if gone off because of background window
|
|
688
|
+
// side effect: updates this.lendingStatus & kicks off lifecycle,
|
|
689
|
+
// if really updated, escape from here
|
|
676
690
|
const resync = await this.reSyncTimerIfGoneOff(secondsLeft);
|
|
677
691
|
|
|
678
|
-
console.log(
|
|
692
|
+
console.log('*** startTimerCountdown = reSyncTimerIfGoneOff ***', {
|
|
693
|
+
secondsLeft,
|
|
694
|
+
resync,
|
|
695
|
+
});
|
|
679
696
|
|
|
680
697
|
if (resync) {
|
|
681
698
|
console.log('have resyncd, will end startTimerCountdown here - ');
|
|
@@ -883,7 +900,6 @@ export default class IABookActions extends LitElement {
|
|
|
883
900
|
|
|
884
901
|
/* show error message if something went wrong */
|
|
885
902
|
async showErrorModal(errorMsg, action) {
|
|
886
|
-
await this.useModalManager();
|
|
887
903
|
|
|
888
904
|
const modalConfig = new ModalConfig({
|
|
889
905
|
title: 'Lending error',
|
|
@@ -948,6 +964,10 @@ export default class IABookActions extends LitElement {
|
|
|
948
964
|
display: block;
|
|
949
965
|
}
|
|
950
966
|
|
|
967
|
+
.hide {
|
|
968
|
+
display: none;
|
|
969
|
+
}
|
|
970
|
+
|
|
951
971
|
.lending-wrapper {
|
|
952
972
|
width: 100%;
|
|
953
973
|
margin: 0 auto;
|