Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. import { Component, Input, OnInit, ViewChild } from '@angular/core';
  2.  
  3. import { Car } from '../../../models/car';
  4. import { CarService, ICarOptionsBackend } from '../../../services/car.service';
  5. import { CarOption } from '../../../models/tradein/car-option';
  6.  
  7. import {
  8. AppPanelComponent
  9. } from '../../../modules/app-panel/app-panel.component';
  10.  
  11. @Component({
  12. selector: 'app-car-card-options',
  13. templateUrl: 'car-options.component.html',
  14. styleUrls: ['car-options.component.scss']
  15. })
  16. export class CarOptionsComponent implements OnInit {
  17. @ViewChild(AppPanelComponent)
  18. private panelContainer: AppPanelComponent;
  19.  
  20. @Input()
  21. private car: Car;
  22.  
  23. private parentOptions: Array<CarOption> = [];
  24.  
  25. private availableParentOptions: Array<CarOption> = [];
  26. private availableOptions: Array<CarOption> = [];
  27.  
  28. private editing = false;
  29.  
  30. constructor(private carService: CarService) {
  31. this
  32. .carService
  33. .equipmentChanged
  34. .subscribe(() => {
  35. this.loadOptions();
  36. });
  37. }
  38.  
  39. ngOnInit() {
  40. this.panelContainer.loading = true;
  41. this.car.options = [];
  42.  
  43. this.loadOptions();
  44. }
  45.  
  46. public edit(): void {
  47. this.editing = true;
  48. this.panelContainer.loading = true;
  49. this
  50. .carService
  51. .editCarOptions(this.car.id)
  52. .subscribe((response: ICarOptionsBackend) => {
  53. let options = response.options.map(opt => new CarOption(opt));
  54. this.availableOptions = CarOption.setChecked(options, this.car.options);
  55. this.availableParentOptions =
  56. response.parent_options.map(opt => new CarOption(opt));
  57. this.panelContainer.loading = false;
  58. }, () => {
  59. this.panelContainer.loading = false;
  60. });
  61. }
  62.  
  63. public cancel(): void {
  64. this.editing = false;
  65. this.availableParentOptions = [];
  66. this.availableOptions = [];
  67. }
  68.  
  69. public update(): void {
  70. this.panelContainer.loading = true;
  71. this.car.options = [];
  72.  
  73. this
  74. .carService
  75. .updateCarOptions(this.car.id, this.availableOptions)
  76. .subscribe((response: ICarOptionsBackend) => {
  77. this.car.options =
  78. response.options.map(opt => new CarOption(opt));
  79. this.parentOptions =
  80. response.parent_options.map(opt => new CarOption(opt));
  81. this.panelContainer.loading = this.editing = false;
  82. }, () => {
  83. this.panelContainer.loading = false;
  84. });
  85. }
  86.  
  87. private loadOptions(): void {
  88. this
  89. .carService
  90. .getCarOptions(this.car.id)
  91. .subscribe((response: ICarOptionsBackend) => {
  92. this.car.options =
  93. response.options.map(opt => new CarOption(opt));
  94. this.parentOptions =
  95. response.parent_options.map(opt => new CarOption(opt));
  96. this.panelContainer.loading = false;
  97. }, () => {
  98. this.panelContainer.loading = false;
  99. });
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement