]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/catalog/record/actions.component.ts
LP2061136 - Stamping 1405 DB upgrade script
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / catalog / record / actions.component.ts
1 import {Component, OnInit, Input, Output, EventEmitter} from '@angular/core';
2 import {Router} from '@angular/router';
3 import {HttpClient} from '@angular/common/http';
4 import {StoreService} from '@eg/core/store.service';
5 import {CatalogService} from '@eg/share/catalog/catalog.service';
6 import {CatalogSearchContext} from '@eg/share/catalog/search-context';
7 import {CatalogUrlService} from '@eg/share/catalog/catalog-url.service';
8 import {StaffCatalogService} from '../catalog.service';
9 import {StringService} from '@eg/share/string/string.service';
10 import {ToastService} from '@eg/share/toast/toast.service';
11 import {HoldingsService} from '@eg/staff/share/holdings/holdings.service';
12
13 export const AC_CLEAR_CACHE_PATH = '/opac/extras/ac/clearcache/all/r/';
14
15 @Component({
16     selector: 'eg-catalog-record-actions',
17     templateUrl: 'actions.component.html'
18 })
19 export class RecordActionsComponent implements OnInit {
20
21     @Output() addHoldingsRequested: EventEmitter<void>
22         = new EventEmitter<void>();
23
24     recId: number;
25     initDone = false;
26     searchContext: CatalogSearchContext;
27
28     targets = {
29         conjoined: {
30             key: 'eg.cat.marked_conjoined_record',
31             current: null
32         },
33         overlay: {
34             key: 'eg.cat.marked_overlay_record',
35             current: null
36         },
37         holdTransfer: {
38             key: 'eg.circ.hold.title_transfer_target',
39             current: null
40         },
41         holdingTransfer: {
42             key: 'eg.cat.transfer_target_record',
43             current: null,
44             clear: [ // Clear these values on mark.
45                 'eg.cat.transfer_target_lib',
46                 'eg.cat.transfer_target_vol'
47             ]
48         }
49     };
50
51     get patronViewUrl(): string {
52         if (!this.staffCat.patronViewUrl) {
53             return `/eg/opac/record/${encodeURIComponent(this.recId)}`;
54         }
55         return encodeURI(this.staffCat.patronViewUrl.replace(
56             /\{eg_record_id\}/g, ''+this.recId
57         ));
58     }
59
60     @Input() set recordId(recId: number) {
61         this.recId = recId;
62         if (this.initDone) {
63             // Fire any record specific actions here
64         }
65     }
66
67     @Input() isHoldable: boolean;
68
69     constructor(
70         private router: Router,
71         private store: StoreService,
72         private strings: StringService,
73         private toast: ToastService,
74         private cat: CatalogService,
75         private catUrl: CatalogUrlService,
76         private staffCat: StaffCatalogService,
77         private holdings: HoldingsService,
78         private http: HttpClient
79     ) {}
80
81     ngOnInit() {
82         this.initDone = true;
83
84         Object.keys(this.targets).forEach(name => {
85             const target = this.targets[name];
86             target.current = this.store.getLocalItem(target.key);
87         });
88     }
89
90     mark(name: string) {
91         const target = this.targets[name];
92         target.current = this.recId;
93         this.store.setLocalItem(target.key, this.recId);
94
95         if (target.clear) {
96             // Some marks require clearing other marks.
97             target.clear.forEach(key => this.store.removeLocalItem(key));
98         }
99
100         this.strings.interpolate('catalog.record.toast.' + name)
101             .then(txt => this.toast.success(txt));
102     }
103
104     clearMarks() {
105         Object.keys(this.targets).forEach(name => {
106             const target = this.targets[name];
107             target.current = null;
108             this.store.removeLocalItem(target.key);
109         });
110         this.strings.interpolate('catalog.record.toast.cleared')
111             .then(txt => this.toast.success(txt));
112     }
113
114     addHoldings() {
115         this.addHoldingsRequested.emit();
116     }
117
118     clearAddedContentCache() {
119         const url = AC_CLEAR_CACHE_PATH + this.recId;
120         this.http.get(url, {responseType: 'text'}).subscribe(
121             data => {
122                 console.debug(data);
123                 this.strings.interpolate('catalog.record.toast.clearAddedContentCache')
124                     .then(txt => this.toast.success(txt));
125             },
126             (err: unknown) => {
127                 this.strings.interpolate('catalog.record.toast.clearAddedContentCacheFailed')
128                     .then(txt => this.toast.danger(txt));
129             }
130         );
131     }
132 }
133
134