proficiencies-table.component.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { Component } from '@angular/core';
  2. import { CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop';
  3. import { DataService } from 'src/services/data/data.service';
  4. import { ModalService } from 'src/services/modal/modal.service';
  5. import { ToolsModalComponent } from './tools-modal/tools-modal.component';
  6. import { FormBuilder } from '@angular/forms';
  7. @Component({
  8. selector: 'proficiencies-table',
  9. templateUrl: './proficiencies-table.component.html',
  10. styleUrls: ['./proficiencies-table.component.scss'],
  11. })
  12. export class ProficienciesTableComponent {
  13. // public proficiencies!: any;
  14. public proficiencies = this.formBuilder.group({
  15. simple: false,
  16. martial: false,
  17. other: '',
  18. light: false,
  19. medium: false,
  20. heavy: false,
  21. shields: false,
  22. tools: this.formBuilder.array([]),
  23. languages: this.formBuilder.array([]),
  24. });
  25. public constructor(
  26. public dataAccessor: DataService,
  27. public modalAccessor: ModalService,
  28. private formBuilder: FormBuilder,
  29. ) {}
  30. public ngOnInit(): void {
  31. // this.proficiencies.setValue(this.dataAccessor.proficiencies);
  32. this.setProficiencies(this.dataAccessor.proficiencies);
  33. }
  34. public dropTools(event: CdkDragDrop<string[]>): void {
  35. moveItemInArray(
  36. this.proficiencies.get('tools')!.value,
  37. event.previousIndex,
  38. event.currentIndex,
  39. );
  40. this.updateDatabase();
  41. }
  42. public dropLanguages(event: CdkDragDrop<string[]>): void {
  43. moveItemInArray(
  44. this.proficiencies.get('languages')!.value,
  45. event.previousIndex,
  46. event.currentIndex,
  47. );
  48. this.updateDatabase();
  49. }
  50. public openModal(): void {
  51. this.modalAccessor.openModal(ToolsModalComponent, {
  52. proficiencies: JSON.parse(JSON.stringify(this.proficiencies.value)),
  53. });
  54. const resultSubscription = this.modalAccessor.result$.subscribe(
  55. (result) => {
  56. if (result.state === 'update') {
  57. this.setProficiencies(result.data);
  58. this.updateDatabase();
  59. } else if (result.state === 'cancel') {
  60. // Do nothing
  61. }
  62. resultSubscription.unsubscribe();
  63. },
  64. );
  65. }
  66. public updateDatabase(): void {
  67. console.warn(this.proficiencies.value);
  68. this.dataAccessor.proficiencies = this.proficiencies.value;
  69. }
  70. public setProficiencies(data: any): void {
  71. const { tools, languages, ...dataWithoutToolsAndLanguages } = data;
  72. Object.keys(dataWithoutToolsAndLanguages).forEach((key) => {
  73. this.proficiencies.get(key)?.setValue(dataWithoutToolsAndLanguages[key]);
  74. });
  75. this.proficiencies.setControl('tools', this.formBuilder.array(tools));
  76. this.proficiencies.setControl(
  77. 'languages',
  78. this.formBuilder.array(languages),
  79. );
  80. }
  81. }