add-external.component.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. typeLink: string = '';
  24. externalId: number;
  25. external = {
  26. name: '',
  27. name_en: '',
  28. link: '',
  29. status: '',
  30. ranking: '',
  31. };
  32. constructor(private toastr: ToastrService,
  33. private spinner: NgxSpinnerService,
  34. private location: Location,
  35. private route: ActivatedRoute,
  36. private authSer: AuthServiceService,
  37. private authService: AuthServiceService,
  38. private externalService: ExternalSerService) { }
  39. ngOnInit() {
  40. //show / hide notification search in header
  41. this.authSer.notificationLogin = true;
  42. this.authSer.showSearchHeader = false;
  43. this.authSer.showHeaderLogin = false;
  44. this.authSer.showHeaderDashBoard = true;
  45. this.authSer.showDashboardHeader = true;
  46. this.authSer.internalHeader = false;
  47. this.route.params.subscribe(
  48. (params: Params) => {
  49. if(params['typeExternalMode'] == 'edit') {
  50. this.spinner.show();
  51. this.typeMode = true;
  52. this.typeLink = 'تعديل';
  53. this.externalId = params['editExternalId'];
  54. this.externalService.getExternalData(this.externalId).subscribe(
  55. (responce) => {
  56. console.log(responce['external_service']);
  57. this.external.name = responce['external_service'].name;
  58. this.external.name_en = responce['external_service'].name_en;
  59. this.external.link = responce['external_service'].link;
  60. this.external.status = responce['external_service'].status;
  61. this.external.ranking = responce['external_service'].ranking;
  62. if(responce['external_service'].photo) {
  63. this.checkValidImg = false;
  64. this.urlImg = this.authService.pathImg + responce['external_service'].photo;
  65. }
  66. this.spinner.hide();
  67. },
  68. (error) => {
  69. console.log(error);
  70. }
  71. )
  72. } else {
  73. this.typeLink = 'أنشاء جديد';
  74. }
  75. }
  76. )
  77. };
  78. onFileChanges(event) {
  79. console.log(event);
  80. this.imageBase64 = event[0].base64;
  81. this.photoType = event[0].type.split('/');
  82. console.log(this.photoType[1]);
  83. this.checkChangeImage = true;
  84. this.checkValidImg = false;
  85. }
  86. getUrl(event) {
  87. if (event.target.files && event.target.files[0]) {
  88. var reader = new FileReader();
  89. reader.readAsDataURL(event.target.files[0]); // read file as data url
  90. reader.onload = (event) => { // called once readAsDataURL is completed
  91. this.urlImg = event.target['result'];
  92. }
  93. }
  94. }
  95. //on submitted
  96. onSubmitted() {
  97. const formData = this.externalFormData.value;
  98. if(this.checkChangeImage){
  99. formData['photo'] = this.imageBase64;
  100. formData['photo_type'] = this.photoType[1];
  101. } else {
  102. delete formData.photo;
  103. delete formData.photo_type;
  104. this.photoEdit = false;
  105. console.log(formData);
  106. }
  107. if(this.typeMode){
  108. if(this.photoType[1] != 'png' && this.photoEdit == true) {
  109. this.toastr.warning('الصوره يجب أن تكون بصيغه Png');
  110. } else if(this.imageBase64 == '' && this.photoEdit == true){
  111. this.toastr.warning('قم باختيار صوره !');
  112. } else {
  113. this.externalService.editExternal(formData, this.externalId).subscribe(
  114. (responce) => {
  115. console.log(responce);
  116. this.toastr.success('تمت التعديل بنجاح');
  117. this.location.back();
  118. },
  119. (error) => {
  120. console.log(error);
  121. this.toastr.error('خطأ في التعديل !');
  122. }
  123. );
  124. }
  125. } else {
  126. if(this.photoType[1] != 'png') {
  127. this.toastr.warning('الصوره يجب أن تكون بصيغه Png');
  128. } else if(this.imageBase64 == ''){
  129. this.toastr.warning('قم باختيار صوره !');
  130. } else {
  131. this.externalService.addExternal(formData).subscribe(
  132. (responce) => {
  133. console.log(responce);
  134. this.toastr.success('تمت الاضافه بنجاح');
  135. this.location.back();
  136. },
  137. (error) => {
  138. console.log(error);
  139. this.toastr.error('خطأ في الحفظ !');
  140. }
  141. );
  142. }
  143. }
  144. }
  145. }