@internetarchive/ia-book-actions 0.0.7-12 → 0.0.7-13

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
@@ -116,9 +116,9 @@
116
116
  iaBookActions.bookTitle = 'Contemporary Black biography. Volume 39 : profiles from the interContemporary Black biography. Volume 39';
117
117
  iaBookActions.lendingStatus = canBrowseAndBorrow;
118
118
  iaBookActions.bwbPurchaseUrl = '';
119
- // iaBookActions.barType = '';
120
119
  iaBookActions.modalConfig = modalConfig;
121
120
  iaBookActions.lendingBarPostInit = lendingBarPostInit;
121
+ iaBookActions.tokenDelay = 2000; // 2 seconds
122
122
 
123
123
  let lendingStatus1 = iaBookActions.lendingStatus;
124
124
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@internetarchive/ia-book-actions",
3
- "version": "0.0.7-12",
3
+ "version": "0.0.7-13",
4
4
  "description": "Bookreader actions for borrowable book",
5
5
  "author": "Internet Archive",
6
6
  "license": "AGPL-3.0-only",
@@ -1,6 +1,4 @@
1
- import { LitElement } from 'lit-element';
2
1
  import { LocalCache } from '@internetarchive/local-cache';
3
-
4
2
  import ActionsHandlerService from './actions-handler/actions-handler-service.js';
5
3
 
6
4
  /**
@@ -8,29 +6,30 @@ import ActionsHandlerService from './actions-handler/actions-handler-service.js'
8
6
  *
9
7
  * ActionsHandlerService is a function being used to execute
10
8
  */
11
- export class LoanTokenPoller extends LitElement {
12
- constructor() {
13
- super();
14
- this.identifier = '';
15
- this.callback = () => {}; // callback function to be called after loan token is created
16
- this.loanTokenPollingDelay =
17
- window.location.pathname === '/demo/' ? 2000 : 120000; // 120000 ms = 2 min
9
+ export class LoanTokenPoller {
10
+ constructor(id, borrowType, successCallback, errorCallback, pollerDelay) {
11
+ this.identifier = id;
12
+ this.borrowType = borrowType;
13
+ this.successCallback = successCallback; // callback function to be called after loan token is created
14
+ this.errorCallback = errorCallback; // callback function to be called after loan token is created
15
+ this.pollerDelay = pollerDelay; // value in ms (1000 ms = 1 sec)
16
+
18
17
  this.loanTokenInterval = undefined;
19
18
  this.localCache = new LocalCache();
19
+ this.enableBookAccess();
20
20
  }
21
21
 
22
- disconnectedCallback() {
22
+ disconnectedInterval() {
23
+ clearInterval(this.loanTokenInterval);
23
24
  this.loanTokenInterval = undefined;
24
25
  }
25
26
 
26
- async enableBookAccess(identifier, borrowType, callback) {
27
- this.identifier = identifier;
28
- this.callback = callback;
27
+ async enableBookAccess() {
29
28
  let consecutiveLoanCounts = 1;
30
29
 
31
- if (borrowType) {
30
+ if (this.borrowType) {
32
31
  // send consecutiveLoanCounts for browsed books only.
33
- if (borrowType === 'browsed') {
32
+ if (this.borrowType === 'browsed') {
34
33
  try {
35
34
  const existingCount = await this.localCache.get(
36
35
  'consecutive-loan-count'
@@ -42,22 +41,22 @@ export class LoanTokenPoller extends LitElement {
42
41
  }
43
42
 
44
43
  // Do an initial token, then set an interval
45
- this.handleLoanTokenPoller();
44
+ this.handleLoanTokenPoller(true);
46
45
 
47
46
  this.loanTokenInterval = setInterval(() => {
48
47
  this.handleLoanTokenPoller();
49
- }, this.loanTokenPollingDelay);
48
+ }, this.pollerDelay);
50
49
 
51
50
  // event category and action for browsing book access
52
- const category = `${borrowType}BookAccess`;
51
+ const category = `${this.borrowType}BookAccess`;
53
52
  const action = `${
54
- borrowType === 'browsed' ? 'BrowseCounts-' : 'Counts-'
53
+ this.borrowType === 'browsed' ? 'BrowseCounts-' : 'Counts-'
55
54
  }${consecutiveLoanCounts}`;
56
55
 
57
56
  this.sendEvent(category, action);
58
57
  } else {
59
58
  // if book is not browsed, just clear token polling interval
60
- clearInterval(this.loanTokenInterval);
59
+ this.disconnectedInterval(); // stop token fetch api
61
60
  }
62
61
  }
63
62
 
@@ -71,18 +70,17 @@ export class LoanTokenPoller extends LitElement {
71
70
  );
72
71
  }
73
72
 
74
- async handleLoanTokenPoller() {
73
+ async handleLoanTokenPoller(isInitial = false) {
75
74
  const action = 'create_token';
76
75
  ActionsHandlerService({
77
76
  identifier: this.identifier,
78
77
  action,
79
78
  error: data => {
80
- this.handleLendingActionError({ detail: { action, data } });
81
- clearInterval(this.loanTokenInterval); // stop token fetch api
82
- this.callback();
79
+ this.disconnectedInterval(); // stop token fetch api
80
+ this.errorCallback({ detail: { action, data } });
83
81
  },
84
82
  success: () => {
85
- this.callback();
83
+ if (isInitial) this.successCallback();
86
84
  },
87
85
  });
88
86
  }
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable camelcase */
2
- import { html, css } from 'lit-element';
2
+ import { html, css, LitElement } from 'lit-element';
3
3
 
4
4
  import { SharedResizeObserver } from '@internetarchive/shared-resize-observer';
5
5
  import { ModalConfig } from '@internetarchive/modal-manager';
@@ -13,7 +13,7 @@ import GetLendingActions from './core/services/get-lending-actions.js';
13
13
  import { mobileContainerWidth } from './core/config/constants.js';
14
14
  import { LoanTokenPoller } from './core/services/loan-token-poller.js';
15
15
 
16
- export default class IABookActions extends LoanTokenPoller {
16
+ export default class IABookActions extends LitElement {
17
17
  static get properties() {
18
18
  return {
19
19
  userid: { type: String },
@@ -22,11 +22,15 @@ export default class IABookActions extends LoanTokenPoller {
22
22
  lendingStatus: { type: Object },
23
23
  width: { type: Number },
24
24
  bwbPurchaseUrl: { type: String },
25
- lendingBarPostInit: { type: Function },
25
+ lendingBarPostInit: {
26
+ type: Function,
27
+ attribute: false,
28
+ },
26
29
  barType: { type: String },
27
30
  sharedObserver: { attribute: false },
28
31
  disableActionGroup: { type: Boolean },
29
32
  modal: { Object },
33
+ tokenDelay: { type: Number },
30
34
  };
31
35
  }
32
36
 
@@ -43,6 +47,7 @@ export default class IABookActions extends LoanTokenPoller {
43
47
  this.sharedObserver = undefined;
44
48
  this.disableActionGroup = false;
45
49
  this.modal = undefined;
50
+ this.tokenDelay = 120000; // 2 minutes
46
51
 
47
52
  // private props
48
53
  this.primaryActions = [];
@@ -160,15 +165,34 @@ export default class IABookActions extends LoanTokenPoller {
160
165
  /**
161
166
  * enable access of borrowed/browsed books
162
167
  *
163
- * this.lendingBarPostInit() callback function use to:-
164
- * - disptach lendingFlow::PostInit event
165
- * - initialize bookreader using br.init()
168
+ * LoanTokenPoller is a class that polls the loan token
169
+ * it takes 5 params
170
+ * 1. this.identifier
171
+ * 2. this.borrowType
172
+ * 3. successCallback - it used to
173
+ * - disptach lendingFlow::PostInit event
174
+ * - initialize bookreader using br.init()
175
+ * 4. errorCallback
176
+ * 5. tokenPollerDelay
166
177
  */
167
178
  if (this.barType === 'action') {
168
- this.enableBookAccess(
179
+ if (this.tokenPoller) {
180
+ this.tokenPoller.disconnectedInterval();
181
+ }
182
+
183
+ const successCallback = () => {
184
+ this.lendingBarPostInit();
185
+ };
186
+ const errorCallback = eventObj => {
187
+ this.handleLendingActionError(eventObj);
188
+ };
189
+
190
+ this.tokenPoller = new LoanTokenPoller(
169
191
  this.identifier,
170
192
  this.borrowType,
171
- this.lendingBarPostInit
193
+ successCallback,
194
+ errorCallback,
195
+ this.tokenDelay // 1000 ms = 1 sec
172
196
  );
173
197
  }
174
198
  }
@@ -279,11 +303,18 @@ export default class IABookActions extends LoanTokenPoller {
279
303
  });
280
304
 
281
305
  if (action === 'create_token') {
306
+ const refreshButton = html`<button
307
+ style="background:none;font-size:inherit;border:0;padding:0;color:#0000ee;cursor:pointer;text-decoration:underline"
308
+ @click=${() => window.location.reload(true)}
309
+ >
310
+ refresh
311
+ </button>`;
312
+
282
313
  modalConfig.closeOnBackdropClick = false;
283
314
  modalConfig.showCloseButton = false;
284
315
  modalConfig.message = html` Uh oh, something went wrong trying to access
285
316
  this book.<br />
286
- Please <a href="/">refresh</a> to try again or send us an email to
317
+ Please ${refreshButton} to try again or send us an email to
287
318
  <a
288
319
  href="mailto:info@archive.org?subject=Help: cannot access my borrowed book: ${this
289
320
  .identifier}"
@@ -0,0 +1,40 @@
1
+ import { expect } from '@open-wc/testing';
2
+ import Sinon from 'sinon';
3
+
4
+ import { LocalCache } from '@internetarchive/local-cache';
5
+ import { LoanTokenPoller } from '../../../src/core/services/loan-token-poller.js';
6
+
7
+ describe('Get Loan Token', () => {
8
+ it('get loan token for browsed books', async () => {
9
+ const localCacheStub = new LocalCache();
10
+ const localCacheGetSpy = Sinon.spy(localCacheStub, 'get');
11
+
12
+ const tokenPoller = new LoanTokenPoller(
13
+ 'identifier1',
14
+ 'browsed',
15
+ () => {
16
+ console.log('success callback is executed!');
17
+ },
18
+ () => {
19
+ console.log('error callback is executed!');
20
+ },
21
+ 2000 // 2 minutes'
22
+ );
23
+ const successCallbackSpy = Sinon.stub(tokenPoller, 'successCallback');
24
+ successCallbackSpy();
25
+ expect(successCallbackSpy.callCount).to.equal(1);
26
+
27
+ localCacheGetSpy();
28
+ expect(localCacheGetSpy.callCount).to.equal(1);
29
+
30
+ expect(tokenPoller.localCache.namespace).to.equal('LocalCache');
31
+ expect(tokenPoller.localCache.get).to.be.a('function');
32
+
33
+ tokenPoller.disconnectedInterval(); // clear token poller interval
34
+ expect(tokenPoller.loanTokenInterval).to.equal(undefined);
35
+
36
+ tokenPoller.handleLoanTokenPoller(true);
37
+ expect(tokenPoller.errorCallback).to.be.a('function');
38
+ expect(tokenPoller.successCallback).have;
39
+ });
40
+ });