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

     1  import {
     2      Component,
     3      Input,
     4      OnInit }                        from '@angular/core';
     5  import { Router }                   from '@angular/router';
     6  import {
     7      FormGroup,
     8      FormControl,
     9      Validators,
    10      FormBuilder }                   from '@angular/forms';
    11  
    12  import * as moment from 'moment';
    13  
    14  import { Purchase }                 from './purchase';
    15  import { PurchaseService }          from './purchase.service';
    16  import { UserService }              from '../user/user.service';
    17  import { User }                     from '../user/user';
    18  import { PublicationService }       from '../publication/publication.service';
    19  import { Publication }              from '../publication/publication';
    20  
    21  @Component({
    22      moduleId: module.id,
    23      selector: 'lcp-purchase-form',
    24      templateUrl: 'purchase-form.component.html'
    25  })
    26  
    27  export class PurchaseFormComponent implements OnInit{
    28      @Input()
    29      purchase: Purchase;
    30      availablePublications: Publication[];
    31      availableUsers: User[];
    32  
    33      edit: boolean = false;
    34      submitButtonLabel: string = "Add";
    35      form: FormGroup;
    36  
    37      private submitted = false;
    38  
    39      constructor(
    40          private fb: FormBuilder,
    41          private router: Router,
    42          private purchaseService: PurchaseService,
    43          private userService: UserService,
    44          private publicationService: PublicationService
    45          ) {}
    46  
    47      refreshAvailablePublications(): void {
    48          this.publicationService.list().then(
    49              publications => {
    50                  this.availablePublications = publications;
    51              }
    52          );
    53      }
    54  
    55      refreshAvailableUsers(): void {
    56          this.userService.list().then(
    57              users => {
    58                  this.availableUsers = users;
    59              }
    60          );
    61      }
    62  
    63      ngOnInit(): void {
    64          this.refreshAvailablePublications();
    65          this.refreshAvailableUsers();
    66  
    67          if (this.purchase == null) {
    68              this.purchase = new Purchase();
    69              this.submitButtonLabel = "Add";
    70              this.form = this.fb.group({
    71                  "publication": ["", Validators.required],
    72                  "user": ["", Validators.required],
    73                  "end_date": ["", Validators.required],
    74                  "type": ["LOAN", Validators.required]
    75              });
    76              
    77              this.form.get('type').valueChanges.subscribe(
    78                  value => {
    79                      if(value == "LOAN") {
    80                          this.form.get('end_date').setValidators(Validators.required);
    81                      } else {
    82                          this.form.get('end_date').clearValidators();
    83                      }
    84                      this.form.updateValueAndValidity();
    85                      this.form.get('end_date').updateValueAndValidity();
    86                  }
    87              );
    88  
    89          } else {
    90              let dateTime = moment(this.purchase.endDate).format('YYYY-MM-DD HH:mm')
    91              this.edit = true;
    92              this.submitButtonLabel = "Save";
    93              this.form = this.fb.group({
    94                  "renew_type": ["NO_END_DATE", Validators.required],
    95                  "end_date": dateTime //[dateTime, Validators.required]
    96              });
    97  
    98              this.form.get('renew_type').valueChanges.subscribe(
    99                  value => {
   100                      if(value == "NO_END_DATE") {
   101                          this.form.get('end_date').clearValidators();
   102                      } else {
   103                          this.form.get('end_date').setValidators(Validators.required);
   104                      }
   105                      this.form.updateValueAndValidity();
   106                      this.form.get('end_date').updateValueAndValidity();
   107                  }
   108              );
   109          }
   110      }
   111  
   112      gotoList() {
   113          if (this.edit) this.router.navigate(['/purchases/' + this.purchase.id + "/status"]);
   114          else this.router.navigate(['/purchases']);
   115      }
   116  
   117      onCancel() {
   118          this.gotoList();
   119      }
   120  
   121      onSubmit() {
   122          this.bindForm();
   123          if (this.edit) {
   124              this.purchaseService.update(
   125                  this.purchase
   126              ).then(
   127                  purchase => {
   128                      this.gotoList();
   129                  }
   130              );
   131          } else {
   132              this.purchaseService.add(this.purchase).then(
   133                  purchase => {
   134                      this.gotoList();
   135                  }
   136              );
   137          }
   138  
   139          this.submitted = true;
   140      }
   141  
   142      // Bind form to purchase
   143      bindForm(): void {
   144          if (!this.edit) {
   145              let publicationId = this.form.value['publication'];
   146              let userId = this.form.value['user'];
   147              let publication = new Publication();
   148              let user = new User();
   149              publication.id = publicationId;
   150              user.id = userId;
   151              this.purchase.publication = publication;
   152              this.purchase.user = user;
   153              this.purchase.type = this.form.value['type'];
   154          } else {
   155              this.purchase.status = 'to-be-renewed';
   156          }
   157  
   158          if (this.form.value['end_date'].trim().length > 0) {
   159              this.purchase.endDate = moment(this.form.value['end_date']).format();
   160          }
   161  
   162          if (this.edit && this.form.value['renew_type'] == 'NO_END_DATE') {
   163              // Set end date to null
   164              // End date will be processed by LSD
   165              this.purchase.endDate = null;
   166          }
   167      }
   168  }