@internetarchive/ia-book-actions 0.0.29-alpha50 → 0.0.29-alpha52

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
@@ -131,13 +131,11 @@
131
131
  iaBookActions.returnUrl = 'https://openlibrary.org';
132
132
  iaBookActions.localCache = localCache;
133
133
 
134
- let isDevBox = true;
135
- iaBookActions.loanRenewTimeConfig = {
136
- loanTotalTime: isDevBox ? 15 : 3600, // 60 second = 1 min
137
- loanRenewAtLast: isDevBox ? 10 : 3540, // at last 10th minute
138
- pageChangedInLast: isDevBox ? 5 : 900, // within last 15 second/minute
139
- isDevBox: isDevBox,
140
- };
134
+ let loanTotalTime2Min = { loanTotalTime: 20, loanRenewAtLast: 15, pageChangedInLast: 15 };
135
+ let loanTotalTime5Min = { loanTotalTime: 300, loanRenewAtLast: 180, pageChangedInLast: 15 };
136
+ let loanTotalTime1Hour = { loanTotalTime: 3600, loanRenewAtLast: 600, pageChangedInLast: 900 };
137
+
138
+ iaBookActions.loanRenewTimeConfig = loanTotalTime2Min;
141
139
 
142
140
  let lendingStatus1 = iaBookActions.lendingStatus;
143
141
  let secondsLeftOnLoan = iaBookActions.loanRenewTimeConfig.loanTotalTime;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@internetarchive/ia-book-actions",
3
- "version": "0.0.29-alpha50",
3
+ "version": "0.0.29-alpha52",
4
4
  "description": "Bookreader actions for borrowable book",
5
5
  "author": "Internet Archive",
6
6
  "license": "AGPL-3.0-only",
@@ -5,8 +5,8 @@ export default class TimerCountdown extends LitElement {
5
5
  static get properties() {
6
6
  return {
7
7
  secondsLeftOnLoan: { type: Number }, // in seconds
8
+ loanTotalTime: { type: Number }, // in seconds
8
9
  loanRenewAtLast: { type: Number }, // in seconds
9
- isDevBox: { type: Boolean },
10
10
  };
11
11
  }
12
12
 
@@ -14,11 +14,17 @@ export default class TimerCountdown extends LitElement {
14
14
  super();
15
15
 
16
16
  /**
17
- * seconds left in current time
17
+ * seconds left in current loan
18
18
  * @type {number}
19
19
  */
20
20
  this.secondsLeftOnLoan = 0;
21
21
 
22
+ /**
23
+ * total seconds a loan does have
24
+ * @type {number}
25
+ */
26
+ this.loanTotalTime = 0;
27
+
22
28
  /**
23
29
  * at the remaining time, we attempt to renew current loan
24
30
  * @type {number}
@@ -26,12 +32,16 @@ export default class TimerCountdown extends LitElement {
26
32
  this.loanRenewAtLast = 0;
27
33
 
28
34
  /**
29
- * @type {boolean}
35
+ * setInterval executed interval
36
+ * @type {number}
30
37
  */
31
- this.isDevBox = false;
32
-
33
- // private props
34
38
  this.timerInterval = undefined;
39
+
40
+ /**
41
+ * delay seconds in setInterval function
42
+ * @type {number}
43
+ */
44
+ this.timerExecutionSeconds = 0;
35
45
  }
36
46
 
37
47
  disconnectedCallback() {
@@ -46,17 +56,19 @@ export default class TimerCountdown extends LitElement {
46
56
  }
47
57
 
48
58
  timerCountdown() {
49
- // in-case of isDevBox, execute timer in each second instead of 60
50
- const timerIntervalSeconds = this.isDevBox ? 1 : 60;
59
+ // - let just execute setInterval in every 1 minute
60
+ this.timerExecutionSeconds = 60;
51
61
 
52
62
  this.timerInterval = setInterval(() => {
53
- // if this.isDevBox, just reduce seconds by 1 instead of 60 (1 min)
54
- this.secondsLeftOnLoan -= timerIntervalSeconds;
63
+ this.secondsLeftOnLoan -= this.timerExecutionSeconds;
55
64
  const secondsLeft = Math.round(this.secondsLeftOnLoan);
56
65
 
57
- // execute from last 10th minute to 0th minute
58
- // - 10th - to check if user has viewed
59
- // - till 0th - to show warning msg with remaining time to auto expired
66
+ /**
67
+ * execute from last 10th minute to 0th minute
68
+ * - 10th - to check if user has viewed
69
+ * - till 0th - to show warning msg with remaining time to auto expired
70
+ * @see IABookActions::bindLoanRenewEvents
71
+ */
60
72
  if (secondsLeft <= this.loanRenewAtLast) {
61
73
  this.dispatchEvent(
62
74
  new CustomEvent('IABookActions:loanRenew', {
@@ -71,11 +83,11 @@ export default class TimerCountdown extends LitElement {
71
83
  }
72
84
 
73
85
  // clear interval
74
- if (secondsLeft <= 1) {
86
+ if (secondsLeft <= 60) {
75
87
  clearInterval(this.timerInterval);
76
88
  window?.Sentry?.captureMessage(sentryLogs.clearOneHourTimer);
77
89
  }
78
- }, timerIntervalSeconds * 1000);
90
+ }, this.timerExecutionSeconds * 1000);
79
91
  }
80
92
 
81
93
  /**
@@ -85,17 +97,11 @@ export default class TimerCountdown extends LitElement {
85
97
  * @return string
86
98
  */
87
99
  get remainingTime() {
88
- let unitOfTime = 'second';
100
+ const unitOfTime = 'minute';
89
101
  let timeLeft = Math.round(this.secondsLeftOnLoan);
90
102
 
91
- if (this.isDevBox) {
92
- return `${timeLeft} seconds`;
93
- }
94
-
95
- if (timeLeft > 60) {
96
- unitOfTime = 'minute';
97
- timeLeft = Math.floor(timeLeft / 60);
98
- }
103
+ // convert time from second to minute
104
+ timeLeft = Math.ceil(timeLeft / 60);
99
105
 
100
106
  return timeLeft !== 1
101
107
  ? `${timeLeft} ${unitOfTime}s`
@@ -114,28 +114,26 @@ export class LoanRenewHelper {
114
114
 
115
115
  /**
116
116
  * Texts we want to show in toast template including remaining time
117
- * - eg. 1 minute, 50 seconds
117
+ * - eg. 1 minute, 2 minutes
118
118
  *
119
119
  * @param {String} texts
120
- * @param {Number} time
120
+ * @param {Number} secondsLeft
121
121
  *
122
122
  * @returns {String} // texts will be appear in toast template
123
123
  */
124
- getMessageTexts(texts, time) {
125
- let unitOfTime = 'second';
124
+ getMessageTexts(texts, secondsLeft) {
125
+ const unitOfTime = 'minute';
126
126
 
127
127
  let toastTexts = texts;
128
- let timeLeft = time;
128
+ let timeLeft = secondsLeft;
129
129
 
130
- if (timeLeft > 60) {
131
- unitOfTime = 'minute';
132
- timeLeft = Math.floor(timeLeft / 60);
133
- }
130
+ // convert time from second to minute
131
+ timeLeft = Math.ceil(timeLeft / 60);
134
132
 
135
- // replace remaining time
133
+ // replace #time variable with remaining time
136
134
  toastTexts = toastTexts?.replace(/#time/, timeLeft);
137
135
 
138
- // replace unitsOfTime eg second vs seconds / minute vs /minutes
136
+ // replace #unitsOfTime variable with minute/minutes
139
137
  return toastTexts?.replace(
140
138
  /#unitsOfTime/,
141
139
  timeLeft !== 1 ? `${unitOfTime}s` : unitOfTime
@@ -78,10 +78,9 @@ export default class IABookActions extends LitElement {
78
78
  * contains one hour auto-loan-renew time configuration
79
79
  *
80
80
  * @type {object} loanRenewTimeConfig
81
- * @property {number} loanTotalTime - number of seconds a loan should last
81
+ * @property {number} loanTotalTime - total seconds a loan does have
82
82
  * @property {number} loanRenewAtLast - check for loan renew at last
83
83
  * @property {number} pageChangedInLast - consider loan renew eligible if viewed new page
84
- * @property {boolean} isDevBox - testing flag
85
84
  */
86
85
  this.loanRenewTimeConfig = {};
87
86
 
@@ -335,11 +334,15 @@ export default class IABookActions extends LitElement {
335
334
  }
336
335
  await iaBookActions.appendChild(toastTemplate);
337
336
 
337
+ // if secondsLeft < 60, consider it 1 minute
338
+ let { secondsLeft } = this.loanRenewResult;
339
+ secondsLeft = secondsLeft > 60 ? secondsLeft : 60;
340
+
338
341
  const config = new ToastConfig();
339
342
  config.dismisOnClick = true;
340
343
  config.texts = this.loanRenewHelper?.getMessageTexts(
341
344
  this.loanRenewResult.texts,
342
- this.loanRenewResult.secondsLeft
345
+ secondsLeft
343
346
  );
344
347
 
345
348
  toastTemplate.showToast({
@@ -452,8 +455,8 @@ export default class IABookActions extends LitElement {
452
455
  return this.borrowType === 'browsed'
453
456
  ? html`<timer-countdown
454
457
  .secondsLeftOnLoan=${Number(this.lendingStatus.secondsLeftOnLoan)}
458
+ .loanTotalTime=${this.loanRenewTimeConfig.loanTotalTime}
455
459
  .loanRenewAtLast=${this.loanRenewTimeConfig.loanRenewAtLast}
456
- .isDevBox=${this.loanRenewTimeConfig.isDevBox}
457
460
  ></timer-countdown>`
458
461
  : nothing;
459
462
  }
@@ -16,7 +16,6 @@ describe('<timer-countdown>', () => {
16
16
  loanTotalTime: 14, // in seconds
17
17
  loanRenewAtLast: 5, // in seconds
18
18
  pageChangedInLast: 5, // in seconds
19
- isDevBox: true,
20
19
  },
21
20
  })
22
21
  );
@@ -33,7 +32,6 @@ describe('<timer-countdown>', () => {
33
32
  loanTotalTime: 14, // in seconds
34
33
  loanRenewAtLast: 5, // in seconds
35
34
  pageChangedInLast: 5, // in seconds
36
- isDevBox: true,
37
35
  },
38
36
  })
39
37
  );
@@ -8,7 +8,6 @@ const loanRenewTimeConfig = {
8
8
  loanTotalTime: 13,
9
9
  loanRenewAtLast: 10,
10
10
  pageChangedInLast: 12,
11
- isDevBox: true,
12
11
  };
13
12
 
14
13
  // localCache used for auto-loan-renew
@@ -81,7 +80,7 @@ describe('Loan Renew Determine', () => {
81
80
  );
82
81
 
83
82
  expect(toastMsg).to.be.equal(
84
- 'This book will be automatically returned in 1 minute unless you turn a page.'
83
+ 'This book will be automatically returned in 2 minutes unless you turn a page.'
85
84
  );
86
85
  });
87
86
  });