@internetarchive/ia-book-actions 0.0.29-alpha23 → 0.0.29-alpha25
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
|
@@ -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
|
-
|
|
6
|
+
timeLeftOnLoan: { type: Number },
|
|
7
7
|
autoCheckAt: { type: Number },
|
|
8
8
|
};
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
constructor() {
|
|
12
12
|
super();
|
|
13
|
-
this.
|
|
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('
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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,
|
|
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 = '';
|
package/src/ia-book-actions.js
CHANGED
|
@@ -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.
|
|
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' });
|
|
@@ -234,7 +236,9 @@ export default class IABookActions extends LitElement {
|
|
|
234
236
|
bindLoanRenewEvents() {
|
|
235
237
|
// dispatched this event from bookreader page changed
|
|
236
238
|
window.addEventListener('BookReader:pageChanged', event => {
|
|
239
|
+
console.log(event);
|
|
237
240
|
if (this.borrowType === 'browsed' && event.detail.hasPageChanged) {
|
|
241
|
+
console.log('actual page changed executed!')
|
|
238
242
|
this.autoLoanRenewChecker(true);
|
|
239
243
|
}
|
|
240
244
|
|
|
@@ -243,7 +247,7 @@ export default class IABookActions extends LitElement {
|
|
|
243
247
|
|
|
244
248
|
document.addEventListener('click', () => {
|
|
245
249
|
if (this.loanRenewHelper && this.loanRenewHelper.result.renewNow === false) {
|
|
246
|
-
this.
|
|
250
|
+
this.suppressToast = true;
|
|
247
251
|
}
|
|
248
252
|
});
|
|
249
253
|
|
|
@@ -259,8 +263,8 @@ export default class IABookActions extends LitElement {
|
|
|
259
263
|
|
|
260
264
|
// show warning message with remaining time to auto returned it.
|
|
261
265
|
if (this.loanRenewHelper.result.renewNow === false) {
|
|
262
|
-
this.
|
|
263
|
-
this.
|
|
266
|
+
this.loanRenewResult.timeLeft = eventDetails.timeLeft;
|
|
267
|
+
this.showToastMessage();
|
|
264
268
|
}
|
|
265
269
|
});
|
|
266
270
|
}
|
|
@@ -268,7 +272,6 @@ export default class IABookActions extends LitElement {
|
|
|
268
272
|
async autoLoanRenewChecker(hasPageChanged = false) {
|
|
269
273
|
this.loanRenewHelper = new LoanRenewHelper(
|
|
270
274
|
hasPageChanged,
|
|
271
|
-
this.hideToast,
|
|
272
275
|
this.identifier,
|
|
273
276
|
this.localCache,
|
|
274
277
|
this.loanRenewConfig
|
|
@@ -279,6 +282,25 @@ export default class IABookActions extends LitElement {
|
|
|
279
282
|
this.loanRenewResult = this.loanRenewHelper.result;
|
|
280
283
|
}
|
|
281
284
|
|
|
285
|
+
async showToastMessage() {
|
|
286
|
+
if (this.suppressToast) return false;
|
|
287
|
+
|
|
288
|
+
// this.result = this.loanRenewHelper.result;
|
|
289
|
+
const iaBookActions = document.querySelector('ia-book-actions').shadowRoot;
|
|
290
|
+
let toastTemplate = iaBookActions.querySelector('toast-template');
|
|
291
|
+
if (!toastTemplate) {
|
|
292
|
+
toastTemplate = document.createElement('toast-template');
|
|
293
|
+
}
|
|
294
|
+
await iaBookActions.appendChild(toastTemplate);
|
|
295
|
+
|
|
296
|
+
const config = new ToastConfig();
|
|
297
|
+
config.texts = this.loanRenewResult.texts?.replace(/#time/, this.loanRenewResult.timeLeft);
|
|
298
|
+
config.dismisOnClick = true;
|
|
299
|
+
toastTemplate.showToast({
|
|
300
|
+
config,
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
|
|
282
304
|
async browseHasRenew() {
|
|
283
305
|
const loanTime = await this.localCache.get(`${this.identifier}-loanTime`);
|
|
284
306
|
const secondsLeft = (loanTime - new Date()) / 1000; // different in seconds
|
|
@@ -328,7 +350,8 @@ export default class IABookActions extends LitElement {
|
|
|
328
350
|
this.loanRenewResult.texts =
|
|
329
351
|
'This book has been automatically returned due to inactivity.';
|
|
330
352
|
|
|
331
|
-
this.
|
|
353
|
+
this.suppressToast = false;
|
|
354
|
+
this.showToastMessage();
|
|
332
355
|
}
|
|
333
356
|
|
|
334
357
|
/**
|
|
@@ -385,7 +408,7 @@ export default class IABookActions extends LitElement {
|
|
|
385
408
|
get timerCountdownTemplate() {
|
|
386
409
|
return this.borrowType === 'browsed'
|
|
387
410
|
? html`<timer-countdown
|
|
388
|
-
.
|
|
411
|
+
.timeLeftOnLoan=${Number(this.lendingStatus.secondsLeftOnLoan)}
|
|
389
412
|
.autoCheckAt=${this.loanRenewConfig.autoCheckAt}
|
|
390
413
|
></timer-countdown>`
|
|
391
414
|
: nothing;
|
|
@@ -425,9 +448,10 @@ export default class IABookActions extends LitElement {
|
|
|
425
448
|
*/
|
|
426
449
|
async handleLoanAutoRenewed() {
|
|
427
450
|
if (this.loanRenewResult.renewNow) {
|
|
428
|
-
this.hideToast = false;
|
|
429
451
|
this.tokenPoller.disconnectedInterval();
|
|
430
|
-
|
|
452
|
+
|
|
453
|
+
this.suppressToast = false;
|
|
454
|
+
await this.showToastMessage();
|
|
431
455
|
await this.browseHasRenew();
|
|
432
456
|
await this.resetTimerCountState();
|
|
433
457
|
}
|