michussj07

add-dialog1.component.ts

Feb 13th, 2020
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. import {Component, OnInit} from '@angular/core';
  2. import {FormControl, FormGroup, Validators} from '@angular/forms';
  3. import {Car} from '../../pojazdy/http-cars.model';
  4. import {HttpCarsService} from '../../pojazdy/http-cars.service';
  5. import {MatSnackBar} from '@angular/material';
  6. import {carStatus} from './carStatus.model';
  7. import {CarStatusService} from './car-status.service';
  8. import {HttpClient, HttpErrorResponse} from '@angular/common/http';
  9. import {ConfigService} from '../../../services/config.service';
  10. import {Observable} from 'rxjs';
  11. import {CarTypeService} from './car-type.service';
  12. import {carType} from './carType.model';
  13.  
  14.  
  15. @Component({
  16. selector: 'app-add-dialog1',
  17. templateUrl: './add-dialog1.component.html',
  18. styleUrls: ['./add-dialog1.component.scss']
  19. })
  20. export class AddDialog1Component implements OnInit {
  21.  
  22. constructor(private httpCarsService: HttpCarsService,
  23. public snackBar: MatSnackBar,
  24. public carStatusService:CarStatusService,
  25. public carTypeService:CarTypeService
  26. ) { }
  27.  
  28. car : Car=new Car();
  29. submitted = false;
  30. selected = null;
  31. statusList: Observable<carStatus[]>;
  32. carStatus: carStatus = new carStatus();
  33. typeList: Observable<carType[]>;
  34. carType: carType = new carType();
  35.  
  36.  
  37.  
  38. ngOnInit() {
  39. this.submitted=false;
  40. this.carStatusService.getCarStatusList().subscribe(data => {
  41. this.statusList = data;
  42. });
  43. this.carTypeService.getCarTypeList().subscribe(data => {
  44. this.typeList = data;
  45. });
  46. }
  47.  
  48. carsaveform=new FormGroup({
  49. mark:new FormControl('' , [Validators.required] ),
  50. model:new FormControl('' , [Validators.required] ),
  51. registrationNumber:new FormControl('' , [Validators.required,Validators.minLength(7),Validators.maxLength(7)] ),
  52. axes:new FormControl('',[Validators.required]),
  53. yearProduction:new FormControl('' , [Validators.required] ),
  54. datePurchase:new FormControl('' , [Validators.required ] ),
  55. dateReview:new FormControl('' , [Validators.required]),
  56. carType:new FormControl(),
  57. carStatus:new FormControl()
  58. });
  59.  
  60. saveCar(saveCar){
  61. this.car=new Car();
  62. this.car.mark = this.Mark.value;
  63. this.car.model = this.Model.value;
  64. this.car.registrationNumber = this.RegistrationNumber.value;
  65. this.car.axes = this.Axes.value;
  66. this.car.yearProduction = this.YearProduction.value;
  67. this.car.datePurchase = this.DatePurchase.value;
  68. this.car.dateReview = this.DateReview.value;
  69. this.car.carType = this.CarType.value;
  70. this.car.carStatus = this.CarStatus.value;
  71. this.submitted = true;
  72. this.save();
  73. }
  74.  
  75. save() {
  76. this.httpCarsService.createCar(this.car)
  77. .subscribe(data => console.log(data), error => console.log(error));
  78. this.car = new Car();
  79. }
  80.  
  81. get Mark() {
  82. return this.carsaveform.get('mark');
  83. }
  84.  
  85. get Model() {
  86. return this.carsaveform.get('model');
  87. }
  88.  
  89. get RegistrationNumber() {
  90. return this.carsaveform.get('registrationNumber');
  91. }
  92.  
  93. get Axes() {
  94. return this.carsaveform.get('axes');
  95. }
  96.  
  97. get YearProduction() {
  98. return this.carsaveform.get('yearProduction');
  99. }
  100.  
  101. get DatePurchase() {
  102. return this.carsaveform.get('datePurchase');
  103. }
  104.  
  105. get DateReview() {
  106. return this.carsaveform.get('dateReview');
  107. }
  108.  
  109. get CarType() {
  110. return this.carsaveform.get('carType');
  111. }
  112.  
  113. get CarStatus() {
  114. return this.carsaveform.get('carStatus');
  115. }
  116.  
  117. addCarForm(){
  118. this.submitted=false;
  119. this.carsaveform.reset();
  120. }
  121.  
  122. openSnackBar(message: string, action: string) {
  123. this.snackBar.open(message, action, {
  124. duration: 2000,
  125. });
  126. }
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment