1 import {Injectable, EventEmitter} from '@angular/core';
2 import {Observable} from 'rxjs/Observable';
3 import {StoreService} from '@eg/core/store.service';
4 import {NetService} from '@eg/core/net.service';
5 import {PcrudService} from '@eg/core/pcrud.service';
6 import {AnonCacheService} from '@eg/share/util/anon-cache.service';
8 // Baskets are stored in an anonymous cache using the cache key stored
9 // in a LoginSessionItem (i.e. cookie) at name BASKET_CACHE_KEY_COOKIE.
10 // The list is stored under attribute BASKET_CACHE_ATTR.
11 // Avoid conflicts with the AngularJS embedded catalog basket by
12 // using a different value for the cookie name, since our version
13 // stores all cookies as JSON, unlike the TPAC.
14 const BASKET_CACHE_KEY_COOKIE = 'basket';
15 const BASKET_CACHE_ATTR = 'recordIds';
18 export class BasketService {
22 // Fired every time our list of ID's are updated.
23 onChange: EventEmitter<number[]>;
26 private net: NetService,
27 private pcrud: PcrudService,
28 private store: StoreService,
29 private anonCache: AnonCacheService
32 this.onChange = new EventEmitter<number[]>();
35 hasRecordId(id: number): boolean {
36 return this.idList.indexOf(Number(id)) > -1;
39 recordCount(): number {
40 return this.idList.length;
43 // TODO: Add server-side API for sorting a set of bibs by ID.
44 // See EGCatLoader/Container::fetch_mylist
45 getRecordIds(): Promise<number[]> {
46 const cacheKey = this.store.getLoginSessionItem(BASKET_CACHE_KEY_COOKIE);
49 if (!cacheKey) { return Promise.resolve(this.idList); }
51 return this.anonCache.getItem(cacheKey, BASKET_CACHE_ATTR).then(
53 if (!list) {return this.idList};
54 this.idList = list.map(id => Number(id));
60 setRecordIds(ids: number[]): Promise<number[]> {
63 // If we have no cache key, that's OK, assume this is the first
64 // attempt at adding a value and let the server create the cache
65 // key for us, then store the value in our cookie.
66 const cacheKey = this.store.getLoginSessionItem(BASKET_CACHE_KEY_COOKIE);
68 return this.anonCache.setItem(cacheKey, BASKET_CACHE_ATTR, this.idList)
70 this.store.setLoginSessionItem(BASKET_CACHE_KEY_COOKIE, cacheKey);
71 this.onChange.emit(this.idList);
76 addRecordIds(ids: number[]): Promise<number[]> {
77 ids = ids.filter(id => !this.hasRecordId(id)); // avoid dupes
79 if (ids.length === 0) {
80 return Promise.resolve(this.idList);
82 return this.setRecordIds(
83 this.idList.concat(ids.map(id => Number(id))));
86 removeRecordIds(ids: number[]): Promise<number[]> {
88 if (this.idList.length === 0) {
89 return Promise.resolve(this.idList);
92 const wantedIds = this.idList.filter(
93 id => ids.indexOf(Number(id)) < 0);
95 return this.setRecordIds(wantedIds); // OK if empty
98 removeAllRecordIds(): Promise<number[]> {
99 return this.setRecordIds([]);