Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

192 linhas
6.5 KiB

  1. import {AfterViewInit, ChangeDetectorRef, Component, OnInit, ViewChild} from '@angular/core';
  2. import {User} from '@app/_models';
  3. import {AccountService} from '@app/_services';
  4. import {Subscription} from "rxjs";
  5. import {
  6. PostJsonld,
  7. PostService,
  8. TaskJsonld,
  9. TaskNoteJsonld,
  10. TaskService,
  11. UserJsonld,
  12. UserService
  13. } from "@app/core/api/v1";
  14. import {MatPaginator, MatPaginatorIntl, PageEvent} from "@angular/material/paginator";
  15. import {NgbModal, NgbModalOptions} from "@ng-bootstrap/ng-bootstrap";
  16. import {MatTableDataSource} from "@angular/material/table";
  17. import {NewTaskComponent} from "@app/tasks/new-task/new-task.component";
  18. import {ModalStatus} from "@app/_helpers/modal.states";
  19. import {NewTaskNoteComponent} from "@app/tasks/new-task-note/new-task-note.component";
  20. import {ApiConverter} from "@app/_helpers/api.converter";
  21. @Component({
  22. templateUrl: 'home.component.html',
  23. styleUrl: 'home.component.scss'
  24. })
  25. export class HomeComponent implements OnInit, AfterViewInit {
  26. @ViewChild(MatPaginator) tasksPaginator: MatPaginator;
  27. protected user: User | null;
  28. protected postSub: Subscription;
  29. protected posts: Array<PostJsonld>;
  30. protected usersSub: Subscription;
  31. protected users: Array<UserJsonld>;
  32. protected userIsAdmin: boolean;
  33. protected readonly ApiConverter = ApiConverter;
  34. protected tasksSub: Subscription;
  35. protected tasks: Array<TaskJsonld>;
  36. protected tasksDataSource;
  37. protected tasksLength: number;
  38. protected tasksPageEvent: PageEvent;
  39. protected tasksPageSize: number;
  40. protected tasksPageIndex: number;
  41. protected taskNotesVisibility: Map<string, boolean>;
  42. protected modalOptions: NgbModalOptions = {
  43. centered: true
  44. };
  45. constructor(
  46. private modalService: NgbModal,
  47. private accountService: AccountService,
  48. private postService: PostService,
  49. private userService: UserService,
  50. private taskService: TaskService
  51. ) {
  52. this.user = this.accountService.userValue;
  53. // this.accountService.user.subscribe(x => this.user = x);
  54. this.postSub = new Subscription();
  55. this.posts = [];
  56. this.usersSub = new Subscription();
  57. this.users = [];
  58. this.userIsAdmin = false;
  59. this.tasksSub = new Subscription();
  60. this.tasks = [];
  61. this.tasksPaginator = new MatPaginator(new MatPaginatorIntl(), ChangeDetectorRef.prototype);
  62. this.tasksDataSource = new MatTableDataSource<TaskJsonld>(this.tasks);
  63. this.tasksLength = 0;
  64. this.tasksPageEvent = new PageEvent();
  65. this.tasksPageSize = 10;
  66. this.tasksPageIndex = 0;
  67. this.taskNotesVisibility = new Map<string, boolean>();
  68. }
  69. ngOnInit(): void {
  70. if (this.user) {
  71. this.userIsAdmin = this.user.roles ? this.user.roles.includes('ROLE_ADMIN') : false;
  72. }
  73. this.postSub = this.postService.postsGetCollection().subscribe(
  74. data => {
  75. this.posts = data["hydra:member"];
  76. }
  77. );
  78. this.usersSub = this.userService.usersGetCollection().subscribe(
  79. data => {
  80. this.users = data["hydra:member"];
  81. }
  82. );
  83. this.getTasksData();
  84. }
  85. ngAfterViewInit() {
  86. this.tasksDataSource.paginator = this.tasksPaginator;
  87. }
  88. getTasksData() {
  89. console.log(this.user?.id);
  90. this.tasksSub = this.taskService.tasksGetCollection(
  91. this.tasksPageIndex + 1,
  92. this.tasksPageSize,
  93. "",
  94. [],
  95. this.user?.id
  96. ).subscribe(
  97. data => {
  98. this.tasks = data["hydra:member"];
  99. this.tasksLength = Number(data["hydra:totalItems"]);
  100. this.tasks.forEach(task => {
  101. if (task.id) {
  102. this.taskNotesVisibility.set(task.id, false);
  103. }
  104. });
  105. console.log(this.tasks);
  106. }
  107. );
  108. }
  109. tasksHandlePageEvent(e: PageEvent) {
  110. this.tasksPageEvent = e;
  111. this.tasksLength = e.length;
  112. this.tasksPageIndex = e.pageIndex.valueOf();
  113. this.tasksPageSize = e.pageSize.valueOf();
  114. this.getTasksData();
  115. }
  116. openModalNewTask() {
  117. const modalRefTask = this.modalService.open(NewTaskComponent, this.modalOptions);
  118. let task: TaskJsonld = {} as TaskJsonld;
  119. task.partner = null;
  120. task.completed = false;
  121. modalRefTask.componentInstance.task = task;
  122. modalRefTask.componentInstance.submit.subscribe((modalStatus: ModalStatus) => {
  123. if (modalStatus === ModalStatus.Submitted) {
  124. modalRefTask.dismiss();
  125. this.getTasksData();
  126. }
  127. });
  128. }
  129. openModalNewTaskNote(task: TaskJsonld) {
  130. const modalRefTaskNote = this.modalService.open(NewTaskNoteComponent, this.modalOptions);
  131. let taskNote: TaskNoteJsonld = {} as TaskNoteJsonld;
  132. taskNote.task = task.id ?? null;
  133. modalRefTaskNote.componentInstance.taskNote = taskNote;
  134. modalRefTaskNote.componentInstance.submit.subscribe((modalStatus: ModalStatus) => {
  135. if (modalStatus === ModalStatus.Submitted) {
  136. modalRefTaskNote.dismiss();
  137. this.getTasksData();
  138. }
  139. });
  140. }
  141. openModalEditTask(task: TaskJsonld) {
  142. const modalRefTaskEdit = this.modalService.open(NewTaskComponent, this.modalOptions);
  143. modalRefTaskEdit.componentInstance.task = task;
  144. modalRefTaskEdit.componentInstance.dueAtValue = ApiConverter.convertDate(task.dueAt);
  145. modalRefTaskEdit.componentInstance.submit.subscribe((modalStatus: ModalStatus) => {
  146. if (modalStatus === ModalStatus.Submitted) {
  147. modalRefTaskEdit.dismiss();
  148. this.getTasksData();
  149. }
  150. });
  151. }
  152. openModalEditTaskNote(taskNote: TaskNoteJsonld) {
  153. const modalRefTaskNote = this.modalService.open(NewTaskNoteComponent, this.modalOptions);
  154. modalRefTaskNote.componentInstance.taskNote = taskNote;
  155. modalRefTaskNote.componentInstance.submit.subscribe((modalStatus: ModalStatus) => {
  156. if (modalStatus === ModalStatus.Submitted) {
  157. modalRefTaskNote.dismiss();
  158. this.getTasksData();
  159. }
  160. });
  161. }
  162. showTaskNotes(task: TaskJsonld) {
  163. if (task.id) {
  164. const currentVisibility = this.taskNotesVisibility.get(task.id);
  165. this.taskNotesVisibility.set(task.id, !currentVisibility);
  166. }
  167. }
  168. }