您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

111 行
3.8 KiB

  1. import {AfterViewInit, Component, EventEmitter, Input, OnInit, Output, ViewChild} from '@angular/core';
  2. import {
  3. PartnerJsonld,
  4. ProductJsonld,
  5. ProductService,
  6. TaskJsonld,
  7. TaskService,
  8. UserService
  9. } from "@app/core/api/v1";
  10. import {ModalStatus} from "@app/_helpers/modal.states";
  11. import {FormGroupInitializer} from "@app/_helpers/formgroup.initializer";
  12. import {FormGroup} from "@angular/forms";
  13. import {taskForm} from "@app/_forms/apiForms";
  14. import {AppHelperService} from "@app/_helpers/app-helper.service";
  15. import {SearchSelectComponent} from "@app/_components/search-select/search-select.component";
  16. import {ListGetDataFunctionType} from "@app/_components/list/list-get-data-function-type";
  17. @Component({
  18. selector: 'app-new-task',
  19. templateUrl: './new-task.component.html',
  20. styleUrl: './new-task.component.scss'
  21. })
  22. export class NewTaskComponent implements OnInit, AfterViewInit {
  23. @Input() public task!: TaskJsonld;
  24. @Input() public partner!: PartnerJsonld;
  25. @Output() public submit: EventEmitter<ModalStatus> = new EventEmitter<ModalStatus>();
  26. @ViewChild('userSearchSelect', {static: false}) userSearchSelect!: SearchSelectComponent;
  27. @ViewChild('productSearchSelect', {static: false}) productSearchSelect!: SearchSelectComponent;
  28. protected readonly SearchSelectComponent = SearchSelectComponent;
  29. protected taskForm: FormGroup;
  30. protected dueAtValue: string;
  31. constructor(
  32. protected taskService: TaskService,
  33. protected userService: UserService,
  34. protected productService: ProductService,
  35. protected appHelperService: AppHelperService
  36. ) {
  37. this.taskForm = taskForm;
  38. this.dueAtValue = "";
  39. }
  40. ngOnInit(): void {
  41. this.taskForm = FormGroupInitializer.initFormGroup(this.taskForm, this.task);
  42. if (this.taskForm.get('dueAt')?.value !== null) {
  43. this.taskForm.get('dueAt')?.setValue(this.taskForm.value.dueAt.slice(0, 10));
  44. this.dueAtValue = this.taskForm.get('dueAt')?.value;
  45. }
  46. const prio = this.taskForm.get('prio');
  47. // Set first radio checked
  48. if (prio && !prio.value) {
  49. prio.patchValue('low');
  50. }
  51. }
  52. ngAfterViewInit(): void {
  53. }
  54. getUsers: ListGetDataFunctionType = (index: number, pageSize: number, term?: string) => {
  55. return this.userService.usersGetCollection(
  56. index,
  57. pageSize,
  58. undefined,
  59. undefined,
  60. term
  61. );
  62. }
  63. getProducts: ListGetDataFunctionType = (index: number, pageSize: number, term?: string) => {
  64. return this.productService.productsGetCollection(
  65. index,
  66. pageSize,
  67. term
  68. );
  69. }
  70. protected onDueAtChange(selectedItem: any) {
  71. // Set T12:00 for correct string
  72. let selectedItemValue = selectedItem.target.value + "T12:00";
  73. this.taskForm.get('dueAt')?.setValue(selectedItemValue);
  74. }
  75. protected onSubmit() {
  76. if (this.taskForm.valid) {
  77. if (this.task.id === null || this.task.id === undefined) {
  78. // Create new task
  79. this.taskService.tasksPost(
  80. this.taskForm.value as TaskJsonld
  81. ).subscribe(
  82. data => {
  83. this.taskForm.reset();
  84. this.submit.emit(ModalStatus.Submitted);
  85. }
  86. );
  87. } else {
  88. // Edit task
  89. this.taskService.tasksIdPatch(
  90. this.appHelperService.extractId(this.task.id),
  91. this.taskForm.value as TaskJsonld
  92. ).subscribe(
  93. data => {
  94. this.taskForm.reset();
  95. this.submit.emit(ModalStatus.Submitted);
  96. }
  97. );
  98. }
  99. }
  100. }
  101. }