@internetarchive/ia-book-actions 0.0.16-1 → 0.0.16-4

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
@@ -119,7 +119,7 @@
119
119
  iaBookActions.modalConfig = modalConfig;
120
120
  iaBookActions.lendingBarPostInit = lendingBarPostInit;
121
121
  iaBookActions.tokenDelay = 2000; // 2 seconds
122
- iaBookActions.loansUrl = 'https://openlibrary.org';
122
+ iaBookActions.returnUrl = 'https://openlibrary.org';
123
123
 
124
124
  let lendingStatus1 = iaBookActions.lendingStatus;
125
125
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@internetarchive/ia-book-actions",
3
- "version": "0.0.16-1",
3
+ "version": "0.0.16-4",
4
4
  "description": "Bookreader actions for borrowable book",
5
5
  "author": "Internet Archive",
6
6
  "license": "AGPL-3.0-only",
@@ -27,7 +27,7 @@ export class CollapsibleActionGroup extends ActionsHandler {
27
27
  hasAdminAccess: { type: Boolean },
28
28
  dropdownArrow: { type: String },
29
29
  disabled: { type: Boolean },
30
- loansUrl: { type: String },
30
+ returnUrl: { type: String },
31
31
  };
32
32
  }
33
33
 
@@ -46,7 +46,7 @@ export class CollapsibleActionGroup extends ActionsHandler {
46
46
  this.title = '';
47
47
  this.loaderIcon = 'https://archive.org/upload/images/tree/loading.gif';
48
48
  this.disabled = false;
49
- this.loansUrl = '';
49
+ this.returnUrl = '';
50
50
  }
51
51
 
52
52
  updated(changed) {
@@ -4,7 +4,7 @@
4
4
  * Helper to call loan service
5
5
  * @param {Object} options
6
6
  */
7
- export default function ActionsHandlerService(options) {
7
+ export default async function ActionsHandlerService(options) {
8
8
  const option = {
9
9
  action: null,
10
10
  identifier: '',
@@ -16,7 +16,13 @@ export default function ActionsHandlerService(options) {
16
16
  let baseHost = '/services/loans/loan';
17
17
  const location = window?.location;
18
18
 
19
- // return intentional error on dev environment
19
+ console.log('failed-to-fecth-loan-data fix is here');
20
+
21
+ // return error reponse when not production and has ?error=true param...
22
+ const tokenError = 'loan token not found. please try again later.';
23
+ const borrowError =
24
+ 'This book is not available to borrow at this time. Please try again later.';
25
+ const erroneousActions = ['browse_book', 'borrow_book', 'create_token'];
20
26
  const shouldReturnError =
21
27
  location?.href?.indexOf('?error=true') !== -1 &&
22
28
  location?.hostname !== 'archive.org';
@@ -26,43 +32,39 @@ export default function ActionsHandlerService(options) {
26
32
  let formData = new FormData();
27
33
  formData.append('action', option.action);
28
34
  formData.append('identifier', option.identifier);
35
+ try {
36
+ await fetch(baseHost, {
37
+ method: 'POST',
38
+ body: formData,
39
+ })
40
+ .then(response => {
41
+ // intentional error on localhost
42
+ if (shouldReturnError && erroneousActions.includes(option?.action)) {
43
+ return {
44
+ success: false,
45
+ error: option?.action === 'create_token' ? tokenError : borrowError,
46
+ };
47
+ }
29
48
 
30
- fetch(baseHost, {
31
- method: 'POST',
32
- body: formData,
33
- })
34
- .then(response => {
35
- const tokenError = 'loan token not found. please try again later.';
36
- const borrowError =
37
- 'This book is not available to borrow at this time. Please try again later.';
38
-
39
- // return error reponse when not production and has ?error=true param...
40
- const erroneousActions = ['browse_book', 'borrow_book', 'create_token'];
41
- if (shouldReturnError && erroneousActions.includes(option?.action)) {
42
- return {
43
- success: false,
44
- error: option?.action === 'create_token' ? tokenError : borrowError,
45
- };
46
- }
47
-
48
- // return success response for /demo/ server...
49
- if (baseHost == '/demo/1' || baseHost == '/demo/') {
50
- return {
51
- success: true,
52
- message: 'operation executed successfully!',
53
- };
54
- }
49
+ // return success response for /demo/ server...
50
+ if (baseHost == '/demo/1' || baseHost == '/demo/') {
51
+ return {
52
+ success: true,
53
+ message: 'operation executed successfully!',
54
+ };
55
+ }
55
56
 
56
- // The response is a Response instance.
57
- // You parse the data into a useable format using `.json()`
58
- return response.json();
59
- })
60
- .then(data => {
61
- // `data` is the parsed version of the JSON returned from the above endpoint.
62
- if (!data?.error) {
63
- option?.success(data);
64
- } else {
65
- option?.error(data);
66
- }
67
- });
57
+ // The response is a Response instance.
58
+ // You parse the data into a useable format using `.json()`
59
+ return response.json();
60
+ })
61
+ .then(data => {
62
+ // `data` is the parsed version of the JSON returned from the above endpoint.
63
+ if (!data?.error) {
64
+ option?.success(data);
65
+ } else {
66
+ option?.error(data);
67
+ }
68
+ });
69
+ } catch (err) {}
68
70
  }
@@ -15,7 +15,7 @@ import * as Cookies from '../doc-cookies.js';
15
15
  export default class ActionsHandler extends LitElement {
16
16
  static properties() {
17
17
  return {
18
- loansUrl: { type: String },
18
+ returnUrl: { type: String },
19
19
  };
20
20
  }
21
21
 
@@ -24,7 +24,7 @@ export default class ActionsHandler extends LitElement {
24
24
  this.identifier = identifier;
25
25
  this.ajaxTimeout = 6000;
26
26
  this.bindEvents();
27
- this.loansUrl = '';
27
+ this.returnUrl = '';
28
28
  }
29
29
 
30
30
  sendEvent(eventCategory, eventAction) {
@@ -128,7 +128,7 @@ export default class ActionsHandler extends LitElement {
128
128
  identifier: this.identifier,
129
129
  success: () => {
130
130
  this.deleteLoanCookies();
131
- URLHelper.goToUrl(this.loansUrl, true);
131
+ URLHelper.goToUrl(this.returnUrl, true);
132
132
  },
133
133
  error: data => {
134
134
  this.dispatchActionError(action, data);
@@ -24,7 +24,7 @@ export default class IABookActions extends LitElement {
24
24
  identifier: { type: String },
25
25
  bookTitle: { type: String },
26
26
  lendingStatus: { type: Object },
27
- loansUrl: { type: String },
27
+ returnUrl: { type: String },
28
28
  width: { type: Number },
29
29
  bwbPurchaseUrl: { type: String },
30
30
  lendingBarPostInit: {
@@ -44,7 +44,7 @@ export default class IABookActions extends LitElement {
44
44
  this.userid = '';
45
45
  this.identifier = '';
46
46
  this.bookTitle = '';
47
- this.loansUrl = '';
47
+ this.returnUrl = '';
48
48
  this.lendingStatus = {};
49
49
  this.width = 0;
50
50
  this.bwbPurchaseUrl = '';
@@ -223,7 +223,7 @@ export default class IABookActions extends LitElement {
223
223
  .secondaryActions=${this.secondaryActions}
224
224
  .width=${this.width}
225
225
  .borrowType=${this.borrowType}
226
- .loansUrl=${this.loansUrl}
226
+ .returnUrl=${this.returnUrl}
227
227
  ?hasAdminAccess=${this.hasAdminAccess}
228
228
  ?disabled=${this.disableActionGroup}
229
229
  @lendingActionError=${this.handleLendingActionError}
@@ -29,7 +29,7 @@ const container = ({
29
29
  .primaryActions=${primaryActions}
30
30
  .secondaryActions=${secondaryActions}
31
31
  .borrowType=${borrowType}
32
- .loansUrl=${'https://openlibrary.org'}
32
+ .returnUrl=${'https://openlibrary.org'}
33
33
  ></collapsible-action-group>`;
34
34
 
35
35
  describe('<collapsible-action-group>', () => {
@@ -86,6 +86,6 @@ describe('<collapsible-action-group>', () => {
86
86
  });
87
87
  it('receives `loanUrl`', async () => {
88
88
  const el = await fixture(container());
89
- expect(el.loansUrl).to.equal('https://openlibrary.org');
89
+ expect(el.returnUrl).to.equal('https://openlibrary.org');
90
90
  });
91
91
  });