production-create.component.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { Component, OnInit, AfterViewInit, ViewChild } from '@angular/core';
  2. import { AppComponent } from '../../app.component';
  3. import { Production, ProductionEnum, ProductionTypeList } from '../../interfaces/production';
  4. import { ProductionService } from '../../services/production.service';
  5. import { ParticipantShort } from '../../interfaces/participant';
  6. import { ParticipantService } from '../../services/participant.service';
  7. import { Router } from '@angular/router';
  8. import { FormsModule, NgForm } from '@angular/forms';
  9. @Component({ selector: 'app-production-create', imports: [FormsModule], templateUrl: './production-create.component.html', styleUrl: './production-create.component.css' })
  10. export class ProductionCreateComponent implements OnInit, AfterViewInit
  11. {
  12. participants: ParticipantShort[] = [];
  13. types: ProductionEnum[] = ProductionTypeList;
  14. @ViewChild('formRef') productionForm!: NgForm;
  15. production: Production = new Production();
  16. constructor(private productionService: ProductionService, private participantService: ParticipantService, private router: Router, private application: AppComponent) { }
  17. ngOnInit(): void { this.retreiveParticipants(); }
  18. ngAfterViewInit() { this.application.menuActivateProds(); }
  19. private retreiveParticipants() { this.participantService.getOptionListParticipant().subscribe(data => { this.participants = data; }); }
  20. onArchiveSelected(event: any)
  21. {
  22. const et = event.target;
  23. const reader = new FileReader();
  24. if (et.files && et.files.length > 0)
  25. {
  26. const file = et.files[0];
  27. this.production.nomArchive = file.name;
  28. this.production.numeroVersion = 1;
  29. reader.onloadend = async (e: any) => { if (e.target.result) { this.production.archive = e.target.result; } }
  30. reader.readAsDataURL(file);
  31. }
  32. }
  33. onVignetteSelected(event: any)
  34. {
  35. const et = event.target;
  36. const reader = new FileReader();
  37. if (et.files && et.files.length > 0)
  38. {
  39. const file = et.files[0];
  40. reader.onloadend = async (e: any) => { if (e.target.result) { this.production.vignette = e.target.result; } }
  41. reader.readAsDataURL(file);
  42. }
  43. }
  44. private saveProduction() { this.productionService.createProduction(this.production).subscribe({ next: () => { this.goToListProduction(); }, error: (err: any) => { console.log(err); }, complete: () => { } }); }
  45. addProduction() { if (this.productionForm.valid) { this.saveProduction(); } }
  46. goToListProduction() {this.router.navigate(['/production-list'], { queryParams: { 'refresh': this.getRandomInteger(1, 100000) } }); }
  47. private getRandomInteger(min: number, max: number) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min)) + min; }
  48. }