github.com/readium/readium-lcp-server@v0.0.0-20240101192032-6e95190e99f1/frontend/manage/app/components/purchase-list-component.ts (about) 1 import { Component, Input, OnInit } from '@angular/core'; 2 import {ActivatedRoute, Params} from '@angular/router'; 3 import {Location} from '@angular/common'; 4 5 import { User } from './user'; 6 import { Purchase } from './purchase'; 7 import { PurchaseService } from './purchase.service'; 8 import {LsdService} from './lsd.service'; 9 10 @Component({ 11 moduleId: module.id, 12 selector: 'purchases', 13 templateUrl: '/app/components/purchases.html', 14 styleUrls: ['../../app/components/purchases.css'], 15 providers: [PurchaseService, LsdService] 16 }) 17 18 export class PurchasesComponent implements OnInit { 19 @Input() user: User; 20 @Input() hours: string; 21 @Input() deviceID: string; 22 @Input() deviceName: string; 23 24 purchases: Purchase[]; 25 selectedPurchase: Purchase; 26 27 constructor( 28 private purchaseService: PurchaseService, 29 private lsdService: LsdService, 30 private route: ActivatedRoute, 31 private location: Location 32 ) {} 33 34 ngOnInit(): void { 35 this.purchaseService.getPurchases(this.user) 36 .then(purchases => this.purchases = purchases); 37 } 38 goBack(): void { 39 this.location.back(); 40 } 41 42 onSelect(p: Purchase): void { 43 this.selectedPurchase = p; 44 } 45 RegisterDevice(p: Purchase, deviceID: string, deviceName: string|undefined) { 46 this.deviceID = deviceID; 47 this.deviceName = deviceName; 48 console.log('register license for device ' + deviceID ); 49 if ( p.licenseID !== '') { 50 this.lsdService.registerDevice(p.licenseID, deviceID, deviceName) 51 .then( status => alert('DEVICE registered!\n' + JSON.stringify(status) ) ) 52 .catch( reason => alert( 'PROBLEM: \n' + reason._body)); 53 } else { 54 alert('No licenseID for this purchase, please press download to create a license.'); 55 } 56 } 57 58 RenewLoan(p: Purchase, hours: number, deviceID: string|undefined, deviceName: string|undefined) { 59 console.log('should renew license for another ' + hours + ' hours. ()' + p.label + ')'); 60 if ( p.licenseID !== '') { 61 let t = Date.now(); 62 t += hours * 3600 * 1000; 63 this.lsdService.renewLoan(p.licenseID, new Date(t), deviceID, deviceName) 64 .then( status => alert(JSON.stringify(status) ) ) 65 .catch( reason => alert( 'RENEW PROBLEM: \n' + reason._body)); 66 } else { 67 alert('No licenseID for this purchase, please press download to create a license.'); 68 } 69 } 70 71 // contact lsd server and return the license 72 ReturnLoan(p: Purchase, deviceID: string|undefined, deviceName: string|undefined) { 73 if ( p.licenseID !== '') { 74 this.lsdService.returnLoan(p.licenseID, deviceID, deviceName) 75 .then( status => alert(JSON.stringify(status) ) ) 76 .catch( reason => console.log('error returning license for ' + p.label + ':' + reason) ) 77 } else { 78 alert('No licenseID yet for this purchase! (clic download first)'); 79 } 80 } 81 82 // contact lsd server and CheckStatus of the license 83 CheckStatus(p: Purchase) { 84 if ( p.licenseID !== '') { 85 this.lsdService.getStatus(p.licenseID,undefined,undefined) 86 .then( status => alert(JSON.stringify(status) ) ) 87 .catch( reason => console.log('error checking LSD status for ' + p.label + ':' + reason) ) 88 } else { 89 alert('No licenseID for this purchase, please press download to create a license.'); 90 } 91 92 } 93 DownloadLicense(p: Purchase): void { 94 // get License ! 95 if ( p.licenseID === undefined) { 96 console.log('Get license and download ' + p.label ); 97 // the license does not yet exist (some error occured ?) 98 // we need to recontact the static server and ask to create a new license 99 window.location.href = '/users/' + p.user.userID + '/purchases/' + p.purchaseID + '/license'; 100 } else { 101 console.log('Re-download ' + p.label + '(' + p.licenseID + ')'); 102 // redirect to /licenses/ p.licenseID 103 window.location.href = '/licenses/' + p.licenseID; 104 } 105 106 } 107 108 // get epub with license 109 DownloadPublication(p: Purchase): void { 110 if ( p.licenseID === undefined) { 111 console.log('Get purchase and download ' + p.label ); 112 // existing lcp function 113 // we need to recontact the static server and ask to create a new license 114 window.location.href = '/users/' + p.user.userID + '/purchases/' + p.purchaseID + '/publication'; 115 } else { 116 console.log('Re-download publication : ' + p.label + '(' + p.licenseID + ')'); 117 // redirect to /licenses/ p.licenseID 118 window.location.href = '/licenses/' + p.licenseID + '/publication'; 119 } 120 } 121 122 }