Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. import { Component, OnInit } from '@angular/core';
  2. import { Injectable } from '@angular/core';
  3. import {CareerPathService} from '../../../career-path/services/career-path.service';
  4. import { FormGroup, FormControl } from '@angular/forms';
  5.  
  6.  
  7. @Injectable()
  8. export class ConfigService {
  9. constructor() { }
  10. }
  11. @Component({
  12. selector: 'app-edit-department',
  13. templateUrl: './edit-department.component.html',
  14. styleUrls: ['./edit-department.component.scss']
  15. })
  16. export class EditDepartmentComponent implements OnInit {
  17.  
  18. input_name: any;
  19. pic_name: any;
  20. option: {id: number, name: string, picture: string} = {id: 0, name: "new", picture:"new"};
  21.  
  22. discoverDescriptionForm: FormGroup;
  23. searchDepartmentOptions: { id: number, name: string, picture:string }[] = [];
  24.  
  25. sub_ed = {label:'Submit'};
  26. ed_create_dep = {label:'Create new department'};
  27.  
  28. constructor(private cpService: CareerPathService) {
  29.  
  30. }
  31.  
  32.  
  33. change(option)
  34. {
  35. if(option.id == '0')
  36. {
  37. this.input_name = "";
  38. this.pic_name = "";
  39. this.sub_ed.label = 'Submit';
  40. this.ed_create_dep.label = 'Create new department: ';
  41. }
  42. else
  43. {
  44. // this.departments.forEach(element => {
  45. // if(element.id==x.value)
  46. // urlValue.push(element);
  47. // });
  48.  
  49. this.input_name = option.name;
  50. this.pic_name = option.picture;
  51. this.sub_ed.label = 'Edit';
  52. this.ed_create_dep.label = 'Edit selected department: ';
  53. }
  54.  
  55. }
  56.  
  57. public departments = [];
  58. getDepart()
  59. {
  60. this.searchDepartmentOptions = [];
  61. this.searchDepartmentOptions.push({id:0, name: "New department", picture: "none"})
  62. this.cpService.getDepartments().subscribe((res =>{
  63. res.forEach(element => {
  64. this.searchDepartmentOptions.push(element)
  65. });
  66. }))
  67. }
  68.  
  69. postDepart()
  70. {
  71. this.cpService.postDepartment(this.input_name,this.pic_name).subscribe(res => { console.log(res)});
  72. this.getDepart();
  73. }
  74.  
  75. patchDepart() {
  76. var x = this.option.id;
  77. this.cpService.patchDepartment(this.input_name, this.pic_name,x)
  78. .subscribe(
  79. (val) => {
  80. console.log("PATCH call successful value returned in body",
  81. val);
  82. },
  83. response => {
  84. console.log("PATCH call in error", response);
  85. },
  86. () => {
  87. console.log("The PATCH observable is now completed.");
  88. });
  89.  
  90. this.getDepart();
  91. }
  92.  
  93. doRequest()
  94. {
  95. var x = this.option.id;
  96. console.log(x)
  97. if(x == 0)
  98. {
  99. this.postDepart();
  100. }
  101. else
  102. {
  103. this.patchDepart();
  104. }
  105. }
  106.  
  107.  
  108. ngOnInit() {
  109. this.getDepart();
  110. this.discoverDescriptionForm = new FormGroup({
  111. discoverDepartment: new FormControl(this.searchDepartmentOptions[0])
  112. });
  113.  
  114. this.discoverDescriptionForm.get('discoverDepartment').valueChanges
  115. .subscribe((option: {id: number, name: string, picture: string}) =>{
  116. console.log(option.name);
  117. this.change(option)
  118. this.option = option;
  119. })
  120. }
  121. }
  122.  
  123.  
  124. <div class="dep-add-edit-c" style="background-color: #e0f0f3;">
  125. <div class="add-dep">
  126. <form (ngSubmit)="doRequest()">
  127. <div class="form-group">
  128. <h2 style="margin-bottom:50px;">{{ed_create_dep.label}}</h2>
  129. <label for="name">Department name: </label>
  130. <input [(ngModel)]="input_name" type="text" class="form-control" id="name" required name="input-name" >
  131. </div>
  132.  
  133. <div class="form-group">
  134. <label for="picture">Image Url: </label>
  135. <input [(ngModel)]="pic_name" type="text" class="form-control" name="picture-name">
  136. </div>
  137. <button type="submit" class="dep-submit-btn" id="sub-add-button">{{sub_ed.label}}</button>
  138. </form>
  139. </div>
  140.  
  141. <div class="edit-dep">
  142. <h4 style="margin-bottom:50px;">Choose a department to be edited </h4>
  143. <form [formGroup]="discoverDescriptionForm">
  144. <div class="btn-group" dropdown>
  145. <label for="discoverDepartment">Departments</label>
  146. <select formControlName="discoverDepartment"
  147. class="form-control"
  148. id="discoverDepartment"
  149. >
  150. <option *ngFor="let option of searchDepartmentOptions"
  151. [ngValue]="option" >
  152. {{option.name}}
  153. </option>
  154. </select>
  155. </div>
  156. </form>
  157. </div>
  158. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement