spell-table.component.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { Component } from '@angular/core';
  2. import { DataService } from 'src/services/data/data.service';
  3. import { ModalService } from 'src/services/modal/modal.service';
  4. import { DetailsService } from 'src/services/details/details.service';
  5. import { CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop';
  6. import { Spell } from 'src/interfaces/spell';
  7. import { FullSpellcardComponent } from 'src/app/shared-components/full-spellcard/full-spellcard.component';
  8. import { FavoriteSpellsModalComponent } from './favorite-spells-modal/favorite-spells-modal.component';
  9. import { TranslateService } from '@ngx-translate/core';
  10. import { UtilsService } from 'src/services/utils/utils.service';
  11. @Component({
  12. selector: 'spell-table',
  13. templateUrl: './spell-table.component.html',
  14. styleUrls: ['./spell-table.component.scss'],
  15. })
  16. export class SpellTableComponent {
  17. public spells!: Spell[];
  18. private preparedSpells!: Spell[];
  19. public newSpellName: string = '';
  20. public constructor(
  21. public dataAccessor: DataService,
  22. private modalAccessor: ModalService,
  23. public detailsAccessor: DetailsService,
  24. public translate: TranslateService,
  25. public utils: UtilsService,
  26. ) {}
  27. public ngOnInit(): void {
  28. this.spells = this.dataAccessor.favoriteSpells;
  29. this.preparedSpells = this.dataAccessor.getAllPreparedSpells();
  30. }
  31. public showFullSpellcard(spellIndex: number): void {
  32. const spell = this.spells[spellIndex];
  33. this.modalAccessor.openModal(FullSpellcardComponent, {
  34. spell: spell,
  35. isFromDashboard: true,
  36. });
  37. const resultSubscription = this.modalAccessor.result$.subscribe(
  38. (result) => {
  39. resultSubscription.unsubscribe();
  40. if (result.state === 'remove') {
  41. this.spells.splice(spellIndex, 1);
  42. this.updateSpellsInDatabase();
  43. } else if (result.state === 'removeFromFavorites') {
  44. this.dataAccessor.removeFavoriteSpell(spell);
  45. }
  46. },
  47. );
  48. }
  49. public openModal(): void {
  50. this.modalAccessor.openModal(FavoriteSpellsModalComponent, {
  51. preparedSpells: this.preparedSpells,
  52. selectedSpells: this.spells,
  53. });
  54. const resultSubscription = this.modalAccessor.result$.subscribe(
  55. (result) => {
  56. if (result.state === 'update') {
  57. this.spells = result.data;
  58. this.updateSpellsInDatabase();
  59. }
  60. resultSubscription.unsubscribe();
  61. },
  62. );
  63. }
  64. public deleteSpell(index: number): void {
  65. this.spells.splice(index, 1);
  66. this.updateSpellsInDatabase();
  67. }
  68. public dropSpells(event: CdkDragDrop<string[]>): void {
  69. moveItemInArray(this.spells, event.previousIndex, event.currentIndex);
  70. this.updateSpellsInDatabase();
  71. }
  72. public updateSpellsInDatabase(): void {
  73. this.dataAccessor.favoriteSpells = this.spells;
  74. }
  75. }