@internetarchive/ia-book-actions 0.0.29-alpha22 → 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 +2 -1
- package/package.json +2 -2
- package/src/components/timer-countdown.js +8 -7
- package/src/core/services/actions-config.js +2 -2
- package/src/core/services/loan-renew-helper.js +5 -4
- package/src/ia-book-actions.js +37 -5
- package/test/components/collapsible-action-group.test.js +2 -2
- package/test/core/services/get-lending-actions.test.js +4 -4
- package/test/ia-book-actions.test.js +4 -4
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-
|
|
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",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"@internetarchive/local-cache": "^0.2.0",
|
|
23
23
|
"@internetarchive/modal-manager": "^0.2.1",
|
|
24
24
|
"@internetarchive/shared-resize-observer": "^0.2.0",
|
|
25
|
-
"@internetarchive/toast-manager": "0.0.1-
|
|
25
|
+
"@internetarchive/toast-manager": "0.0.1-alpha9",
|
|
26
26
|
"lit": "^2.5.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
@@ -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,6 +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.timeLeftOnLoan)}</span>
|
|
68
69
|
`;
|
|
69
70
|
}
|
|
70
71
|
|
|
@@ -36,7 +36,7 @@ export default class ActionsConfig {
|
|
|
36
36
|
firstBrowseConfig() {
|
|
37
37
|
return {
|
|
38
38
|
id: 'browseBook',
|
|
39
|
-
text: 'Borrow for 1 hour'
|
|
39
|
+
text: 'Borrow', // changed texts from 'Borrow for 1 hour' to 'Borrow'
|
|
40
40
|
className: 'primary',
|
|
41
41
|
analyticsEvent: {
|
|
42
42
|
category: this.analyticsCategories.preview,
|
|
@@ -48,7 +48,7 @@ export default class ActionsConfig {
|
|
|
48
48
|
browseAgainConfig() {
|
|
49
49
|
return {
|
|
50
50
|
id: 'browseBookAgain',
|
|
51
|
-
text: 'Borrow again'
|
|
51
|
+
text: 'Borrow', // changed texts fro 'Borrow again' to 'Borrow'
|
|
52
52
|
className: 'primary',
|
|
53
53
|
analyticsEvent: {
|
|
54
54
|
category: this.analyticsCategories.browse,
|
|
@@ -20,15 +20,12 @@ export class LoanRenewHelper {
|
|
|
20
20
|
this.result = {
|
|
21
21
|
texts: null,
|
|
22
22
|
renewNow: false,
|
|
23
|
-
timeLeft: 0,
|
|
24
23
|
};
|
|
25
24
|
}
|
|
26
25
|
|
|
27
26
|
handleLoanRenew() {
|
|
28
27
|
try {
|
|
29
28
|
if (this.hasPageChanged) {
|
|
30
|
-
// when user click/flip on book page
|
|
31
|
-
this.showToastMessage();
|
|
32
29
|
return this.pageChanged(); // user clicked on page
|
|
33
30
|
}
|
|
34
31
|
return this.autoChecker(); // auto checker at 50th minute
|
|
@@ -64,6 +61,7 @@ export class LoanRenewHelper {
|
|
|
64
61
|
if (currentTime >= lastTimeFrame) {
|
|
65
62
|
this.result.texts = this.loanRenewMessage;
|
|
66
63
|
this.result.renewNow = true;
|
|
64
|
+
console.log('manually renewed by page click!');
|
|
67
65
|
}
|
|
68
66
|
|
|
69
67
|
this.setPageChangedTime();
|
|
@@ -96,10 +94,11 @@ export class LoanRenewHelper {
|
|
|
96
94
|
) {
|
|
97
95
|
this.result.texts = this.loanReturnWarning;
|
|
98
96
|
this.result.renewNow = false; // not viewed
|
|
99
|
-
|
|
97
|
+
console.log('not viewed!');
|
|
100
98
|
} else if (lastPageFlipTime >= lastFlipTimeFrame) {
|
|
101
99
|
this.result.texts = '';
|
|
102
100
|
this.result.renewNow = true; // viewed in last time frame
|
|
101
|
+
console.log('silently renewed!');
|
|
103
102
|
}
|
|
104
103
|
|
|
105
104
|
return this.result;
|
|
@@ -123,6 +122,8 @@ export class LoanRenewHelper {
|
|
|
123
122
|
* - show warninig msg when book is about to auto returned
|
|
124
123
|
*/
|
|
125
124
|
async showToastMessage() {
|
|
125
|
+
if (this.hideToast) return false;
|
|
126
|
+
|
|
126
127
|
const iaBookActions = document.querySelector('ia-book-actions').shadowRoot;
|
|
127
128
|
let toastTemplate = iaBookActions.querySelector('toast-template');
|
|
128
129
|
if (!toastTemplate) {
|
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,6 +74,7 @@ export default class IABookActions extends LitElement {
|
|
|
73
74
|
// loan auto renew
|
|
74
75
|
this.browserTimer = undefined;
|
|
75
76
|
this.loanRenewConfig = {};
|
|
77
|
+
this.suppressToast = false;
|
|
76
78
|
|
|
77
79
|
/**
|
|
78
80
|
* contains loan auto-renew response
|
|
@@ -82,6 +84,7 @@ export default class IABookActions extends LitElement {
|
|
|
82
84
|
this.loanRenewResult = {
|
|
83
85
|
texts: null,
|
|
84
86
|
renewNow: false,
|
|
87
|
+
timeLeft: 0
|
|
85
88
|
};
|
|
86
89
|
|
|
87
90
|
this._shadowRoot = this.attachShadow({ mode: 'open' });
|
|
@@ -234,12 +237,19 @@ export default class IABookActions extends LitElement {
|
|
|
234
237
|
// dispatched this event from bookreader page changed
|
|
235
238
|
window.addEventListener('BookReader:pageChanged', event => {
|
|
236
239
|
if (this.borrowType === 'browsed' && event.detail.hasPageChanged) {
|
|
240
|
+
console.log('actual page changed executed!')
|
|
237
241
|
this.autoLoanRenewChecker(true);
|
|
238
242
|
}
|
|
239
243
|
|
|
240
244
|
return nothing;
|
|
241
245
|
});
|
|
242
246
|
|
|
247
|
+
document.addEventListener('click', () => {
|
|
248
|
+
if (this.loanRenewHelper && this.loanRenewHelper.result.renewNow === false) {
|
|
249
|
+
this.suppressToast = true;
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
|
|
243
253
|
/**
|
|
244
254
|
* this event dispatched from timer-countdown component for:
|
|
245
255
|
* 1. user turned book page after showing auto-returned warning message
|
|
@@ -252,8 +262,8 @@ export default class IABookActions extends LitElement {
|
|
|
252
262
|
|
|
253
263
|
// show warning message with remaining time to auto returned it.
|
|
254
264
|
if (this.loanRenewHelper.result.renewNow === false) {
|
|
255
|
-
this.
|
|
256
|
-
this.
|
|
265
|
+
this.loanRenewResult.timeLeft = eventDetails.timeLeft;
|
|
266
|
+
this.showToastMessage();
|
|
257
267
|
}
|
|
258
268
|
});
|
|
259
269
|
}
|
|
@@ -271,6 +281,25 @@ export default class IABookActions extends LitElement {
|
|
|
271
281
|
this.loanRenewResult = this.loanRenewHelper.result;
|
|
272
282
|
}
|
|
273
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
|
+
|
|
274
303
|
async browseHasRenew() {
|
|
275
304
|
const loanTime = await this.localCache.get(`${this.identifier}-loanTime`);
|
|
276
305
|
const secondsLeft = (loanTime - new Date()) / 1000; // different in seconds
|
|
@@ -320,7 +349,8 @@ export default class IABookActions extends LitElement {
|
|
|
320
349
|
this.loanRenewResult.texts =
|
|
321
350
|
'This book has been automatically returned due to inactivity.';
|
|
322
351
|
|
|
323
|
-
this.
|
|
352
|
+
this.suppressToast = false;
|
|
353
|
+
this.showToastMessage();
|
|
324
354
|
}
|
|
325
355
|
|
|
326
356
|
/**
|
|
@@ -377,7 +407,7 @@ export default class IABookActions extends LitElement {
|
|
|
377
407
|
get timerCountdownTemplate() {
|
|
378
408
|
return this.borrowType === 'browsed'
|
|
379
409
|
? html`<timer-countdown
|
|
380
|
-
.
|
|
410
|
+
.timeLeftOnLoan=${Number(this.lendingStatus.secondsLeftOnLoan)}
|
|
381
411
|
.autoCheckAt=${this.loanRenewConfig.autoCheckAt}
|
|
382
412
|
></timer-countdown>`
|
|
383
413
|
: nothing;
|
|
@@ -418,7 +448,9 @@ export default class IABookActions extends LitElement {
|
|
|
418
448
|
async handleLoanAutoRenewed() {
|
|
419
449
|
if (this.loanRenewResult.renewNow) {
|
|
420
450
|
this.tokenPoller.disconnectedInterval();
|
|
421
|
-
|
|
451
|
+
|
|
452
|
+
this.suppressToast = false;
|
|
453
|
+
await this.showToastMessage();
|
|
422
454
|
await this.browseHasRenew();
|
|
423
455
|
await this.resetTimerCountState();
|
|
424
456
|
}
|
|
@@ -6,7 +6,7 @@ const container = ({
|
|
|
6
6
|
{
|
|
7
7
|
analyticsEvent: { action: 'Browse', category: 'Lending' },
|
|
8
8
|
className: 'ia-button primary',
|
|
9
|
-
text: 'Borrow
|
|
9
|
+
text: 'Borrow',
|
|
10
10
|
id: 'browseBook',
|
|
11
11
|
},
|
|
12
12
|
{
|
|
@@ -40,7 +40,7 @@ describe('<collapsible-action-group>', () => {
|
|
|
40
40
|
.true;
|
|
41
41
|
|
|
42
42
|
const primaryButton = primaryActionContainer.querySelector('.ia-button');
|
|
43
|
-
expect(primaryButton.innerText).to.equal('Borrow
|
|
43
|
+
expect(primaryButton.innerText).to.equal('Borrow');
|
|
44
44
|
});
|
|
45
45
|
|
|
46
46
|
it('check if loader is active and action-group is disabled', async () => {
|
|
@@ -25,7 +25,7 @@ describe('Get Lending Actions', () => {
|
|
|
25
25
|
|
|
26
26
|
expect(actions.primaryTitle).to.equal(bookTitles.available_1hr);
|
|
27
27
|
expect(actions.primaryActions.length).to.equal(3);
|
|
28
|
-
expect(actions.primaryActions[0].text).to.equal('Borrow
|
|
28
|
+
expect(actions.primaryActions[0].text).to.equal('Borrow');
|
|
29
29
|
expect(actions.primaryActions[1].text).to.equal('Borrow for 14 days');
|
|
30
30
|
expect(actions.secondaryActions[1].text).to.equal('Purchase at');
|
|
31
31
|
});
|
|
@@ -111,7 +111,7 @@ describe('Get Lending Actions', () => {
|
|
|
111
111
|
expect(actions.primaryTitle).to.equal(bookTitles.claim_waitlist);
|
|
112
112
|
expect(actions.primaryActions.length).to.equal(3);
|
|
113
113
|
expect(actions.primaryActions[0].text).to.equal('Borrow for 14 days');
|
|
114
|
-
expect(actions.primaryActions[1].text).to.equal('Borrow
|
|
114
|
+
expect(actions.primaryActions[1].text).to.equal('Borrow');
|
|
115
115
|
expect(actions.primaryActions[2].text).to.equal('Leave Waitlist');
|
|
116
116
|
expect(spy.called).to.be.true;
|
|
117
117
|
});
|
|
@@ -138,7 +138,7 @@ describe('Get Lending Actions', () => {
|
|
|
138
138
|
expect(actions.primaryTitle).to.equal(bookTitles.on_waitlist);
|
|
139
139
|
expect(actions.primaryActions.length).to.equal(2);
|
|
140
140
|
expect(actions.primaryActions[0].text).to.equal('Leave Waitlist');
|
|
141
|
-
expect(actions.primaryActions[1].text).to.equal('Borrow
|
|
141
|
+
expect(actions.primaryActions[1].text).to.equal('Borrow');
|
|
142
142
|
expect(claimSpy.called).to.be.false;
|
|
143
143
|
expect(waitlistSpy.called).to.be.true;
|
|
144
144
|
});
|
|
@@ -162,7 +162,7 @@ describe('Get Lending Actions', () => {
|
|
|
162
162
|
expect(actions.primaryTitle).to.equal(bookTitles.on_waitlist);
|
|
163
163
|
expect(actions.primaryActions.length).to.equal(2);
|
|
164
164
|
expect(actions.primaryActions[0].text).to.equal('Leave Waitlist');
|
|
165
|
-
expect(actions.primaryActions[1].text).to.equal('Borrow
|
|
165
|
+
expect(actions.primaryActions[1].text).to.equal('Borrow');
|
|
166
166
|
expect(waitlistSpy.called).to.be.true;
|
|
167
167
|
});
|
|
168
168
|
});
|
|
@@ -81,7 +81,7 @@ describe('Primary Actions data', () => {
|
|
|
81
81
|
);
|
|
82
82
|
const expectedPrimaryActions = [
|
|
83
83
|
{
|
|
84
|
-
text: 'Borrow for 1 hour'
|
|
84
|
+
text: 'Borrow', // changed from 'Borrow for 1 hour'
|
|
85
85
|
callback: () => {},
|
|
86
86
|
className: 'ia-button primary',
|
|
87
87
|
},
|
|
@@ -161,7 +161,7 @@ describe('Borrow status actions', () => {
|
|
|
161
161
|
|
|
162
162
|
// removed available_to_borrow from lending bar
|
|
163
163
|
expect(el.lendingStatus.available_to_borrow).to.be.false;
|
|
164
|
-
expect(el.primaryActions[0].text).to.equal('Borrow
|
|
164
|
+
expect(el.primaryActions[0].text).to.equal('Borrow');
|
|
165
165
|
});
|
|
166
166
|
|
|
167
167
|
it('Check action for borrowable book without user', async () => {
|
|
@@ -251,7 +251,7 @@ describe('Browsing expired status', () => {
|
|
|
251
251
|
await aTimeout(1500); // wait for 1.5 second
|
|
252
252
|
await el.updateComplete;
|
|
253
253
|
|
|
254
|
-
expect(el.primaryActions[0].text).to.equal('Borrow
|
|
254
|
+
expect(el.primaryActions[0].text).to.equal('Borrow');
|
|
255
255
|
});
|
|
256
256
|
|
|
257
257
|
it('Expiring book cancels interval & emits event', async () => {
|
|
@@ -286,7 +286,7 @@ describe('Browsing expired status', () => {
|
|
|
286
286
|
await aTimeout(1500); // wait for 1.5 sec
|
|
287
287
|
|
|
288
288
|
expect(eventReceived).to.equal(true);
|
|
289
|
-
expect(el.primaryActions[0].text).to.equal('Borrow
|
|
289
|
+
expect(el.primaryActions[0].text).to.equal('Borrow');
|
|
290
290
|
expect(el.tokenPoller.loanTokenInterval).to.equal(undefined);
|
|
291
291
|
});
|
|
292
292
|
});
|