From 695fde5fd2a8d54e785feaa8293a4b5829b96b60 Mon Sep 17 00:00:00 2001 From: Kyle Kemp Date: Wed, 8 May 2024 12:13:31 -0500 Subject: [PATCH] closes #39 --- src/app/card/card.page.html | 4 ++-- src/app/card/card.page.ts | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/app/card/card.page.html b/src/app/card/card.page.html index bf1b415..4781232 100644 --- a/src/app/card/card.page.html +++ b/src/app/card/card.page.html @@ -3,7 +3,7 @@ -
+
@if(cardData(); as cardData) { @@ -49,7 +49,7 @@ @if(faq().length > 0) { - + {{ 'Pages.Card.FAQ' | translate }} diff --git a/src/app/card/card.page.ts b/src/app/card/card.page.ts index 417b48d..6abf5ca 100644 --- a/src/app/card/card.page.ts +++ b/src/app/card/card.page.ts @@ -3,6 +3,9 @@ import { computed, inject, signal, + viewChild, + type ElementRef, + type OnDestroy, type OnInit, type Signal, type WritableSignal, @@ -20,7 +23,9 @@ import { FAQService } from '../faq.service'; templateUrl: './card.page.html', styleUrls: ['./card.page.scss'], }) -export class CardPage implements OnInit { +export class CardPage implements OnInit, OnDestroy { + private cardPage = viewChild('cardPage'); + private router = inject(Router); private route = inject(ActivatedRoute); private cardsService = inject(CardsService); @@ -37,6 +42,8 @@ export class CardPage implements OnInit { return this.faqService.getCardFAQ(cardData.game, cardData.name); }); + private clickListener!: () => void; + ngOnInit() { const cardId = this.route.snapshot.paramMap.get('id'); const cardData = this.cardsService.getCardById(cardId ?? ''); @@ -51,6 +58,30 @@ export class CardPage implements OnInit { this.template = compiledTemplate(cardData); this.cardData.set(cardData); + + this.clickListener = this.cardPage()?.nativeElement.addEventListener( + 'click', + (evt: Event) => { + evt.stopPropagation(); + evt.preventDefault(); + + const href = (evt.target as HTMLAnchorElement)?.href; + if (!href) return; + + const url = new URL(href); + const [, , cardId] = url.pathname.split('/'); + this.router.navigate(['/card', decodeURIComponent(cardId)]); + } + ); + } + + ngOnDestroy() { + if (this.clickListener) { + this.cardPage()?.nativeElement.removeEventListener( + 'click', + this.clickListener + ); + } } search(query: string) {