From 1018549b053fafe4ead68cd833076eebf229a89e Mon Sep 17 00:00:00 2001 From: Kunta Mallik Raj <96738904+KuntaMallikRaj@users.noreply.github.com> Date: Fri, 29 May 2026 22:32:19 +0530 Subject: [PATCH] fix(author-browser): convert AuthorMatchComponent state to signals (#1581) Signed-off-by: Kunta Mallik Raj --- .../author-match/author-match.component.html | 25 +++++---- .../author-match.component.spec.ts | 56 +++++++++---------- .../author-match/author-match.component.ts | 55 +++++++++--------- 3 files changed, 68 insertions(+), 68 deletions(-) diff --git a/frontend/src/app/features/author-browser/components/author-match/author-match.component.html b/frontend/src/app/features/author-browser/components/author-match/author-match.component.html index 53ee37102..a5cefa9f6 100644 --- a/frontend/src/app/features/author-browser/components/author-match/author-match.component.html +++ b/frontend/src/app/features/author-browser/components/author-match/author-match.component.html @@ -4,18 +4,19 @@
- +
- +
@@ -26,14 +27,14 @@ [label]="t('searchBtn')" icon="pi pi-search" (onClick)="search()" - [loading]="searching" - [disabled]="!canSearch"> + [loading]="searching()" + [disabled]="!canSearch()">
- @if (searching) { + @if (searching()) {
} - @if (!searching && hasSearched && results.length === 0) { + @if (!searching() && hasSearched() && results().length === 0) {

{{ t('noResults') }}

@@ -53,7 +54,7 @@
} - @if (!searching && !hasSearched) { + @if (!searching() && !hasSearched()) {

{{ t('readyToSearch') }}

@@ -61,12 +62,12 @@
} - @if (!searching && results.length > 0) { + @if (!searching() && results().length > 0) {
- {{ t('resultsFound', {count: results.length}) }} + {{ t('resultsFound', {count: results().length}) }}
- @for (result of results; track result.asin) { + @for (result of results(); track result.asin) {
@if (result.imageUrl) { @@ -89,7 +90,7 @@ [label]="t('matchBtn')" icon="pi pi-link" severity="success" - [loading]="matching" + [loading]="matching()" (onClick)="matchAuthor(result)">
diff --git a/frontend/src/app/features/author-browser/components/author-match/author-match.component.spec.ts b/frontend/src/app/features/author-browser/components/author-match/author-match.component.spec.ts index 24685fab1..e8f7b26f6 100644 --- a/frontend/src/app/features/author-browser/components/author-match/author-match.component.spec.ts +++ b/frontend/src/app/features/author-browser/components/author-match/author-match.component.spec.ts @@ -62,29 +62,29 @@ describe('AuthorMatchComponent', () => { component.ngOnInit(); - expect(component.searchQuery).toBe(' Ada Lovelace '); - expect(component.canSearch).toBe(true); + expect(component.searchQuery()).toBe(' Ada Lovelace '); + expect(component.canSearch()).toBe(true); - component.searchQuery = ' '; - component.asinQuery = ' '; - expect(component.canSearch).toBe(false); + component.searchQuery.set(' '); + component.asinQuery.set(' '); + expect(component.canSearch()).toBe(false); - component.asinQuery = ' B00TEST '; - expect(component.canSearch).toBe(true); + component.asinQuery.set(' B00TEST '); + expect(component.canSearch()).toBe(true); }); it('does not search when both query inputs are blank after trimming', () => { const component = createComponent(); - component.searchQuery = ' '; - component.asinQuery = ' '; - component.results = [{source: 'seed', asin: 'old', name: 'Existing'}]; + component.searchQuery.set(' '); + component.asinQuery.set(' '); + component.results.set([{source: 'seed', asin: 'old', name: 'Existing'}]); component.search(); expect(searchAuthorMetadata).not.toHaveBeenCalled(); - expect(component.searching).toBe(false); - expect(component.hasSearched).toBe(false); - expect(component.results).toEqual([{source: 'seed', asin: 'old', name: 'Existing'}]); + expect(component.searching()).toBe(false); + expect(component.hasSearched()).toBe(false); + expect(component.results()).toEqual([{source: 'seed', asin: 'old', name: 'Existing'}]); }); it('searches with trimmed values, clears stale results, and stores returned matches', () => { @@ -92,17 +92,17 @@ describe('AuthorMatchComponent', () => { searchAuthorMetadata.mockReturnValue(results$); const component = createComponent(); - component.searchQuery = ' Ada '; - component.asinQuery = ' B00MATCH '; - component.selectedRegion = 'uk'; - component.results = [{source: 'seed', asin: 'old', name: 'Existing'}]; + component.searchQuery.set(' Ada '); + component.asinQuery.set(' B00MATCH '); + component.selectedRegion.set('uk'); + component.results.set([{source: 'seed', asin: 'old', name: 'Existing'}]); component.search(); expect(searchAuthorMetadata).toHaveBeenCalledWith(9, 'Ada', 'uk', 'B00MATCH'); - expect(component.searching).toBe(true); - expect(component.hasSearched).toBe(true); - expect(component.results).toEqual([]); + expect(component.searching()).toBe(true); + expect(component.hasSearched()).toBe(true); + expect(component.results()).toEqual([]); results$.next([ {source: 'amazon', asin: 'B00MATCH', name: 'Ada Lovelace'}, @@ -110,22 +110,22 @@ describe('AuthorMatchComponent', () => { ]); results$.complete(); - expect(component.results).toEqual([ + expect(component.results()).toEqual([ {source: 'amazon', asin: 'B00MATCH', name: 'Ada Lovelace'}, {source: 'amazon', asin: 'B00OTHER', name: 'A. Lovelace'}, ]); - expect(component.searching).toBe(false); + expect(component.searching()).toBe(false); }); it('reports a translated search failure toast when metadata search errors', () => { searchAuthorMetadata.mockReturnValue(throwError(() => new Error('boom'))); const component = createComponent(); - component.searchQuery = 'Ada'; + component.searchQuery.set('Ada'); component.search(); - expect(component.searching).toBe(false); + expect(component.searching()).toBe(false); expect(translate).toHaveBeenCalledWith('authorBrowser.match.toast.searchFailedSummary'); expect(translate).toHaveBeenCalledWith('authorBrowser.match.toast.searchFailedDetail'); expect(messageService.add).toHaveBeenCalledWith({ @@ -150,7 +150,7 @@ describe('AuthorMatchComponent', () => { matchAuthor.mockReturnValue(match$); const component = createComponent(); - component.selectedRegion = 'de'; + component.selectedRegion.set('de'); const emitSpy = vi.spyOn(component.authorMatched, 'emit'); component.matchAuthor({ @@ -165,12 +165,12 @@ describe('AuthorMatchComponent', () => { asin: 'B00MATCH', region: 'de', }); - expect(component.matching).toBe(true); + expect(component.matching()).toBe(true); match$.next(updatedAuthor); match$.complete(); - expect(component.matching).toBe(false); + expect(component.matching()).toBe(false); expect(translate).toHaveBeenCalledWith('authorBrowser.match.toast.matchSuccessSummary'); expect(translate).toHaveBeenCalledWith('authorBrowser.match.toast.matchSuccessDetail'); expect(messageService.add).toHaveBeenCalledWith({ @@ -193,7 +193,7 @@ describe('AuthorMatchComponent', () => { name: 'Ada Lovelace', }); - expect(component.matching).toBe(false); + expect(component.matching()).toBe(false); expect(translate).toHaveBeenCalledWith('authorBrowser.match.toast.matchFailedSummary'); expect(translate).toHaveBeenCalledWith('authorBrowser.match.toast.matchFailedDetail'); expect(messageService.add).toHaveBeenCalledWith({ diff --git a/frontend/src/app/features/author-browser/components/author-match/author-match.component.ts b/frontend/src/app/features/author-browser/components/author-match/author-match.component.ts index 9b45078f3..66a4fad7a 100644 --- a/frontend/src/app/features/author-browser/components/author-match/author-match.component.ts +++ b/frontend/src/app/features/author-browser/components/author-match/author-match.component.ts @@ -1,4 +1,4 @@ -import {Component, EventEmitter, inject, Input, OnInit, Output} from '@angular/core'; +import {ChangeDetectionStrategy, Component, computed, EventEmitter, inject, Input, OnInit, Output, signal} from '@angular/core'; import {FormsModule} from '@angular/forms'; import {Button} from 'primeng/button'; import {InputText} from 'primeng/inputtext'; @@ -17,6 +17,7 @@ interface RegionOption { @Component({ selector: 'app-author-match', standalone: true, + changeDetection: ChangeDetectionStrategy.OnPush, templateUrl: './author-match.component.html', styleUrls: ['./author-match.component.scss'], imports: [ @@ -38,13 +39,15 @@ export class AuthorMatchComponent implements OnInit { private messageService = inject(MessageService); private t = inject(TranslocoService); - searchQuery = ''; - asinQuery = ''; - selectedRegion = 'us'; - searching = false; - matching = false; - results: AuthorSearchResult[] = []; - hasSearched = false; + searchQuery = signal(''); + asinQuery = signal(''); + selectedRegion = signal('us'); + searching = signal(false); + matching = signal(false); + results = signal([]); + hasSearched = signal(false); + + canSearch = computed(() => !!this.searchQuery().trim() || !!this.asinQuery().trim()); regionOptions: RegionOption[] = [ {label: 'US', value: 'us'}, @@ -60,29 +63,25 @@ export class AuthorMatchComponent implements OnInit { ]; ngOnInit(): void { - this.searchQuery = this.authorName; - } - - get canSearch(): boolean { - return !!this.searchQuery.trim() || !!this.asinQuery.trim(); + this.searchQuery.set(this.authorName); } search(): void { - const asin = this.asinQuery.trim(); - const query = this.searchQuery.trim(); + const asin = this.asinQuery().trim(); + const query = this.searchQuery().trim(); if (!query && !asin) return; - this.searching = true; - this.results = []; - this.hasSearched = true; + this.searching.set(true); + this.results.set([]); + this.hasSearched.set(true); - this.authorService.searchAuthorMetadata(this.authorId, query, this.selectedRegion, asin || undefined) + this.authorService.searchAuthorMetadata(this.authorId, query, this.selectedRegion(), asin || undefined) .subscribe({ - next: (results) => { - this.results = results; - this.searching = false; + next: (results: AuthorSearchResult[]) => { + this.results.set(results); + this.searching.set(false); }, error: () => { - this.searching = false; + this.searching.set(false); this.messageService.add({ severity: 'error', summary: this.t.translate('authorBrowser.match.toast.searchFailedSummary'), @@ -94,16 +93,16 @@ export class AuthorMatchComponent implements OnInit { } matchAuthor(result: AuthorSearchResult): void { - this.matching = true; + this.matching.set(true); const request: AuthorMatchRequest = { source: result.source, asin: result.asin, - region: this.selectedRegion + region: this.selectedRegion() }; this.authorService.matchAuthor(this.authorId, request).subscribe({ - next: (updatedAuthor) => { - this.matching = false; + next: (updatedAuthor: AuthorDetails) => { + this.matching.set(false); this.messageService.add({ severity: 'success', summary: this.t.translate('authorBrowser.match.toast.matchSuccessSummary'), @@ -113,7 +112,7 @@ export class AuthorMatchComponent implements OnInit { this.authorMatched.emit(updatedAuthor); }, error: () => { - this.matching = false; + this.matching.set(false); this.messageService.add({ severity: 'error', summary: this.t.translate('authorBrowser.match.toast.matchFailedSummary'),