character-picker.component.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import {
  2. Component,
  3. inject,
  4. QueryList,
  5. TemplateRef,
  6. ViewChildren,
  7. ViewChild,
  8. } from '@angular/core';
  9. import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
  10. import { DataService } from 'src/services/data/data.service';
  11. import { Router } from '@angular/router';
  12. import { CharacterCardComponent } from './character-card/character-card.component';
  13. @Component({
  14. selector: 'app-character-picker',
  15. templateUrl: './character-picker.component.html',
  16. styleUrls: ['./character-picker.component.scss'],
  17. })
  18. export class CharacterPickerComponent {
  19. public characters: any[] = [];
  20. public currentCharacter: string = '';
  21. private modalService = inject(NgbModal);
  22. @ViewChildren(CharacterCardComponent)
  23. characterCards!: QueryList<CharacterCardComponent>;
  24. @ViewChild('warning') warning!: TemplateRef<any>;
  25. // @ViewChildren(warning) warning!: TemplateRef<any>;
  26. public constructor(
  27. public dataService: DataService,
  28. private Router: Router,
  29. ) {
  30. this.dataService.dataLoaded = false;
  31. this.dataService.getCollection('characters').then((characters: any) => {
  32. this.characters = characters;
  33. });
  34. }
  35. ngAfterViewInit() {
  36. this.showWarning(this.warning);
  37. }
  38. public addCharacter() {
  39. this.Router.navigate(['character/creator']);
  40. }
  41. open(content: TemplateRef<any>, index: number) {
  42. this.currentCharacter = this.characters[index].name;
  43. this.modalService
  44. .open(content, { ariaLabelledBy: 'modal-basic-title' })
  45. .result.then(
  46. (result) => {},
  47. (reason) => {
  48. if (reason == 'delete') {
  49. this.deleteCharacter(index);
  50. }
  51. },
  52. );
  53. }
  54. public deleteCharacter(index: number) {
  55. this.characters.splice(index, 1);
  56. this.dataService.deleteCollection(this.currentCharacter);
  57. this.dataService.deleteCollection('characters');
  58. setTimeout(() => {
  59. this.dataService.setCollection('characters', this.characters);
  60. }, 200);
  61. setTimeout(() => {
  62. this.characterCards.forEach((card) => {
  63. card.loadCharacterData();
  64. });
  65. }, 500);
  66. }
  67. public selectCharacter(character: any) {
  68. console.log(character);
  69. sessionStorage.setItem('characterName', character.name);
  70. this.Router.navigate(['journal']);
  71. }
  72. showWarning(warning: TemplateRef<any>) {
  73. let warningWasAcknowledged = sessionStorage.getItem(
  74. 'warningWasAcknowledged',
  75. );
  76. if (!warningWasAcknowledged) {
  77. this.modalService.open(warning, { ariaLabelledBy: 'modal-basic-title' });
  78. }
  79. }
  80. public acknowledgeWarning() {
  81. sessionStorage.setItem('warningWasAcknowledged', 'true');
  82. }
  83. }