Projekt

Obecné

Profil

Stáhnout (1.9 KB) Statistiky
| Větev: | Tag: | Revize:
1 bb7795cd hlavja
import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
2 a3ae1cab hlavja
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
3
import {ManagementService} from '../../../shared/api/endpoints/services/management.service';
4 6ea14224 hlavja
import {ToastService} from '../../../shared/services/toast.service';
5 bb7795cd hlavja
6
@Component({
7 25a20eec hlavja
  selector: 'app-unit-popup',
8 bb7795cd hlavja
  templateUrl: './unit-popup.component.html',
9
  styleUrls: ['./unit-popup.component.scss']
10
})
11
export class UnitPopupComponent implements OnInit {
12
13 a3ae1cab hlavja
  insertForm: FormGroup;
14
15 bb7795cd hlavja
  @Input() isVisible;
16
  @Input() unit;
17
  @Output() isVisibleChange: EventEmitter<boolean> = new EventEmitter<boolean>();
18 a3ae1cab hlavja
19
  constructor(
20
    private formBuilder: FormBuilder,
21 6ea14224 hlavja
    private managementService: ManagementService,
22
    private toastService: ToastService
23 a3ae1cab hlavja
  ) {
24
    this.initForm();
25
  }
26
27
  initForm() {
28
    this.insertForm = this.formBuilder.group({
29 abe9aea2 hlavja
      unitDescription: ['', Validators.required]
30 a3ae1cab hlavja
    });
31 6ea14224 hlavja
    setTimeout(() => {
32
      this.insertForm.controls.unitDescription.setValue(this.unit.description);
33
    }, 0);
34 a3ae1cab hlavja
  }
35 bb7795cd hlavja
36
  ngOnInit(): void {
37
  }
38
39 cbb91c90 hlavja
  /**
40
   * Send insert unit request to backend and handle it
41
   */
42 a3ae1cab hlavja
  saveUnit() {
43 abe9aea2 hlavja
    if (this.insertForm.controls.unitDescription.value && this.insertForm.controls.unitDescription.value !== this.unit.description) {
44
      this.unit.description = this.insertForm.controls.unitDescription.value;
45 b5525aef hlavja
      this.managementService.updateUnit$Response({ body: {
46
        unit: {
47
            unit_id: this.unit.unitId,
48
            description: this.unit.description
49
          }}
50
      }).toPromise().then( response => {
51
        if (response.status === 200) {
52 6ea14224 hlavja
          this.toastService.showSuccess();
53 b5525aef hlavja
          this.close();
54
        } else {
55
          this.toastService.showError(response.body);
56 6ea14224 hlavja
        }
57 b5525aef hlavja
      }).catch(err => this.toastService.showError(err.body.message));
58 abe9aea2 hlavja
    }
59 bb7795cd hlavja
  }
60
61
  close() {
62 a3ae1cab hlavja
    this.insertForm.reset();
63 bb7795cd hlavja
    this.isVisibleChange.emit(false);
64
  }
65
}