@internetarchive/ia-book-actions 0.0.7-6 → 0.0.7-9

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
@@ -104,6 +104,13 @@
104
104
  const modalConfig = new ModalConfig();
105
105
  modalConfig.headerColor = '#d9534f';
106
106
 
107
+ let lendingBarPostInit = function lendingBarPostInit() {
108
+ setTimeout(function() {
109
+ console.log('executed');
110
+ // emitLendingActionBarInitEvent();
111
+ // brInit();
112
+ }, 100);
113
+ }
107
114
  // set defaultLendingStatus for unavailable (without borrowables) book
108
115
  iaBookActions.userid = userid;
109
116
  iaBookActions.identifier = identifier;
@@ -112,6 +119,7 @@
112
119
  iaBookActions.bwbPurchaseUrl = '';
113
120
  iaBookActions.barType = '';
114
121
  iaBookActions.modalConfig = modalConfig;
122
+ iaBookActions.lendingBarPostInit = lendingBarPostInit;
115
123
 
116
124
  let lendingStatus1 = iaBookActions.lendingStatus;
117
125
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@internetarchive/ia-book-actions",
3
- "version": "0.0.7-6",
3
+ "version": "0.0.7-9",
4
4
  "description": "Bookreader actions for borrowable book",
5
5
  "author": "Internet Archive",
6
6
  "license": "AGPL-3.0-only",
@@ -27,7 +27,6 @@ export class CollapsibleActionGroup extends ActionsHandler {
27
27
  hasAdminAccess: { type: Boolean },
28
28
  dropdownArrow: { type: String },
29
29
  disabled: { type: Boolean },
30
- borrowType: { type: String },
31
30
  };
32
31
  }
33
32
 
@@ -46,15 +45,6 @@ export class CollapsibleActionGroup extends ActionsHandler {
46
45
  this.title = '';
47
46
  this.loaderIcon = 'https://archive.org/upload/images/tree/loading.gif';
48
47
  this.disabled = false;
49
- this.borrowType = ''; // (browsed|borrowed)
50
- this.consecutiveLoanCounts = 1; // consecutive loan count
51
- }
52
-
53
- firstUpdated() {
54
- // this is execute to fetch loan token
55
- if (this.borrowType) {
56
- this.emitEnableBookAccess();
57
- }
58
48
  }
59
49
 
60
50
  updated(changed) {
@@ -63,46 +53,6 @@ export class CollapsibleActionGroup extends ActionsHandler {
63
53
  }
64
54
  }
65
55
 
66
- /**
67
- * Dispatches event when patron is borrowing book.
68
- * Notes borrow type, and consecutive borrow counts
69
- *
70
- * @fires CollapsibleActionGroup#enableBookAccess
71
- */
72
- async emitEnableBookAccess() {
73
- // send consecutiveLoanCounts for browsed books only.
74
- if (this.borrowType === 'browsed') {
75
- try {
76
- const existingCount = await this.localCache.get(
77
- 'consecutive-loan-count'
78
- );
79
- if (existingCount !== undefined) {
80
- this.consecutiveLoanCounts = existingCount.value ?? 1;
81
- }
82
- } catch (error) {
83
- console.log(error);
84
- }
85
- }
86
-
87
- // event category and action for browsing book access
88
- const eventCategory = `${this.borrowType}BookAccess`;
89
- const eventAction = `${
90
- this.borrowType === 'browsed' ? 'BrowseCounts-' : 'Counts-'
91
- }${this.consecutiveLoanCounts}`;
92
-
93
- this.dispatchEvent(
94
- new CustomEvent('enableBookAccess', {
95
- detail: {
96
- event: {
97
- category: eventCategory, // browsedBookAccess | borrowedBookAccess
98
- action: eventAction, // (BrowseCounts-N|Counts-N)
99
- },
100
- borrowType: this.borrowType,
101
- },
102
- })
103
- );
104
- }
105
-
106
56
  /**
107
57
  * merge primaryActions and secondaryActions into dropdown
108
58
  */
@@ -17,17 +17,10 @@ export default class ActionsHandler extends LitElement {
17
17
  super();
18
18
  this.identifier = identifier;
19
19
  this.ajaxTimeout = 7000;
20
- this.loanTokenPollingDelay =
21
- window.location.pathname === '/demo/' ? 2000 : 120000; // 120000 ms = 2 min
22
- this.loanTokenInterval = undefined;
23
20
  this.localCache = new LocalCache();
24
21
  this.bindEvents();
25
22
  }
26
23
 
27
- disconnectedCallback() {
28
- this.loanTokenInterval = undefined;
29
- }
30
-
31
24
  sendEvent(eventCategory, eventAction) {
32
25
  // eslint-disable-next-line no-console
33
26
  console?.log('Book action: ', { eventCategory, eventAction });
@@ -102,23 +95,6 @@ export default class ActionsHandler extends LitElement {
102
95
  const { category, action } = detail.event;
103
96
  this.sendEvent(category, action);
104
97
  });
105
-
106
- this.addEventListener('enableBookAccess', ({ detail }) => {
107
- const borrowType = detail?.borrowType;
108
-
109
- // fetch loan token for browsed/borrowed book and set an interval
110
- if (borrowType) {
111
- this.loanTokenInterval = setInterval(() => {
112
- this.handleLoanTokenPoller();
113
- }, this.loanTokenPollingDelay);
114
-
115
- const { category, action } = detail?.event;
116
- this.sendEvent(category, action);
117
- } else {
118
- // if book is not browsed, just clear token polling interval
119
- clearInterval(this.loanTokenInterval);
120
- }
121
- });
122
98
  }
123
99
 
124
100
  handleBrowseIt() {
@@ -202,17 +178,6 @@ export default class ActionsHandler extends LitElement {
202
178
  });
203
179
  }
204
180
 
205
- handleLoanTokenPoller() {
206
- const action = 'create_token';
207
- ActionsHandlerService({
208
- identifier: this.identifier,
209
- action,
210
- error: data => {
211
- this.dispatchActionError(action, data);
212
- clearInterval(this.loanTokenInterval); // stop token fetch api
213
- },
214
- });
215
- }
216
181
 
217
182
  /**
218
183
  * Dispatches event when an error occured on action
@@ -0,0 +1,103 @@
1
+ import { LitElement } from 'lit-element';
2
+ import { LocalCache } from '@internetarchive/local-cache';
3
+
4
+ import ActionsHandlerService from './actions-handler/actions-handler-service.js';
5
+
6
+ /**
7
+ * This class is used to create loan token for borrowed books
8
+ *
9
+ * ActionsHandlerService is a function being used to execute
10
+ */
11
+ export class LoanTokenPoller extends LitElement {
12
+ constructor() {
13
+ super();
14
+ this.identifier = '';
15
+ this.loanTokenPollingDelay = window.location.pathname === '/demo/' ? 2000 : 120000; // 120000 ms = 2 min
16
+ this.loanTokenInterval = undefined;
17
+ this.localCache = new LocalCache();
18
+ }
19
+
20
+ disconnectedCallback() {
21
+ this.loanTokenInterval = undefined;
22
+ }
23
+
24
+ async enableBookAccess(identifier, borrowType) {
25
+ this.identifier = identifier
26
+ let consecutiveLoanCounts = 1;
27
+
28
+ if (borrowType) {
29
+ // send consecutiveLoanCounts for browsed books only.
30
+ if (borrowType === 'browsed') {
31
+ try {
32
+ const existingCount = await this.localCache.get(
33
+ 'consecutive-loan-count'
34
+ );
35
+ consecutiveLoanCounts = existingCount ?? 1;
36
+ } catch (error) {
37
+ console.log(error);
38
+ }
39
+ }
40
+
41
+ // Do an initial token, then set an interval
42
+ this.handleLoanTokenPoller();
43
+
44
+ this.loanTokenInterval = setInterval(() => {
45
+ this.handleLoanTokenPoller();
46
+ }, this.loanTokenPollingDelay);
47
+
48
+ // event category and action for browsing book access
49
+ const category = `${borrowType}BookAccess`;
50
+ const action = `${
51
+ borrowType === 'browsed' ? 'BrowseCounts-' : 'Counts-'
52
+ }${consecutiveLoanCounts}`;
53
+ console.log(consecutiveLoanCounts)
54
+ this.sendEvent(category, action);
55
+ } else {
56
+ // if book is not browsed, just clear token polling interval
57
+ clearInterval(this.loanTokenInterval);
58
+ }
59
+ }
60
+
61
+ sendEvent(eventCategory, eventAction) {
62
+ // eslint-disable-next-line no-console
63
+ console?.log('Book action: ', { eventCategory, eventAction });
64
+ window?.archive_analytics?.send_event_no_sampling(
65
+ eventCategory,
66
+ eventAction,
67
+ `identifier=${this.identifier}`
68
+ );
69
+ }
70
+
71
+ async handleLoanTokenPoller() {
72
+ const action = 'create_token';
73
+ // await new Promise(r => setTimeout(r, 3000));
74
+ ActionsHandlerService({
75
+ identifier: this.identifier,
76
+ action,
77
+ error: data => {
78
+ console.log('error', data);
79
+ this.handleLendingActionError({detail: { action, data }})
80
+ // this.dispatchActionError(action, data);
81
+ clearInterval(this.loanTokenInterval); // stop token fetch api
82
+ },
83
+ });
84
+ }
85
+
86
+ /**
87
+ * Dispatches event when an error occured on action
88
+ * Notes:- toggle <collapsible-action-group> visibility (enable/disable).
89
+ *
90
+ * @param { String } action - name of action like browse_book, borrow_book
91
+ * @param { Object } data - erroneous response from api call
92
+ *
93
+ * @fires ActionsHandler#lendingActionError
94
+ */
95
+ dispatchActionError(action, data = {}) {
96
+ console.log('disptaced');
97
+ this.dispatchEvent(
98
+ new CustomEvent('lendingActionError', {
99
+ detail: { action, data },
100
+ })
101
+ );
102
+ }
103
+ }
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable camelcase */
2
- import { html, css, LitElement } from 'lit-element';
2
+ import { html, css } from 'lit-element';
3
3
 
4
4
  import { SharedResizeObserver } from '@internetarchive/shared-resize-observer';
5
5
  import { ModalConfig } from '@internetarchive/modal-manager';
@@ -11,8 +11,9 @@ import './components/info-icon.js';
11
11
 
12
12
  import GetLendingActions from './core/services/get-lending-actions.js';
13
13
  import { mobileContainerWidth } from './core/config/constants.js';
14
+ import { LoanTokenPoller } from './core/services/loan-token-poller.js';
14
15
 
15
- export default class IABookActions extends LitElement {
16
+ export default class IABookActions extends LoanTokenPoller {
16
17
  static get properties() {
17
18
  return {
18
19
  userid: { type: String },
@@ -21,6 +22,7 @@ export default class IABookActions extends LitElement {
21
22
  lendingStatus: { type: Object },
22
23
  width: { type: Number },
23
24
  bwbPurchaseUrl: { type: String },
25
+ lendingBarPostInit: { type: Function },
24
26
  barType: { type: String },
25
27
  sharedObserver: { attribute: false },
26
28
  disableActionGroup: { type: Boolean },
@@ -36,6 +38,7 @@ export default class IABookActions extends LitElement {
36
38
  this.lendingStatus = {};
37
39
  this.width = 0;
38
40
  this.bwbPurchaseUrl = '';
41
+ this.lendingBarPostInit = () => {};
39
42
  this.barType = 'action'; // 'title'|'action'
40
43
  this.sharedObserver = undefined;
41
44
  this.disableActionGroup = false;
@@ -47,6 +50,8 @@ export default class IABookActions extends LitElement {
47
50
  this.primaryColor = 'primary';
48
51
  this.secondaryActions = [];
49
52
  this.lendingOptions = {};
53
+ this.borrowType = ''; // (browsed|borrowed)
54
+ this.consecutiveLoanCounts = 1; // consecutive loan count
50
55
  }
51
56
 
52
57
  disconnectedCallback() {
@@ -58,8 +63,6 @@ export default class IABookActions extends LitElement {
58
63
  this.sharedObserver = new SharedResizeObserver();
59
64
  this.setupResizeObserver();
60
65
  }
61
-
62
- this.setupLendingToolbarActions();
63
66
  }
64
67
 
65
68
  updated(changed) {
@@ -153,14 +156,27 @@ export default class IABookActions extends LitElement {
153
156
  // when browse is completed, we shows browse-again button
154
157
  this.startBrowseTimer();
155
158
  }
159
+
160
+ // enable access of borrowed/browsed books
161
+ if (this.borrowType) {
162
+ await this.enableBookAccess(this.identifier, this.borrowType);
163
+ }
164
+
165
+ /**
166
+ * this callback function use to:-
167
+ * - disptach lenfingFlow::PostInit event
168
+ * - initialize bookreader using br.init()
169
+ */
170
+ console.log(this.lendingBarPostInit);
171
+ this.lendingBarPostInit;
156
172
  }
157
173
 
158
174
  render() {
159
- return html`
160
- <section class="lending-wrapper">
161
- ${this.barType === 'title' ? this.bookTitleBar : this.bookActionBar}
162
- </section>
163
- `;
175
+ if (this.barType === 'title') {
176
+ return html`<section class="lending-wrapper">${this.bookTitleBar}</section>`;
177
+ }
178
+
179
+ return html`<section class="lending-wrapper">${this.bookActionBar}</section>`;
164
180
  }
165
181
 
166
182
  get bookTitleBar() {