Skip to content

Commit

Permalink
hrz-89846: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Sinucid committed Sep 20, 2023
1 parent 0fee42e commit 1b9d569
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
14 changes: 5 additions & 9 deletions libs/domain/search/box/box.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,6 @@ describe('SearchBoxComponent', () => {
});

describe('when typing a query string', () => {
const callback = vi.fn();

beforeEach(async () => {
element = await fixture(html`<oryx-search-box></oryx-search-box>`);

Expand Down Expand Up @@ -253,9 +251,7 @@ describe('SearchBoxComponent', () => {
const typeahead = element.renderRoot.querySelector('oryx-typeahead');

typeahead?.toggleAttribute('open', true);
typeahead?.dispatchEvent(
new CustomEvent('oryx.search', { detail: {} })
);
typeahead?.dispatchEvent(new CustomEvent('oryx.search'));
});

it('should get the link from service without params', () => {
Expand All @@ -272,12 +268,12 @@ describe('SearchBoxComponent', () => {
describe('and query is provided', () => {
const q = 'test';
beforeEach(async () => {
element = await fixture(html`<oryx-search-box></oryx-search-box>`);
element = await fixture(
html`<oryx-search-box .query=${q}></oryx-search-box>`
);
element.renderRoot
.querySelector('oryx-typeahead')
?.dispatchEvent(
new CustomEvent('oryx.search', { detail: { query: q } })
);
?.dispatchEvent(new CustomEvent('oryx.search'));
});

it('should get the link from service with params', () => {
Expand Down
40 changes: 23 additions & 17 deletions libs/domain/search/box/box.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import {
Size,
computed,
debounce,
effect,
elementEffect,
hydrate,

Check warning on line 18 in libs/domain/search/box/box.component.ts

View workflow job for this annotation

GitHub Actions / test-lint

'hydrate' is defined but never used
signalAware,

Check warning on line 19 in libs/domain/search/box/box.component.ts

View workflow job for this annotation

GitHub Actions / test-lint

'signalAware' is defined but never used
signalProperty,

Check warning on line 20 in libs/domain/search/box/box.component.ts

View workflow job for this annotation

GitHub Actions / test-lint

'signalProperty' is defined but never used
Expand Down Expand Up @@ -70,8 +68,13 @@ export class SearchBoxComponent
)
);

@elementEffect()
protected queryEffect = effect(() => this.query$.next(this.query));
protected $link = computed(() =>
this.semanticLinkService.get({
type: RouteType.ProductList,
...(this.query ? { params: { q: this.query } } : {}),
})
);

protected $raw = computed(() => this.suggestion$);
protected $suggestion = computed(() => {
const query = this.query?.trim();
Expand All @@ -86,7 +89,7 @@ export class SearchBoxComponent
return html`
<oryx-typeahead
@oryx.search=${this.onSearch}
@oryx.typeahead=${debounce(this.onTypeahead.bind(this), 300)}
@oryx.typeahead=${this.onTypeahead}
.clearIcon=${IconTypes.Close}
?float=${this.$options().float}
>
Expand Down Expand Up @@ -139,20 +142,23 @@ export class SearchBoxComponent

protected onTypeahead(event: CustomEvent<SearchEventDetail>): void {
this.query = event.detail.query;

this.assignSuggestionQuery();
}

protected onSearch(e: CustomEvent<SearchEventDetail>): void {
const q = e.detail.query;

this.semanticLinkService
.get({
type: RouteType.ProductList,
...(q ? { params: { q } } : {}),
})
.subscribe((link) => {
this.routerService.navigate(link!);
this.onClose();
});
protected assignSuggestionQuery = debounce(() => {
this.query$.next(this.query);
}, 300);

protected onSearch(): void {
const link = this.$link();

if (!link) {
return;
}

this.routerService.navigate(link);
this.onClose();
}

protected isNothingFound(suggestion: Suggestion | null | undefined): boolean {
Expand Down

0 comments on commit 1b9d569

Please sign in to comment.