@internetarchive/ia-book-actions 0.0.10-2 → 0.0.12-1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@internetarchive/ia-book-actions",
3
- "version": "0.0.10-2",
3
+ "version": "0.0.12-1",
4
4
  "description": "Bookreader actions for borrowable book",
5
5
  "author": "Internet Archive",
6
6
  "license": "AGPL-3.0-only",
@@ -25,7 +25,6 @@
25
25
  "@internetarchive/local-cache": "^0.2.0",
26
26
  "@internetarchive/modal-manager": "^0.1.0",
27
27
  "@internetarchive/shared-resize-observer": "^0.0.1",
28
- "js-cookie": "^2.2.1",
29
28
  "lit-element": "^2.4.0",
30
29
  "lit-html": "^1.3.0"
31
30
  },
@@ -2,7 +2,7 @@ import { LitElement } from 'lit-element';
2
2
 
3
3
  import { URLHelper } from '../../config/url-helper.js';
4
4
  import ActionsHandlerService from './actions-handler-service.js';
5
- import 'js-cookie';
5
+ import * as Cookies from '../doc-cookies.js';
6
6
 
7
7
  /**
8
8
  * These are callback functions calling from actions-config.js file.
@@ -241,8 +241,7 @@ export default class ActionsHandler extends LitElement {
241
241
  try {
242
242
  let newCount = 1;
243
243
  const storageKey = `loan-count-${this.identifier}`;
244
- /* eslint-disable no-undef */
245
- const existingCount = Cookies.get(storageKey);
244
+ const existingCount = Cookies.getItem(storageKey);
246
245
 
247
246
  // increase browse-count by 1 when you consecutive reading a book.
248
247
  if (action === 'browseAgain' && existingCount !== undefined) {
@@ -250,11 +249,10 @@ export default class ActionsHandler extends LitElement {
250
249
  }
251
250
 
252
251
  const date = new Date();
253
- date.setHours(date.getHours() + 2);
252
+ date.setHours(date.getHours() + 2); // 2 hours
254
253
 
255
254
  // set new value
256
- /* eslint-disable no-undef */
257
- Cookies.set(storageKey, newCount, { expires: date, path: '/' });
255
+ Cookies.setItem(storageKey, newCount, date, '/');
258
256
  } catch (error) {
259
257
  this.sendEvent('Cookies-Error-Actions', error);
260
258
  }
@@ -263,15 +261,20 @@ export default class ActionsHandler extends LitElement {
263
261
  deleteLoanCookies() {
264
262
  const date = new Date();
265
263
  date.setTime(date.getTime() - 24 * 60 * 60 * 1000); // one day ago
266
- const expiry = date.toGMTString();
267
- let cookie = `loan-${this.identifier}=""`;
268
- cookie += `; expires=${expiry}`;
269
- cookie += '; path=/; domain=.archive.org;';
270
- document.cookie = cookie;
271
-
272
- cookie = `br-loan-${this.identifier}=""`;
273
- cookie += `; expires=${expiry}`;
274
- cookie += '; path=/; domain=.archive.org;';
275
- document.cookie = cookie;
264
+
265
+ Cookies.setItem(
266
+ `loan-${this.identifier}=""`,
267
+ '',
268
+ date,
269
+ '/',
270
+ '.archive.org'
271
+ );
272
+ Cookies.setItem(
273
+ `br-loan-${this.identifier}=""`,
274
+ '',
275
+ date,
276
+ '/',
277
+ '.archive.org'
278
+ );
276
279
  }
277
280
  }
@@ -0,0 +1,83 @@
1
+ /* eslint-disable */
2
+ /**
3
+ * Helper module use to get, set and remove item from cookie
4
+ *
5
+ * See more:
6
+ * https://developer.mozilla.org/en-US/docs/Web/API/document.cookie
7
+ * https://developer.mozilla.org/User:fusionchess
8
+ * https://github.com/madmurphy/cookies.js
9
+ * This framework is released under the GNU Public License, version 3 or later.
10
+ * http://www.gnu.org/licenses/gpl-3.0-standalone.html
11
+ */
12
+
13
+ /**
14
+ * Get specific key's value stored in cookie
15
+ *
16
+ * @param {string} sKey
17
+ *
18
+ * @returns {string|null}
19
+ */
20
+ export function getItem(sKey) {
21
+ if (!sKey) return null;
22
+
23
+ return (
24
+ decodeURIComponent(
25
+ // eslint-disable-next-line no-useless-escape
26
+ document.cookie.replace(
27
+ new RegExp(
28
+ '(?:(?:^|.*;)\\s*' +
29
+ encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, '\\$&') +
30
+ '\\s*\\=\\s*([^;]*).*$)|^.*$'
31
+ ),
32
+ '$1'
33
+ )
34
+ ) || null
35
+ );
36
+ }
37
+
38
+ /**
39
+ * Set specific key's value in cookie
40
+ *
41
+ * @param {string} sKey cookie name
42
+ * @param {string} sValue cookie value
43
+ * @param {string} [vEnd] expire|max-age
44
+ * @param {string} [sPath] path of current item
45
+ * @param {string} [sDomain] domain name
46
+ * @param {boolean} [bSecure]
47
+ *
48
+ * @returns {true}
49
+ */
50
+ export function setItem(sKey, sValue, vEnd, sPath, sDomain, bSecure) {
51
+ document.cookie =
52
+ encodeURIComponent(sKey) +
53
+ '=' +
54
+ encodeURIComponent(sValue) +
55
+ (vEnd ? `; expires=${vEnd.toUTCString()}` : '') +
56
+ (sDomain ? `; domain=${sDomain}` : '') +
57
+ (sPath ? `; path=${sPath}` : '') +
58
+ (bSecure ? `; secure` : '');
59
+
60
+ return true;
61
+ }
62
+
63
+ /**
64
+ * BROKEN Remove specific key's value from cookie
65
+ * @fixme hasItem isn't even implemented! This will always error.
66
+ * @param {string} sKey cookie name
67
+ * @param {string} [sPath] path of current item
68
+ * @param {string} [sDomain]
69
+ *
70
+ * @returns {boolean}
71
+ */
72
+ export function removeItem(sKey, sPath, sDomain) {
73
+ // eslint-disable-next-line
74
+ if (!hasItem(sKey)) return false;
75
+
76
+ document.cookie =
77
+ encodeURIComponent(sKey) +
78
+ `=; expires=Thu, 01 Jan 1970 00:00:00 GMT` +
79
+ (sDomain ? `; domain=${sDomain}` : '') +
80
+ (sPath ? `; path=${sPath}` : '');
81
+
82
+ return true;
83
+ }
@@ -144,6 +144,7 @@ export default class GetLendingActions {
144
144
  primaryTitle: 'Book available to patrons with print disabilities.',
145
145
  primaryActions: [unavailable],
146
146
  primaryColor: 'primary',
147
+ secondaryActions: [],
147
148
  };
148
149
  }
149
150
 
@@ -1,5 +1,5 @@
1
1
  import ActionsHandlerService from './actions-handler/actions-handler-service.js';
2
- import 'js-cookie';
2
+ import * as Cookies from './doc-cookies.js';
3
3
 
4
4
  /**
5
5
  * This class is used to create loan token for borrowed books
@@ -30,8 +30,9 @@ export class LoanTokenPoller {
30
30
  // send consecutiveLoanCounts for browsed books only.
31
31
  if (this.borrowType === 'browsed') {
32
32
  try {
33
- /* eslint-disable no-undef */
34
- const existingCount = Cookies.get(`loan-count-${this.identifier}`);
33
+ const existingCount = Cookies.getItem(
34
+ `loan-count-${this.identifier}`
35
+ );
35
36
  consecutiveLoanCounts = existingCount ?? 1;
36
37
  } catch (error) {
37
38
  this.sendEvent('Cookies-Error-Token', error);