@internetarchive/ia-book-actions 1.4.3 → 1.4.4-alpha1

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.
@@ -1,5 +1,6 @@
1
1
  /* eslint-disable */
2
2
  import { sentryLogs } from '../../config/sentry-events.js';
3
+ import log from '../log.js';
3
4
 
4
5
  /**
5
6
  * Helper to call loan service
@@ -29,7 +30,7 @@ export default async function ActionsHandlerService(options) {
29
30
  'return_loan',
30
31
  ];
31
32
  const shouldReturnError =
32
- location?.href?.indexOf('?error=true') !== -1 &&
33
+ new URLSearchParams(location?.search).get('error') === 'true' &&
33
34
  location?.hostname !== 'archive.org';
34
35
 
35
36
  const testHostname = ['localhost', 'internetarchive.github.io'];
@@ -42,6 +43,7 @@ export default async function ActionsHandlerService(options) {
42
43
  let formData = new FormData();
43
44
  formData.append('action', option.action);
44
45
  formData.append('identifier', option.identifier);
46
+
45
47
  try {
46
48
  await fetch(baseHost, {
47
49
  method: 'POST',
@@ -82,8 +84,10 @@ export default async function ActionsHandlerService(options) {
82
84
  .then(data => {
83
85
  // `data` is the parsed version of the JSON returned from the above endpoint.
84
86
  if (!data?.error) {
87
+ log(`[ActionsHandlerService] ✓ ${option.action} succeeded`, data);
85
88
  option?.success(data);
86
89
  } else {
90
+ log(`[ActionsHandlerService] ✗ ${option.action} failed`, data);
87
91
  option?.error(data);
88
92
  }
89
93
  });
@@ -183,14 +183,15 @@ export default class ActionsHandler extends LitElement {
183
183
  ActionsHandlerService({
184
184
  action,
185
185
  identifier: this.identifier,
186
- success: data => {
186
+ success: async data => {
187
187
  log('RENEW_LOAN --- ', data, action, data.loan, this.identifier);
188
188
  const activeLoan = data.loan ? data.loan : undefined;
189
189
  const isRenewal = activeLoan.renewal;
190
190
 
191
191
  if (activeLoan && isRenewal) {
192
- // when loan is renewed, let's reset timer & let everyone know.
193
- this.setBrowseTimeSession();
192
+ // Await — loanAutoRenewed listeners read this same cache key
193
+ // immediately; dispatching before the write lands was a race.
194
+ await this.setBrowseTimeSession();
194
195
  } else {
195
196
  log('RENEW_LOAN ERROR --- ', {
196
197
  action,
@@ -150,7 +150,7 @@ export default class LoanAnanlytics {
150
150
  }
151
151
 
152
152
  /**
153
- * reponsible to sent events to GA
153
+ * responsible to sent events to GA
154
154
  *
155
155
  * @param {*} eventCategory
156
156
  * @param {*} eventAction
@@ -16,8 +16,7 @@ export class LoanRenewHelper {
16
16
  // messages for auto return machenism
17
17
  this.loanRenewMessage =
18
18
  'This book has been renewed for #time #unitsOfTime.';
19
- this.loanReturnWarning =
20
- 'With no action, this book will be auto-returned in #time #unitsOfTime.';
19
+ this.loanReturnWarning = 'Go to any other page to keep your loan active.';
21
20
 
22
21
  // private props
23
22
  this.result = {
@@ -27,7 +26,7 @@ export class LoanRenewHelper {
27
26
  };
28
27
  }
29
28
 
30
- handleLoanRenew() {
29
+ async handleLoanRenew() {
31
30
  try {
32
31
  if (this.hasPageChanged) {
33
32
  return this.pageChanged(); // user clicked on page