/client-side/admin/src/app/components/view-cars/view-car/view-car.component.ts

https://bitbucket.org/AhmedElMetwally/cars-rental · TypeScript · 279 lines · 228 code · 48 blank · 3 comment · 35 complexity · a05dce8e4b4f777c2e0f14f7c13eeef0 MD5 · raw file

  1. import { ICarModel } from '../../../models/carModel';
  2. import { CarService } from '../../../service/car.service';
  3. import { AuthService } from '../../../service/auth.service';
  4. import { ICarGroupModel } from '../../../models/carGroupModel';
  5. import { ActivatedRoute } from '@angular/router';
  6. import { TranslateService } from '@ngx-translate/core';
  7. import { SharedService } from '../../../service/shared.service';
  8. import { LoadingService } from '../../../service/loading.service';
  9. import { AlertService } from '../../../service/alert.service';
  10. import { Component, OnInit, OnDestroy, EventEmitter } from '@angular/core';
  11. import { LogService } from '../../../service/log.service';
  12. import { SelectItem } from 'primeng/components/common/selectitem';
  13. import { EnumValues } from 'enum-values';
  14. declare const $;
  15. @Component({
  16. selector: 'app-view-car',
  17. templateUrl: './view-car.component.html',
  18. styleUrls : ['view-car.component.scss']
  19. })
  20. export class ViewCarComponent implements OnInit , OnDestroy {
  21. constructor(
  22. private authService: AuthService,
  23. private alertService: AlertService,
  24. private loadingService: LoadingService,
  25. private translateService: TranslateService,
  26. private carService: CarService,
  27. private logService: LogService,
  28. private sharedService: SharedService,
  29. private activatedRoute: ActivatedRoute
  30. ) { };
  31. private translateServiceSubscribe: EventEmitter<any>;
  32. private activatedRouteSubscribe: any;
  33. private allCarGroupsCanThisEmployeeAccess: ICarGroupModel[];
  34. public carGroupsCanThisEmployeeAccessOptions: SelectItem[] = [];
  35. public gearTypesOptions: SelectItem[] = [];
  36. public statusOptions: SelectItem[] = [];
  37. public carGroupTitle: string;
  38. public car: ICarModel;
  39. public edit = {
  40. carGroup_id: false, plateNumber: false, gearType: false, status: false,
  41. };
  42. public car_id: string;
  43. public RegExpCarGroupModel = this.sharedService.mainRegExp.RegExpCarGroupModel;
  44. public RegExpCarModel = this.sharedService.mainRegExp.RegExpCarModel;
  45. // validationErrorMsg Enum
  46. public carGroup_id_required = this.sharedService.mainErrorEnum.carGroup_id_required;
  47. public carGroup_id_invalid = this.sharedService.mainErrorEnum.carGroup_id_invalid;
  48. public plateNumber_required = this.sharedService.mainErrorEnum.plateNumber_required;
  49. public plateNumber_invalid = this.sharedService.mainErrorEnum.plateNumber_invalid;
  50. public gearType_required = this.sharedService.mainErrorEnum.gearType_required;
  51. public gearType_invalid = this.sharedService.mainErrorEnum.gearType_invalid;
  52. public status_required = this.sharedService.mainErrorEnum.status_required;
  53. public status_invalid = this.sharedService.mainErrorEnum.status_invalid;
  54. // validationErrorMsg Enum
  55. // screens
  56. public canEditCar: boolean;
  57. public canDeleteCar: boolean;
  58. public canRestoreCar: boolean;
  59. private checkScreens(): void {
  60. const isHaveAccessToThisBranch = this.authService.checkBranch((this.car.carGroup as any).branch._id);
  61. this.canEditCar = isHaveAccessToThisBranch && this.authService.checkScreen(this.sharedService.Escreens.edit_car);
  62. this.canDeleteCar = isHaveAccessToThisBranch && this.authService.checkScreen(this.sharedService.Escreens.delete_car);
  63. this.canRestoreCar = isHaveAccessToThisBranch && this.authService.checkScreen(this.sharedService.Escreens.restore_car);
  64. };
  65. public openEdit($event): void {
  66. this.edit[$event.editNameInEvent] = true;
  67. };
  68. public closeEdit($event): void {
  69. this.edit[$event.editNameInEvent] = false;
  70. };
  71. private setCarGroupTitle(): void {
  72. const fieldName = (window as any).lang === 'ar' ? 'nameAr': 'nameEn'
  73. this.carGroupTitle = `
  74. ${(this.car.carGroup as any).branch[fieldName]} -
  75. ${(this.car.carGroup as any).carType[fieldName]} -
  76. ${(this.car.carGroup as any).carModel[fieldName]} -
  77. ${(this.car.carGroup as any).year}
  78. `;
  79. $('title').text(this.carGroupTitle);
  80. }
  81. private setStatusOptions(): void {
  82. this.statusOptions = EnumValues.getNamesAndValues(
  83. (window as any).lang === 'ar' ? this.sharedService.EstatusAr : this.sharedService.EstatusEn
  84. )
  85. .map( obj => {
  86. return {
  87. label : obj.name,
  88. value : obj.value
  89. }
  90. });
  91. };
  92. private setGearTypeOptions(): void {
  93. this.gearTypesOptions = EnumValues.getNamesAndValues(
  94. (window as any).lang === 'ar' ? this.sharedService.EgearTypeAr : this.sharedService.EgearTypeEn
  95. )
  96. .map( obj => {
  97. return {
  98. label : obj.name,
  99. value : obj.value
  100. }
  101. });
  102. };
  103. private setCarGroupsOptions(allCarGroups: ICarGroupModel[]): void {
  104. if((window as any).lang === 'ar') {
  105. this.carGroupsCanThisEmployeeAccessOptions = allCarGroups.map( carGroup => {
  106. const name = `${carGroup.branch.nameAr} - ${carGroup.carType.nameAr} <br> ${carGroup.carModel.nameAr} - ${carGroup.year}`
  107. return {
  108. label : name,
  109. value : carGroup._id,
  110. }
  111. });
  112. } else {
  113. this.carGroupsCanThisEmployeeAccessOptions = allCarGroups.map( carGroup => {
  114. const name = `${carGroup.branch.nameEn} - ${carGroup.carType.nameEn} <br> ${carGroup.carModel.nameEn} - ${carGroup.year}`
  115. return {
  116. label : name,
  117. value : carGroup._id,
  118. }
  119. });
  120. };
  121. };
  122. public setOptions(): void {
  123. this.setGearTypeOptions();
  124. this.setStatusOptions();
  125. if(this.allCarGroupsCanThisEmployeeAccess) {
  126. this.setCarGroupsOptions(this.allCarGroupsCanThisEmployeeAccess);
  127. } else{
  128. this.carService.getAllCarGroupsCanThisEmployeeAccess()
  129. .then( res => {
  130. this.allCarGroupsCanThisEmployeeAccess = res.carGroups;
  131. this.setCarGroupsOptions(res.carGroups);
  132. })
  133. .catch( e => this.alertService.httpError(e) );
  134. };
  135. };
  136. public onSaveEdit($event): void {
  137. const {fieldName , value} = $event;
  138. this.loadingService.show();
  139. const obj = {};
  140. obj[fieldName] = value;
  141. obj['car_id'] = this.car_id;
  142. this.carService.editCar(obj)
  143. .then(() => {
  144. this.loadingService.hide();
  145. this.edit[fieldName] = false;
  146. this.car.updatedAt = new Date();
  147. if(fieldName === 'carGroup_id') {
  148. this.car.carGroup = this.allCarGroupsCanThisEmployeeAccess.filter( v => v._id === value)[0];
  149. this.setCarGroupTitle();
  150. } else {
  151. this.car[fieldName] = value;
  152. };
  153. })
  154. .catch( e => this.alertService.httpError(e) );
  155. };
  156. public onValidationError($event): void {
  157. const {validationErrorMsg} = $event;
  158. this.alertService.alertError(validationErrorMsg);
  159. };
  160. private getCar(): void {
  161. this.loadingService.show();
  162. this.carService.getCar(this.car_id)
  163. .then(({ car }) => {
  164. this.car = car;
  165. this.loadingService.hide();
  166. this.afterGetCar();
  167. })
  168. .catch( e => this.alertService.httpError(e) );
  169. };
  170. private log(text): void {
  171. this.logService.log('employee-profile' , text);
  172. };
  173. private init(){
  174. this.setOptions();
  175. this.getCar_id();
  176. };
  177. private onLangChange(): void {
  178. this.setOptions();
  179. this.setCarGroupTitle();
  180. };
  181. public ngOnInit(): void {
  182. this.init();
  183. this.translateServiceSubscribe = this.translateService.onLangChange.subscribe(
  184. () => {
  185. this.onLangChange();
  186. }
  187. );
  188. };
  189. public ngOnDestroy(): void {
  190. this.translateServiceSubscribe.unsubscribe();
  191. this.activatedRouteSubscribe.unsubscribe();
  192. };
  193. private getCar_id(): void {
  194. this.activatedRouteSubscribe = this.activatedRoute.params.subscribe( params => {
  195. this.car_id = params['car_id'];
  196. this.afterGetCar_id();
  197. });
  198. };
  199. private afterGetCar_id(): void {
  200. this.getCar();
  201. };
  202. private afterGetCar(): void {
  203. this.checkScreens();
  204. this.setCarGroupTitle();
  205. };
  206. public delete(): void {
  207. this.carService.deleteCar(this.car_id)
  208. .then(() => {
  209. this.car.deleted = 1;
  210. this.alertService.done();
  211. })
  212. .catch(e => this.alertService.httpError(e))
  213. };
  214. public restore(): void {
  215. const obj = {
  216. car_id : this.car_id
  217. };
  218. this.carService.restoreCar(obj)
  219. .then(() => {
  220. this.car.deleted = 0;
  221. this.alertService.done();
  222. })
  223. .catch(e => this.alertService.httpError(e))
  224. };
  225. public getStatus(num): string {
  226. return (window as any).lang === 'ar' ? this.sharedService.EstatusAr[num] : this.sharedService.EstatusEn[num];
  227. }
  228. public getGearType(num): string {
  229. return (window as any).lang === 'ar' ? this.sharedService.EgearTypeAr[num] : this.sharedService.EgearTypeEn[num];
  230. }
  231. };