@internetarchive/ia-book-actions 0.0.12-2 → 0.0.13-2

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.0.12-2",
3
+ "version": "0.0.13-2",
4
4
  "description": "Bookreader actions for borrowable book",
5
5
  "author": "Internet Archive",
6
6
  "license": "AGPL-3.0-only",
@@ -15,7 +15,7 @@
15
15
  "format": "eslint --ext .js,.html . --fix --ignore-path .gitignore && prettier \"**/*.js\" --write --ignore-path .gitignore",
16
16
  "test": "yarn run lint && web-test-runner --coverage",
17
17
  "test:fast": "web-test-runner --coverage",
18
- "test:watch": "web-test-runner --watch"
18
+ "test:watch": "web-test-runner --watch --puppeteer"
19
19
  },
20
20
  "dependencies": {
21
21
  "@internetarchive/ia-icons": "^1.2.2",
@@ -25,6 +25,7 @@
25
25
  "@internetarchive/local-cache": "^0.2.0",
26
26
  "@internetarchive/modal-manager": "^0.1.0",
27
27
  "@internetarchive/shared-resize-observer": "^0.0.1",
28
+ "@web/test-runner-puppeteer": "^0.10.5",
28
29
  "lit-element": "^2.4.0",
29
30
  "lit-html": "^1.3.0"
30
31
  },
@@ -76,12 +76,7 @@ export class LoanTokenPoller {
76
76
  action,
77
77
  error: data => {
78
78
  this.disconnectedInterval(); // stop token fetch api
79
-
80
- if (isInitial) {
81
- this.errorCallback({ detail: { action, data } });
82
- } else {
83
- this.errorCallback({ detail: { action, data, isExpired: true } });
84
- }
79
+ this.errorCallback({ detail: { action, data } });
85
80
  },
86
81
  success: () => {
87
82
  if (isInitial) this.successCallback();
@@ -156,16 +156,21 @@ export default class IABookActions extends LitElement {
156
156
  });
157
157
 
158
158
  this.borrowType = actions.borrowType;
159
+
160
+ const hasExpired =
161
+ 'browsingExpired' in this.lendingStatus &&
162
+ this.lendingStatus?.browsingExpired;
163
+ if (hasExpired) {
164
+ this.tokenPoller?.disconnectedInterval();
165
+ return;
166
+ }
167
+
159
168
  if (this.borrowType === 'browsed') {
160
169
  // start timer for browsed.
161
170
  // when browse is completed, we shows browse-again button
162
171
  this.startBrowseTimer();
163
172
  }
164
173
 
165
- if (this.lendingStatus.browsingExpired) {
166
- this.tokenPoller.disconnectedInterval();
167
- }
168
-
169
174
  // early return if not borrowed or no action-bar
170
175
  if (!this.borrowType || this.barType === 'title') {
171
176
  this.lendingBarPostInit();
@@ -265,7 +270,6 @@ export default class IABookActions extends LitElement {
265
270
  * @param {Object} event - The employee who is responsible for the project
266
271
  * @param {string} event.detail.action - action when error occurred like 'browseBook', 'borrowBook'
267
272
  * @param {string} event.detail.data.error - error message
268
- * @param {string} event.detail.data.isExpired - triggered when loan is expired
269
273
  */
270
274
  handleLendingActionError(event) {
271
275
  // toggle activity loader
@@ -273,9 +277,8 @@ export default class IABookActions extends LitElement {
273
277
 
274
278
  const action = event?.detail?.action;
275
279
  const errorMsg = event?.detail?.data?.error;
276
- const loanExpired = event?.detail?.data?.isExpired;
277
280
 
278
- if (errorMsg) this.showErrorModal(errorMsg, action, loanExpired);
281
+ if (errorMsg) this.showErrorModal(errorMsg, action);
279
282
 
280
283
  // update action bar state if book is not available to browse or borrow.
281
284
  if (errorMsg && errorMsg.match(/not available to borrow/gm)) {
@@ -296,7 +299,7 @@ export default class IABookActions extends LitElement {
296
299
  }
297
300
 
298
301
  /* show error message if something went wrong */
299
- async showErrorModal(errorMsg, action, loanExpired) {
302
+ async showErrorModal(errorMsg, action) {
300
303
  this.disableActionGroup = false;
301
304
 
302
305
  // check if this.modal passed as prop
@@ -314,17 +317,25 @@ export default class IABookActions extends LitElement {
314
317
  title: 'Lending error',
315
318
  message: errorMsg,
316
319
  headerColor: '#d9534f',
320
+ showCloseButton: true,
317
321
  });
318
322
 
319
323
  if (action === 'create_token') {
320
- modalConfig.closeOnBackdropClick = false;
321
- if (loanExpired) {
322
- modalConfig.showCloseButton = true;
323
- modalConfig.message = this.loanExpiredMessage;
324
- } else {
325
- modalConfig.showCloseButton = false;
326
- modalConfig.message = this.loanErrorMessage;
327
- }
324
+ const refreshButton = html`<button
325
+ style="background:none;font-size:inherit;border:0;padding:0;color:#0000ee;cursor:pointer;text-decoration:underline"
326
+ @click=${() => window.location.reload(true)}
327
+ >
328
+ refresh
329
+ </button>`;
330
+
331
+ modalConfig.message = html` Uh oh, something went wrong trying to access
332
+ this book.<br />
333
+ Please ${refreshButton} to try again or send us an email to
334
+ <a
335
+ href="mailto:info@archive.org?subject=Help: cannot access my borrowed book: ${this
336
+ .identifier}"
337
+ >info@archive.org</a
338
+ >`;
328
339
  }
329
340
 
330
341
  this.modal.showModal({
@@ -332,29 +343,6 @@ export default class IABookActions extends LitElement {
332
343
  });
333
344
  }
334
345
 
335
- /* eslint-disable class-methods-use-this */
336
- get loanExpiredMessage() {
337
- return html`Your current loan is expired, please close this model and click
338
- on <strong>Borrow Again</strong> button to continue reading this book`;
339
- }
340
-
341
- get loanErrorMessage() {
342
- const refreshButton = html`<button
343
- style="background:none;font-size:inherit;border:0;padding:0;color:#0000ee;cursor:pointer;text-decoration:underline"
344
- @click=${() => window.location.reload(true)}
345
- >
346
- refresh
347
- </button>`;
348
-
349
- return html`Uh oh, something went wrong trying to access this book.<br />
350
- Please ${refreshButton} to try again or send us an email to
351
- <a
352
- href="mailto:info@archive.org?subject=Help: cannot access my borrowed book: ${this
353
- .identifier}"
354
- >info@archive.org</a
355
- >`;
356
- }
357
-
358
346
  get iconClass() {
359
347
  return this.width <= mobileContainerWidth ? 'mobile' : 'desktop';
360
348
  }
@@ -251,10 +251,41 @@ describe('Browsing expired status', () => {
251
251
  );
252
252
 
253
253
  await aTimeout(1500); // wait for 1.5 second
254
- el.updateComplete;
254
+ await el.updateComplete;
255
+
256
+ expect(el.primaryTitle).contains('Your loan has expired.');
257
+ expect(el.primaryActions[0].text).to.equal('Borrow again');
258
+ });
259
+
260
+ it('Expiring book cancels interval', async () => {
261
+ const baseStatus = {
262
+ is_lendable: true,
263
+ user_has_browsed: false,
264
+ browseHasExpired: false,
265
+ secondsLeftOnLoan: 1,
266
+ };
267
+ const el = await fixture(
268
+ container({
269
+ userid: '@userid',
270
+ lendingStatus: baseStatus,
271
+ })
272
+ );
273
+ const browsingStatus = { ...baseStatus, user_has_browsed: true };
274
+ el.lendingStatus = browsingStatus;
275
+ await el.updateComplete;
276
+
277
+ expect(el.primaryTitle).contains('Borrow ends at ');
278
+ expect(el.primaryActions[0].text).to.equal('Return now');
279
+ expect(el.tokenPoller.loanTokenInterval).to.not.equal(undefined);
280
+
281
+ const expiredStatus = { ...browsingStatus, browsingExpired: true };
282
+ el.lendingStatus = expiredStatus;
283
+ await el.updateComplete;
284
+ await aTimeout(1500); // wait for 1.5 sec
255
285
 
256
286
  expect(el.primaryTitle).contains('Your loan has expired.');
257
287
  expect(el.primaryActions[0].text).to.equal('Borrow again');
288
+ expect(el.tokenPoller.loanTokenInterval).to.equal(undefined);
258
289
  });
259
290
  });
260
291