add-lecture.component.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { AuthServiceService } from './../../../shared/auth-service.service';
  2. import { Location } from '@angular/common';
  3. import { Component, OnInit, ViewChild } from '@angular/core';
  4. import { Router, ActivatedRoute, Params } from '@angular/router';
  5. import { UserService } from '../../../shared/user.service';
  6. import { ToastrService } from 'ngx-toastr';
  7. import { NgxSpinnerService } from 'ngx-spinner';
  8. import { FormGroup, NgForm } from '@angular/forms';
  9. import { LectureService } from '../../../shared/lecture.service';
  10. @Component({
  11. selector: 'app-add-lecture',
  12. templateUrl: './add-lecture.component.html',
  13. styleUrls: ['./add-lecture.component.css']
  14. })
  15. export class AddLectureComponent implements OnInit {
  16. @ViewChild('f') lectureForm: NgForm;
  17. typeMode: boolean = false;
  18. typeLink: string = '';
  19. lectureId: number;
  20. lecture = {
  21. name: '',
  22. name_en: '',
  23. ranking: '',
  24. display_location: '',
  25. end_time: '',
  26. lecture_time: '',
  27. status: '',
  28. description: '',
  29. description_en: '',
  30. }
  31. constructor(private lectureServices: LectureService,
  32. private userService: UserService,
  33. private location: Location,
  34. private toastr: ToastrService,
  35. private authSer: AuthServiceService,
  36. private route: ActivatedRoute,
  37. private spineer: NgxSpinnerService) { }
  38. ngOnInit() {
  39. //show / hide notification search in header
  40. this.authSer.notificationLogin = true;
  41. this.authSer.showSearchHeader = false;
  42. this.authSer.showHeaderLogin = false;
  43. this.authSer.showHeaderDashBoard = true;
  44. this.authSer.showDashboardHeader = true;
  45. this.authSer.internalHeader = false;
  46. this.route.params.subscribe(
  47. (params: Params) => {
  48. if(params['typeLectureMode'] == 'edit') {
  49. this.typeLink = 'تعديل';
  50. this.spineer.show();
  51. this.typeMode = true;
  52. this.lectureId = params['editLectureId'];
  53. this.lectureServices.getLectureData(this.lectureId).subscribe(
  54. (responce) => {
  55. console.log(responce);
  56. const lectureData = responce['lecture'];
  57. console.log(lectureData);
  58. this.lecture.name = lectureData.name;
  59. this.lecture.name_en = lectureData.name_en;
  60. this.lecture.ranking = lectureData.ranking;
  61. this.lecture.display_location = lectureData.display_location;
  62. this.lecture.lecture_time = lectureData.lecture_time.split(' ').join("T");
  63. this.lecture.end_time = lectureData.end_time.split(' ').join("T");;
  64. this.lecture.status = lectureData.status;
  65. this.lecture.description = lectureData.description;
  66. this.lecture.description_en = lectureData.description_en;
  67. this.spineer.hide();
  68. },
  69. (error) => {
  70. console.log(error);
  71. }
  72. )
  73. } else {
  74. this.typeLink = 'إنشاء جديد';
  75. }
  76. }
  77. );
  78. }
  79. //submitted form
  80. onSubmitted() {
  81. console.log(this.lectureForm.value);
  82. this.lectureForm.value['lecture_time'] = this.lectureForm.value['lecture_time'].split('T').join(" ");
  83. this.lectureForm.value['end_time'] = this.lectureForm.value['end_time'].split('T').join(" ");
  84. console.log(this.lectureForm.value['lecture_time']);
  85. if(this.typeMode) {
  86. this.lectureServices.editLecture(this.lectureForm.value , this.lectureId).subscribe(
  87. (responce) => {
  88. console.log(responce);
  89. this.toastr.success('تمت التعديل بنجاح');
  90. this.location.back();
  91. },
  92. (error) => {
  93. this.toastr.error('حدث خطأ !');
  94. console.log(error);
  95. }
  96. );
  97. } else {
  98. this.lectureServices.addLecture(this.lectureForm.value).subscribe(
  99. (responce) => {
  100. console.log(responce);
  101. this.toastr.success('تمت الاضافه بنجاح');
  102. this.location.back();
  103. },
  104. (error) => {
  105. this.toastr.error('حدث خطأ !');
  106. console.log(error);
  107. }
  108. );
  109. }
  110. }
  111. }