fix(author-browser): convert AuthorMatchComponent state to signals (#1581)
Signed-off-by: Kunta Mallik Raj <kuntamallikraj@gmail.com>
This commit is contained in:
+13
-12
@@ -4,18 +4,19 @@
|
|||||||
<div class="search-fields">
|
<div class="search-fields">
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<label for="author-match-name">{{ t('authorNameLabel') }}</label>
|
<label for="author-match-name">{{ t('authorNameLabel') }}</label>
|
||||||
<input id="author-match-name" pInputText type="text" [(ngModel)]="searchQuery" [placeholder]="t('authorNamePlaceholder')" class="search-input"/>
|
<input id="author-match-name" pInputText type="text" [ngModel]="searchQuery()" (ngModelChange)="searchQuery.set($event)" [placeholder]="t('authorNamePlaceholder')" class="search-input"/>
|
||||||
</div>
|
</div>
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<label for="author-match-asin">{{ t('asinLabel') }}</label>
|
<label for="author-match-asin">{{ t('asinLabel') }}</label>
|
||||||
<input id="author-match-asin" pInputText type="text" [(ngModel)]="asinQuery" [placeholder]="t('asinPlaceholder')" class="search-input"/>
|
<input id="author-match-asin" pInputText type="text" [ngModel]="asinQuery()" (ngModelChange)="asinQuery.set($event)" [placeholder]="t('asinPlaceholder')" class="search-input"/>
|
||||||
</div>
|
</div>
|
||||||
<div class="field region-field">
|
<div class="field region-field">
|
||||||
<label for="author-match-region">{{ t('regionLabel') }}</label>
|
<label for="author-match-region">{{ t('regionLabel') }}</label>
|
||||||
<p-select
|
<p-select
|
||||||
inputId="author-match-region"
|
inputId="author-match-region"
|
||||||
[options]="regionOptions"
|
[options]="regionOptions"
|
||||||
[(ngModel)]="selectedRegion"
|
[ngModel]="selectedRegion()"
|
||||||
|
(ngModelChange)="selectedRegion.set($event)"
|
||||||
optionLabel="label"
|
optionLabel="label"
|
||||||
optionValue="value"
|
optionValue="value"
|
||||||
class="region-dropdown">
|
class="region-dropdown">
|
||||||
@@ -26,14 +27,14 @@
|
|||||||
[label]="t('searchBtn')"
|
[label]="t('searchBtn')"
|
||||||
icon="pi pi-search"
|
icon="pi pi-search"
|
||||||
(onClick)="search()"
|
(onClick)="search()"
|
||||||
[loading]="searching"
|
[loading]="searching()"
|
||||||
[disabled]="!canSearch">
|
[disabled]="!canSearch()">
|
||||||
</p-button>
|
</p-button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@if (searching) {
|
@if (searching()) {
|
||||||
<div class="search-loading">
|
<div class="search-loading">
|
||||||
<p-progressSpinner
|
<p-progressSpinner
|
||||||
[style]="{'width': '40px', 'height': '40px'}"
|
[style]="{'width': '40px', 'height': '40px'}"
|
||||||
@@ -45,7 +46,7 @@
|
|||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
|
||||||
@if (!searching && hasSearched && results.length === 0) {
|
@if (!searching() && hasSearched() && results().length === 0) {
|
||||||
<div class="empty-results">
|
<div class="empty-results">
|
||||||
<i class="pi pi-info-circle"></i>
|
<i class="pi pi-info-circle"></i>
|
||||||
<p>{{ t('noResults') }}</p>
|
<p>{{ t('noResults') }}</p>
|
||||||
@@ -53,7 +54,7 @@
|
|||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
|
||||||
@if (!searching && !hasSearched) {
|
@if (!searching() && !hasSearched()) {
|
||||||
<div class="ready-state">
|
<div class="ready-state">
|
||||||
<i class="pi pi-search"></i>
|
<i class="pi pi-search"></i>
|
||||||
<p>{{ t('readyToSearch') }}</p>
|
<p>{{ t('readyToSearch') }}</p>
|
||||||
@@ -61,12 +62,12 @@
|
|||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
|
||||||
@if (!searching && results.length > 0) {
|
@if (!searching() && results().length > 0) {
|
||||||
<div class="results-header">
|
<div class="results-header">
|
||||||
<span>{{ t('resultsFound', {count: results.length}) }}</span>
|
<span>{{ t('resultsFound', {count: results().length}) }}</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="results-list">
|
<div class="results-list">
|
||||||
@for (result of results; track result.asin) {
|
@for (result of results(); track result.asin) {
|
||||||
<div class="result-card">
|
<div class="result-card">
|
||||||
<div class="result-image">
|
<div class="result-image">
|
||||||
@if (result.imageUrl) {
|
@if (result.imageUrl) {
|
||||||
@@ -89,7 +90,7 @@
|
|||||||
[label]="t('matchBtn')"
|
[label]="t('matchBtn')"
|
||||||
icon="pi pi-link"
|
icon="pi pi-link"
|
||||||
severity="success"
|
severity="success"
|
||||||
[loading]="matching"
|
[loading]="matching()"
|
||||||
(onClick)="matchAuthor(result)">
|
(onClick)="matchAuthor(result)">
|
||||||
</p-button>
|
</p-button>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
+28
-28
@@ -62,29 +62,29 @@ describe('AuthorMatchComponent', () => {
|
|||||||
|
|
||||||
component.ngOnInit();
|
component.ngOnInit();
|
||||||
|
|
||||||
expect(component.searchQuery).toBe(' Ada Lovelace ');
|
expect(component.searchQuery()).toBe(' Ada Lovelace ');
|
||||||
expect(component.canSearch).toBe(true);
|
expect(component.canSearch()).toBe(true);
|
||||||
|
|
||||||
component.searchQuery = ' ';
|
component.searchQuery.set(' ');
|
||||||
component.asinQuery = ' ';
|
component.asinQuery.set(' ');
|
||||||
expect(component.canSearch).toBe(false);
|
expect(component.canSearch()).toBe(false);
|
||||||
|
|
||||||
component.asinQuery = ' B00TEST ';
|
component.asinQuery.set(' B00TEST ');
|
||||||
expect(component.canSearch).toBe(true);
|
expect(component.canSearch()).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('does not search when both query inputs are blank after trimming', () => {
|
it('does not search when both query inputs are blank after trimming', () => {
|
||||||
const component = createComponent();
|
const component = createComponent();
|
||||||
component.searchQuery = ' ';
|
component.searchQuery.set(' ');
|
||||||
component.asinQuery = ' ';
|
component.asinQuery.set(' ');
|
||||||
component.results = [{source: 'seed', asin: 'old', name: 'Existing'}];
|
component.results.set([{source: 'seed', asin: 'old', name: 'Existing'}]);
|
||||||
|
|
||||||
component.search();
|
component.search();
|
||||||
|
|
||||||
expect(searchAuthorMetadata).not.toHaveBeenCalled();
|
expect(searchAuthorMetadata).not.toHaveBeenCalled();
|
||||||
expect(component.searching).toBe(false);
|
expect(component.searching()).toBe(false);
|
||||||
expect(component.hasSearched).toBe(false);
|
expect(component.hasSearched()).toBe(false);
|
||||||
expect(component.results).toEqual([{source: 'seed', asin: 'old', name: 'Existing'}]);
|
expect(component.results()).toEqual([{source: 'seed', asin: 'old', name: 'Existing'}]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('searches with trimmed values, clears stale results, and stores returned matches', () => {
|
it('searches with trimmed values, clears stale results, and stores returned matches', () => {
|
||||||
@@ -92,17 +92,17 @@ describe('AuthorMatchComponent', () => {
|
|||||||
searchAuthorMetadata.mockReturnValue(results$);
|
searchAuthorMetadata.mockReturnValue(results$);
|
||||||
|
|
||||||
const component = createComponent();
|
const component = createComponent();
|
||||||
component.searchQuery = ' Ada ';
|
component.searchQuery.set(' Ada ');
|
||||||
component.asinQuery = ' B00MATCH ';
|
component.asinQuery.set(' B00MATCH ');
|
||||||
component.selectedRegion = 'uk';
|
component.selectedRegion.set('uk');
|
||||||
component.results = [{source: 'seed', asin: 'old', name: 'Existing'}];
|
component.results.set([{source: 'seed', asin: 'old', name: 'Existing'}]);
|
||||||
|
|
||||||
component.search();
|
component.search();
|
||||||
|
|
||||||
expect(searchAuthorMetadata).toHaveBeenCalledWith(9, 'Ada', 'uk', 'B00MATCH');
|
expect(searchAuthorMetadata).toHaveBeenCalledWith(9, 'Ada', 'uk', 'B00MATCH');
|
||||||
expect(component.searching).toBe(true);
|
expect(component.searching()).toBe(true);
|
||||||
expect(component.hasSearched).toBe(true);
|
expect(component.hasSearched()).toBe(true);
|
||||||
expect(component.results).toEqual([]);
|
expect(component.results()).toEqual([]);
|
||||||
|
|
||||||
results$.next([
|
results$.next([
|
||||||
{source: 'amazon', asin: 'B00MATCH', name: 'Ada Lovelace'},
|
{source: 'amazon', asin: 'B00MATCH', name: 'Ada Lovelace'},
|
||||||
@@ -110,22 +110,22 @@ describe('AuthorMatchComponent', () => {
|
|||||||
]);
|
]);
|
||||||
results$.complete();
|
results$.complete();
|
||||||
|
|
||||||
expect(component.results).toEqual([
|
expect(component.results()).toEqual([
|
||||||
{source: 'amazon', asin: 'B00MATCH', name: 'Ada Lovelace'},
|
{source: 'amazon', asin: 'B00MATCH', name: 'Ada Lovelace'},
|
||||||
{source: 'amazon', asin: 'B00OTHER', name: 'A. 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', () => {
|
it('reports a translated search failure toast when metadata search errors', () => {
|
||||||
searchAuthorMetadata.mockReturnValue(throwError(() => new Error('boom')));
|
searchAuthorMetadata.mockReturnValue(throwError(() => new Error('boom')));
|
||||||
|
|
||||||
const component = createComponent();
|
const component = createComponent();
|
||||||
component.searchQuery = 'Ada';
|
component.searchQuery.set('Ada');
|
||||||
|
|
||||||
component.search();
|
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.searchFailedSummary');
|
||||||
expect(translate).toHaveBeenCalledWith('authorBrowser.match.toast.searchFailedDetail');
|
expect(translate).toHaveBeenCalledWith('authorBrowser.match.toast.searchFailedDetail');
|
||||||
expect(messageService.add).toHaveBeenCalledWith({
|
expect(messageService.add).toHaveBeenCalledWith({
|
||||||
@@ -150,7 +150,7 @@ describe('AuthorMatchComponent', () => {
|
|||||||
matchAuthor.mockReturnValue(match$);
|
matchAuthor.mockReturnValue(match$);
|
||||||
|
|
||||||
const component = createComponent();
|
const component = createComponent();
|
||||||
component.selectedRegion = 'de';
|
component.selectedRegion.set('de');
|
||||||
const emitSpy = vi.spyOn(component.authorMatched, 'emit');
|
const emitSpy = vi.spyOn(component.authorMatched, 'emit');
|
||||||
|
|
||||||
component.matchAuthor({
|
component.matchAuthor({
|
||||||
@@ -165,12 +165,12 @@ describe('AuthorMatchComponent', () => {
|
|||||||
asin: 'B00MATCH',
|
asin: 'B00MATCH',
|
||||||
region: 'de',
|
region: 'de',
|
||||||
});
|
});
|
||||||
expect(component.matching).toBe(true);
|
expect(component.matching()).toBe(true);
|
||||||
|
|
||||||
match$.next(updatedAuthor);
|
match$.next(updatedAuthor);
|
||||||
match$.complete();
|
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.matchSuccessSummary');
|
||||||
expect(translate).toHaveBeenCalledWith('authorBrowser.match.toast.matchSuccessDetail');
|
expect(translate).toHaveBeenCalledWith('authorBrowser.match.toast.matchSuccessDetail');
|
||||||
expect(messageService.add).toHaveBeenCalledWith({
|
expect(messageService.add).toHaveBeenCalledWith({
|
||||||
@@ -193,7 +193,7 @@ describe('AuthorMatchComponent', () => {
|
|||||||
name: 'Ada Lovelace',
|
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.matchFailedSummary');
|
||||||
expect(translate).toHaveBeenCalledWith('authorBrowser.match.toast.matchFailedDetail');
|
expect(translate).toHaveBeenCalledWith('authorBrowser.match.toast.matchFailedDetail');
|
||||||
expect(messageService.add).toHaveBeenCalledWith({
|
expect(messageService.add).toHaveBeenCalledWith({
|
||||||
|
|||||||
+27
-28
@@ -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 {FormsModule} from '@angular/forms';
|
||||||
import {Button} from 'primeng/button';
|
import {Button} from 'primeng/button';
|
||||||
import {InputText} from 'primeng/inputtext';
|
import {InputText} from 'primeng/inputtext';
|
||||||
@@ -17,6 +17,7 @@ interface RegionOption {
|
|||||||
@Component({
|
@Component({
|
||||||
selector: 'app-author-match',
|
selector: 'app-author-match',
|
||||||
standalone: true,
|
standalone: true,
|
||||||
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||||
templateUrl: './author-match.component.html',
|
templateUrl: './author-match.component.html',
|
||||||
styleUrls: ['./author-match.component.scss'],
|
styleUrls: ['./author-match.component.scss'],
|
||||||
imports: [
|
imports: [
|
||||||
@@ -38,13 +39,15 @@ export class AuthorMatchComponent implements OnInit {
|
|||||||
private messageService = inject(MessageService);
|
private messageService = inject(MessageService);
|
||||||
private t = inject(TranslocoService);
|
private t = inject(TranslocoService);
|
||||||
|
|
||||||
searchQuery = '';
|
searchQuery = signal('');
|
||||||
asinQuery = '';
|
asinQuery = signal('');
|
||||||
selectedRegion = 'us';
|
selectedRegion = signal('us');
|
||||||
searching = false;
|
searching = signal(false);
|
||||||
matching = false;
|
matching = signal(false);
|
||||||
results: AuthorSearchResult[] = [];
|
results = signal<AuthorSearchResult[]>([]);
|
||||||
hasSearched = false;
|
hasSearched = signal(false);
|
||||||
|
|
||||||
|
canSearch = computed(() => !!this.searchQuery().trim() || !!this.asinQuery().trim());
|
||||||
|
|
||||||
regionOptions: RegionOption[] = [
|
regionOptions: RegionOption[] = [
|
||||||
{label: 'US', value: 'us'},
|
{label: 'US', value: 'us'},
|
||||||
@@ -60,29 +63,25 @@ export class AuthorMatchComponent implements OnInit {
|
|||||||
];
|
];
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
this.searchQuery = this.authorName;
|
this.searchQuery.set(this.authorName);
|
||||||
}
|
|
||||||
|
|
||||||
get canSearch(): boolean {
|
|
||||||
return !!this.searchQuery.trim() || !!this.asinQuery.trim();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
search(): void {
|
search(): void {
|
||||||
const asin = this.asinQuery.trim();
|
const asin = this.asinQuery().trim();
|
||||||
const query = this.searchQuery.trim();
|
const query = this.searchQuery().trim();
|
||||||
if (!query && !asin) return;
|
if (!query && !asin) return;
|
||||||
this.searching = true;
|
this.searching.set(true);
|
||||||
this.results = [];
|
this.results.set([]);
|
||||||
this.hasSearched = true;
|
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({
|
.subscribe({
|
||||||
next: (results) => {
|
next: (results: AuthorSearchResult[]) => {
|
||||||
this.results = results;
|
this.results.set(results);
|
||||||
this.searching = false;
|
this.searching.set(false);
|
||||||
},
|
},
|
||||||
error: () => {
|
error: () => {
|
||||||
this.searching = false;
|
this.searching.set(false);
|
||||||
this.messageService.add({
|
this.messageService.add({
|
||||||
severity: 'error',
|
severity: 'error',
|
||||||
summary: this.t.translate('authorBrowser.match.toast.searchFailedSummary'),
|
summary: this.t.translate('authorBrowser.match.toast.searchFailedSummary'),
|
||||||
@@ -94,16 +93,16 @@ export class AuthorMatchComponent implements OnInit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
matchAuthor(result: AuthorSearchResult): void {
|
matchAuthor(result: AuthorSearchResult): void {
|
||||||
this.matching = true;
|
this.matching.set(true);
|
||||||
const request: AuthorMatchRequest = {
|
const request: AuthorMatchRequest = {
|
||||||
source: result.source,
|
source: result.source,
|
||||||
asin: result.asin,
|
asin: result.asin,
|
||||||
region: this.selectedRegion
|
region: this.selectedRegion()
|
||||||
};
|
};
|
||||||
|
|
||||||
this.authorService.matchAuthor(this.authorId, request).subscribe({
|
this.authorService.matchAuthor(this.authorId, request).subscribe({
|
||||||
next: (updatedAuthor) => {
|
next: (updatedAuthor: AuthorDetails) => {
|
||||||
this.matching = false;
|
this.matching.set(false);
|
||||||
this.messageService.add({
|
this.messageService.add({
|
||||||
severity: 'success',
|
severity: 'success',
|
||||||
summary: this.t.translate('authorBrowser.match.toast.matchSuccessSummary'),
|
summary: this.t.translate('authorBrowser.match.toast.matchSuccessSummary'),
|
||||||
@@ -113,7 +112,7 @@ export class AuthorMatchComponent implements OnInit {
|
|||||||
this.authorMatched.emit(updatedAuthor);
|
this.authorMatched.emit(updatedAuthor);
|
||||||
},
|
},
|
||||||
error: () => {
|
error: () => {
|
||||||
this.matching = false;
|
this.matching.set(false);
|
||||||
this.messageService.add({
|
this.messageService.add({
|
||||||
severity: 'error',
|
severity: 'error',
|
||||||
summary: this.t.translate('authorBrowser.match.toast.matchFailedSummary'),
|
summary: this.t.translate('authorBrowser.match.toast.matchFailedSummary'),
|
||||||
|
|||||||
Reference in New Issue
Block a user