add-external.component.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { AuthServiceService } from './../../../shared/auth-service.service';
  2. import { ActivatedRoute, Params } from '@angular/router';
  3. import { Location } from '@angular/common';
  4. import { ExternalSerService } from './../../../shared/external-ser.service';
  5. import { Component, OnInit, ViewChild } from '@angular/core';
  6. import { NgxSpinnerService } from 'ngx-spinner';
  7. import { ToastrService } from 'ngx-toastr';
  8. import { NgForm } from '@angular/forms';
  9. @Component({
  10. selector: 'app-add-external',
  11. templateUrl: './add-external.component.html',
  12. styleUrls: ['./add-external.component.css']
  13. })
  14. export class AddExternalComponent implements OnInit {
  15. @ViewChild('f') externalFormData: NgForm;
  16. urlImg: string = '../../assets/image/avatar.png';
  17. imageBase64: string = '';
  18. photoType: string = '';
  19. checkChangeImage: boolean = false;
  20. checkValidImg: boolean = true;
  21. typeMode: boolean = false;
  22. photoEdit: boolean = true;
  23. checkSaveClick:boolean = false;
  24. typeLink: string = '';
  25. externalId: number;
  26. external = {
  27. name: '',
  28. name_en: '',
  29. link: '',
  30. status: '',
  31. ranking: '',
  32. };
  33. constructor(private toastr: ToastrService,
  34. private spinner: NgxSpinnerService,
  35. private location: Location,
  36. private route: ActivatedRoute,
  37. private authSer: AuthServiceService,
  38. private authService: AuthServiceService,
  39. private externalService: ExternalSerService) { }
  40. ngOnInit() {
  41. //show / hide notification search in header
  42. this.authSer.notificationLogin = true;
  43. this.authSer.showSearchHeader = false;
  44. this.authSer.showHeaderLogin = false;
  45. this.authSer.showHeaderDashBoard = true;
  46. this.authSer.showDashboardHeader = true;
  47. this.authSer.internalHeader = false;
  48. this.external.status = "1";
  49. this.route.params.subscribe(
  50. (params: Params) => {
  51. if(params['typeExternalMode'] == 'edit') {
  52. this.spinner.show();
  53. this.typeMode = true;
  54. this.typeLink = 'تعديل';
  55. this.externalId = params['editExternalId'];
  56. this.externalService.getExternalData(this.externalId).subscribe(
  57. (responce) => {
  58. console.log(responce['external_service']);
  59. this.external.name = responce['external_service'].name;
  60. this.external.name_en = responce['external_service'].name_en;
  61. this.external.link = responce['external_service'].link;
  62. this.external.status = responce['external_service'].status;
  63. this.external.ranking = responce['external_service'].ranking;
  64. if(responce['external_service'].photo) {
  65. this.checkValidImg = false;
  66. this.urlImg = this.authService.pathImg + responce['external_service'].photo;
  67. }
  68. this.spinner.hide();
  69. },
  70. (error) => {
  71. console.log(error);
  72. }
  73. )
  74. } else {
  75. this.typeLink = 'أنشاء جديد';
  76. }
  77. }
  78. )
  79. };
  80. onFileChanges(event) {
  81. console.log(event);
  82. this.imageBase64 = event[0].base64;
  83. this.photoType = event[0].type.split('/');
  84. console.log(this.photoType[1]);
  85. this.checkChangeImage = true;
  86. this.checkValidImg = false;
  87. }
  88. getUrl(event) {
  89. if (event.target.files && event.target.files[0]) {
  90. var reader = new FileReader();
  91. reader.readAsDataURL(event.target.files[0]); // read file as data url
  92. reader.onload = (event) => { // called once readAsDataURL is completed
  93. this.urlImg = event.target['result'];
  94. }
  95. }
  96. }
  97. //on submitted
  98. onSubmitted() {
  99. this.checkSaveClick = true;
  100. const formData = this.externalFormData.value;
  101. if(this.checkChangeImage){
  102. formData['photo'] = this.imageBase64;
  103. formData['photo_type'] = this.photoType[1];
  104. } else {
  105. delete formData.photo;
  106. delete formData.photo_type;
  107. this.photoEdit = false;
  108. console.log(formData);
  109. }
  110. if(this.typeMode){
  111. if((this.photoType[1] != 'png' && this.photoType[1] != 'gif') && this.photoEdit == true) {
  112. this.toastr.warning('الصوره يجب أن تكون بصيغه Png');
  113. this.checkSaveClick = false;
  114. } else if(this.imageBase64 == '' && this.photoEdit == true){
  115. this.toastr.warning('قم باختيار صوره !');
  116. this.checkSaveClick = false;
  117. } else {
  118. this.externalService.editExternal(formData, this.externalId).subscribe(
  119. (responce) => {
  120. console.log(responce);
  121. this.toastr.success('تمت التعديل بنجاح');
  122. this.location.back();
  123. this.checkSaveClick = true;
  124. },
  125. (error) => {
  126. console.log(error);
  127. this.checkSaveClick = false;
  128. this.toastr.error('خطأ في التعديل !');
  129. }
  130. );
  131. }
  132. } else {
  133. if(this.photoType[1] != 'png' && this.photoType[1] != 'gif') {
  134. this.toastr.warning('الصوره يجب أن تكون بصيغه Png');
  135. this.checkSaveClick = false;
  136. } else if(this.imageBase64 == ''){
  137. this.toastr.warning('قم باختيار صوره !');
  138. this.checkSaveClick = false;
  139. } else {
  140. this.externalService.addExternal(formData).subscribe(
  141. (responce) => {
  142. console.log(responce);
  143. this.toastr.success('تمت الاضافه بنجاح');
  144. this.location.back();
  145. this.checkSaveClick = true;
  146. },
  147. (error) => {
  148. console.log(error);
  149. this.toastr.error('خطأ في الحفظ !');
  150. this.checkSaveClick = false;
  151. }
  152. );
  153. }
  154. }
  155. }
  156. }