@internetarchive/ia-book-actions 0.0.29-alpha3 → 0.0.29-alpha5

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
@@ -128,13 +128,13 @@
128
128
  iaBookActions.returnUrl = 'https://openlibrary.org';
129
129
  iaBookActions.localCache = localCache;
130
130
  iaBookActions.autoRenewConfig = {
131
- totalTime: 60, // 60 second = 1 min
132
- autoCheckAt: 55, // totalTime - 10th minute
131
+ totalTime: 20, // 60 second = 1 min
132
+ autoCheckAt: 15, // totalTime - 10th minute
133
133
  FlippedInLast: 15, // within last 15 second/minute
134
134
  };
135
135
 
136
136
  let lendingStatus1 = iaBookActions.lendingStatus;
137
- let secondsLeftOnLoan = 60;
137
+ let secondsLeftOnLoan = iaBookActions.autoRenewConfig.totalTime;
138
138
 
139
139
  document.querySelectorAll('.titleBar input[type=checkbox]').forEach((item) => {
140
140
  item.addEventListener('click', (e) => {
@@ -226,7 +226,7 @@
226
226
  await localCache.set({
227
227
  key: `${identifier}-loanTime`,
228
228
  value: new Date(), // current time
229
- ttl: parseInt(iaBookActions.autoRenewConfig.totalTime),
229
+ ttl: Number(iaBookActions.autoRenewConfig.totalTime),
230
230
  })
231
231
 
232
232
  await localCache.delete(`${identifier}-pageFlipTime`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@internetarchive/ia-book-actions",
3
- "version": "0.0.29-alpha3",
3
+ "version": "0.0.29-alpha5",
4
4
  "description": "Bookreader actions for borrowable book",
5
5
  "author": "Internet Archive",
6
6
  "license": "AGPL-3.0-only",
@@ -100,7 +100,7 @@ export const canBrowseAndBorrow = {
100
100
  // userid: "",
101
101
  bookUrl: '/details/practicalorganic00plim',
102
102
  daysLeftOnLoan: 0,
103
- isAdmin: true,
103
+ isAdmin: false,
104
104
  isArchiveOrgLending: true,
105
105
  isAvailable: false,
106
106
  isAvailableForBrowsing: false,
@@ -4,7 +4,7 @@ export class ToastMessage extends LitElement {
4
4
  static get properties() {
5
5
  return {
6
6
  texts: { type: String },
7
- dismisOnClick: { type: Function },
7
+ dismisOnClick: { type: Boolean },
8
8
  hideToast: { type: Boolean },
9
9
  };
10
10
  }
@@ -19,7 +19,11 @@ export class ToastMessage extends LitElement {
19
19
  render() {
20
20
  return !this.hideToast
21
21
  ? html`
22
- <span class="toast-message ${this.textClass}">${this.texts}</span>
22
+ <span
23
+ class="toast-message fade-in ${this.textClass}"
24
+ title="Click/tap to close"
25
+ @click=${() => { this.dismisOnClick ? this.hideToast = true : nothing}}
26
+ >${this.texts}</span>
23
27
  `
24
28
  : nothing;
25
29
  }
@@ -30,15 +34,16 @@ export class ToastMessage extends LitElement {
30
34
  return css`
31
35
  :host {
32
36
  display: inline-block;
37
+
33
38
  }
34
39
  .toast-message {
35
40
  z-index: 2;
36
41
  position: absolute;
37
42
  top: ${heightFromTop};
38
43
  font-size: 1.4rem;
44
+ font-family: "Helvetica Neue";
39
45
  color: #fff;
40
46
  left: 50%;
41
- right: 50%;
42
47
  background: #333;
43
48
  border: 1px solid #fff;
44
49
  border-radius: 5px;
@@ -46,7 +51,18 @@ export class ToastMessage extends LitElement {
46
51
  padding: 10px;
47
52
  width: fit-content;
48
53
  transform: translate(-50%, -50%);
54
+ cursor: pointer;
55
+ animation: fadeIn 5s;
56
+ @keyframes fadeIn {
57
+ 0% {
58
+ opacity: 0;
59
+ }
60
+ 100% {
61
+ opacity: 1;
62
+ }
63
+ }
49
64
  }
65
+
50
66
  .hidden {
51
67
  display: none;
52
68
  }
@@ -127,14 +127,17 @@ export default class ActionsHandler extends LitElement {
127
127
  }
128
128
 
129
129
  handleAutoBrowseIt() {
130
- const action = 'auto_browse';
131
- this.setBrowseTimeSession()
130
+ const action = 'renew_loan';
132
131
 
133
132
  ActionsHandlerService({
134
133
  action,
135
134
  identifier: this.identifier,
136
- success: () => {},
135
+ success: () => {
136
+ console.log('success!');
137
+ },
137
138
  error: data => {
139
+ console.log(data);
140
+ console.log('error!');
138
141
  this.dispatchActionError(action, data);
139
142
  },
140
143
  });
@@ -291,6 +294,19 @@ export default class ActionsHandler extends LitElement {
291
294
  }
292
295
  }
293
296
 
297
+ async setBrowseTimeSession(_date = '') {
298
+ try {
299
+ // set a value
300
+ await this.localCache.set({
301
+ key: `${this.identifier}-loanTime`,
302
+ value: new Date(), // current time
303
+ ttl: Number(this.autoRenewConfig.totalTime),
304
+ })
305
+
306
+ await this.localCache.delete(`${this.identifier}-pageFlipTime`);
307
+ } catch (error) {}
308
+ }
309
+
294
310
  deleteLoanCookies() {
295
311
  const date = new Date();
296
312
  date.setTime(date.getTime() - 24 * 60 * 60 * 1000); // one day ago
@@ -310,17 +326,4 @@ export default class ActionsHandler extends LitElement {
310
326
  '.archive.org'
311
327
  );
312
328
  }
313
-
314
- async setBrowseTimeSession() {
315
- try {
316
- // set a value
317
- await this.localCache.set({
318
- key: `${this.identifier}-loanTime`,
319
- value: new Date(), // current time
320
- ttl: parseInt(this.autoRenewConfig.totalTime),
321
- })
322
-
323
- await this.localCache.delete(`${this.identifier}-pageFlipTime`);
324
- } catch (error) {}
325
- }
326
329
  }
@@ -13,18 +13,18 @@ export class OneHourAutoRenew {
13
13
  this.autoRenewConfig = autoRenewConfig;
14
14
  this.userHasBrowsed = userHasBrowsed;
15
15
 
16
+ // messages for auto return machenism
17
+ this.autoRenewMessage = 'This book has been renewed for another hour.';
18
+ this.autoReturnMessage = 'This book has been auto-returned due to inactivity.';
19
+ this.autoReturnWarning = 'This book will be auto-returned in 10 minutes unless you turn a page.';
20
+
16
21
  // private props
17
22
  this.resultRes = {
18
- toastTexts: null,
23
+ toastTexts: this.autoReturnMessage,
19
24
  hideToast: true,
20
25
  renewNow: false,
21
26
  };
22
27
 
23
- // messages for auto return machenism
24
- this.autoRenewMessage = 'This book has been renewed for another hour.';
25
- this.autoReturnMessage = 'This book has been auto-returned due to inactivity.';
26
- this.autoReturnWarning = 'This book will be auto-returned in 10 minutes unless you turn a page.';
27
-
28
28
  return this.handleBookAutoRenew();
29
29
  }
30
30
 
@@ -51,7 +51,7 @@ export class OneHourAutoRenew {
51
51
  await this.localCache.set({
52
52
  key: `${this.identifier}-pageFlipTime`,
53
53
  value: new Date(), // current time
54
- ttl: parseInt(this.autoRenewConfig.totalTime),
54
+ ttl: Number(this.autoRenewConfig.totalTime),
55
55
  })
56
56
  }
57
57
 
@@ -75,6 +75,8 @@ export class OneHourAutoRenew {
75
75
  this.resultRes.browseAgainNow = true;
76
76
  this.resultRes.hideToast = false;
77
77
  this.resultRes.renewNow = true;
78
+
79
+ this.setNewBrowseTime();
78
80
  }
79
81
 
80
82
  return this.resultRes;
@@ -105,11 +107,32 @@ export class OneHourAutoRenew {
105
107
 
106
108
  // delete pageFlipTime when book is auto renew at nth minute
107
109
  await this.localCache.delete(`${this.identifier}-pageFlipTime`);
110
+ this.setNewBrowseTime();
108
111
  }
109
112
 
110
113
  return this.resultRes;
111
114
  }
112
115
 
116
+ async setNewBrowseTime() {
117
+ try {
118
+ // last 15 min if used flipped a page.
119
+ const expireDate = await this.changeTime(
120
+ new Date(),
121
+ this.autoRenewConfig.totalTime,
122
+ 'add'
123
+ ); // 15 seconds
124
+
125
+ // set a value
126
+ await this.localCache.set({
127
+ key: `${this.identifier}-loanTime`,
128
+ value: expireDate, // expireDate time
129
+ ttl: Number(this.autoRenewConfig.totalTime),
130
+ })
131
+
132
+ await this.localCache.delete(`${this.identifier}-pageFlipTime`);
133
+ } catch (error) {}
134
+ }
135
+
113
136
  async changeTime(date, minutes, op) {
114
137
  if (date === undefined) return undefined;
115
138
 
@@ -128,24 +151,4 @@ export class OneHourAutoRenew {
128
151
  `identifier=${this.identifier}`
129
152
  );
130
153
  }
131
-
132
- async handleAutoRenew() {
133
- const action = 'renew_loan';
134
- ActionsHandlerService({
135
- identifier: this.identifier,
136
- action,
137
- error: data => {
138
- window?.Sentry?.captureMessage(
139
- 'handleAutoRenew error',
140
- JSON.stringify(data)
141
- );
142
- // this.errorCallback({ detail: { action, data } });
143
- // send LendingServiceError to GA
144
- this.sendEvent('handleAutoRenew', action);
145
- },
146
- success: () => {
147
- return 'success';
148
- },
149
- });
150
- }
151
154
  }
@@ -70,6 +70,7 @@ export default class IABookActions extends LitElement {
70
70
  namespace: '1HourLoanStorage'
71
71
  }),
72
72
  this.autoRenewConfig = {},
73
+ this.autoRenewObj = undefined;
73
74
 
74
75
  // private props
75
76
  this.primaryActions = [];
@@ -101,6 +102,7 @@ export default class IABookActions extends LitElement {
101
102
  }
102
103
 
103
104
  updated(changed) {
105
+ // console.log(changed)
104
106
  if (changed.has('lendingStatus') || changed.has('bwbPurchaseUrl')) {
105
107
  this.setupLendingToolbarActions();
106
108
  this.update();
@@ -120,7 +122,7 @@ export default class IABookActions extends LitElement {
120
122
  this.addEventListener('autoRenewAttempt', async event => {
121
123
  const isPageFlipped = event.detail.bookPageChange;
122
124
 
123
- const dd = await new OneHourAutoRenew(
125
+ this.autoRenewObj = await new OneHourAutoRenew(
124
126
  isPageFlipped,
125
127
  this.identifier,
126
128
  this.localCache,
@@ -128,34 +130,37 @@ export default class IABookActions extends LitElement {
128
130
  this.lendingStatus.user_has_browsed && !this.lendingStatus.browsingExpired
129
131
  );
130
132
 
131
- this.toastTexts = dd.toastTexts;
132
- this.hideToast = dd.hideToast;
133
- this.browseAgainNow = dd.renewNow;
133
+ this.toastTexts = this.autoRenewObj.toastTexts;
134
+ this.hideToast = this.autoRenewObj.hideToast;
135
+ this.browseAgainNow = this.autoRenewObj.renewNow;
134
136
  if (this.browseAgainNow) {
135
137
  await this.browseHasRenew();
136
138
  }
137
- // this.handleBookAutoRenew(event);
138
139
  });
139
140
  }
140
141
 
141
142
  async browseHasExpired() {
142
143
  const currStatus = { ...this.lendingStatus, browsingExpired: true };
143
144
  this.lendingStatus = currStatus;
145
+ this.browseAgainNow = false;
144
146
 
145
147
  // remove respected key:value
146
148
  await this.localCache.delete(`${this.identifier}-loanTime`);
147
149
  await this.localCache.delete(`${this.identifier}-pageFlipTime`);
148
150
 
149
151
  // show message after browsed book is expired.
150
- this.toastTexts = this.autoReturnMessage;
152
+ this.toastTexts = 'This book has been auto-returned due to inactivity.';
151
153
  }
152
154
 
153
155
  async browseHasRenew() {
154
- const currStatus = {
156
+ const loanTime = await this.localCache.get(`${this.identifier}-loanTime`);
157
+ const secondsLeft =(loanTime - new Date()) / 1000; // different in seconds
158
+
159
+ const currStatus = {
155
160
  ...this.lendingStatus,
156
161
  user_has_browsed: true,
157
162
  browsingExpired: false,
158
- secondsLeftOnLoan: 61
163
+ secondsLeftOnLoan: Math.abs(Math.round(secondsLeft)),
159
164
  };
160
165
  this.lendingStatus = currStatus;
161
166
 
@@ -301,7 +306,7 @@ export default class IABookActions extends LitElement {
301
306
  return html`
302
307
  <toast-message
303
308
  .texts=${this.toastTexts}
304
- .dismisOnClick="1"
309
+ ?dismisOnClick=${true}
305
310
  ?hideToast=${this.hideToast}
306
311
  ></toast-message>
307
312
  `;