github.com/readium/readium-lcp-server@v0.0.0-20240101192032-6e95190e99f1/frontend/manage/app/purchase/purchase-status.component.ts (about)

     1  import { Component, OnInit }        from '@angular/core';
     2  import { ActivatedRoute, Params }   from '@angular/router';
     3  import { Observable, Subscription } from 'rxjs/Rx';
     4  import 'rxjs/add/operator/switchMap';
     5  
     6  import { Purchase }                 from './purchase';
     7  import { PurchaseService }          from './purchase.service';
     8  import { LsdService }               from '../lsd/lsd.service';
     9  import { LicenseStatus }            from '../lsd/license-status';
    10  import * as moment                  from 'moment';
    11  
    12  declare var Config: any;
    13  
    14  @Component({
    15      moduleId: module.id,
    16      selector: 'lcp-purchase-status',
    17      templateUrl: 'purchase-status.component.html'
    18  })
    19  
    20  export class PurchaseStatusComponent implements OnInit {
    21      purchase: Purchase;
    22      licenseStatus: LicenseStatus;
    23      revokeMessage: string = "";
    24  
    25      constructor(
    26          private route: ActivatedRoute,
    27          private purchaseService: PurchaseService,
    28          private lsdService: LsdService) {
    29      }
    30  
    31      ngOnInit(): void {
    32          this.refreshPurchase();
    33      }
    34  
    35      refreshPurchase(){
    36          this.route.params
    37              .switchMap((params: Params) => this.purchaseService.get(params['id']))
    38              .subscribe(purchase => {
    39                  this.purchase = purchase;
    40                  if (purchase.licenseUuid){
    41                      this.lsdService.get(purchase.licenseUuid).then(
    42                          licenseStatus => {
    43                              this.licenseStatus = licenseStatus;
    44                          });
    45                  }
    46              });
    47      }
    48  
    49      formatDate(date: string): string {
    50          return moment(date).format('YYYY-MM-DD HH:mm');
    51      }
    52  
    53      onDownload_LSD(purchase: Purchase): void {
    54  
    55          // The URL does not resolve to a content-disposition+filename like "ebook_title.lsd"
    56          // If this were the case, most web browsers would normally just download the linked file.
    57          // Instead, with some browsers the file is displayed (the current page context is overwritten)
    58          let url = this.buildLsdDownloadUrl(purchase);
    59          
    60          //document.location.href = url;
    61          window.open(url, "_blank");
    62      }
    63  
    64       buildLsdDownloadUrl(purchase: Purchase): string {
    65          return Config.lsd.url + '/licenses/' + purchase.licenseUuid + '/status';
    66      }
    67  
    68      onDownload_LCPL(purchase: Purchase): void {
    69          // Wait 5 seconds before refreshing purchases
    70          let downloadTimer = Observable.timer(5000);
    71          let downloadSubscriber = downloadTimer.subscribe(
    72              (t: any) => {
    73                  this.refreshPurchase();
    74                  downloadSubscriber.unsubscribe();
    75              }
    76          );
    77  
    78          // The URL resolves to a content-disposition+filename like "ebook_title.lcpl"
    79          // Most web browsers should normally just download the linked file, not display it.
    80          let url = this.buildLcplDownloadUrl(purchase);
    81  
    82          window.open(url, "_blank");
    83      }
    84  
    85      buildLcplDownloadUrl(purchase: Purchase): string {
    86          return Config.frontend.url + '/api/v1/purchases/' + purchase.id + '/license';
    87      }
    88  
    89      onReturn(purchase: Purchase): void {
    90          purchase.status = 'to-be-returned';
    91          this.purchaseService.update(purchase).then(
    92              purchase => {
    93                  this.refreshPurchase();
    94              }
    95          );
    96      }
    97  
    98      onRevoke(purchase: Purchase): void {
    99          this.purchaseService.revoke("", purchase.licenseUuid).then(
   100              status => {
   101                  this.refreshPurchase();
   102                  var success:boolean = false;
   103                  if (status == 200) {
   104                      this.revokeMessage = "The license has been revoked";
   105                      success = true;
   106                  } else if (status == 400){
   107                      this.revokeMessage = '400 The new status is not compatible with current status';
   108                  }
   109                  else if (status ==  401){
   110                      this.revokeMessage = '401 License not Found';
   111                  }
   112                  else if (status == 404){
   113                      this.revokeMessage = '404 License not Found';
   114                  }
   115                  else if (status >= 500){
   116                      this.revokeMessage = 'An internal error appear';
   117                  }
   118                  else{
   119                      this.revokeMessage = 'An internal error appear';
   120                  }
   121                  this.showSnackBar(success);
   122              }
   123          );
   124      }
   125  
   126      showSnackBar(success: boolean) {
   127          var x = $("#snackbar");
   128          var xClass = "show";
   129          if (success) xClass = "show success";
   130          x.attr("class",xClass);
   131          setTimeout(function(){ x.attr("class",""); }, 3000);
   132      }
   133      
   134  }