From 7c33237ea2ee98703d6a896932218ed3e8074aee Mon Sep 17 00:00:00 2001 From: Juliana Date: Tue, 1 Feb 2022 22:37:24 -0300 Subject: [PATCH] =?UTF-8?q?Constru=C3=A7=C3=A3o=20da=20busca=20na=20p?= =?UTF-8?q?=C3=A1gina=20de=20sugest=C3=A3o=20de=20livros?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../servicosInterface/sugestoes.service.ts | 11 ++- src/app/sugestoes/sugestoes.component.html | 68 +++++++++++++------ src/app/sugestoes/sugestoes.component.ts | 32 +++++++-- 3 files changed, 86 insertions(+), 25 deletions(-) diff --git a/src/app/servicosInterface/sugestoes.service.ts b/src/app/servicosInterface/sugestoes.service.ts index bf0b682a..f3999ee4 100644 --- a/src/app/servicosInterface/sugestoes.service.ts +++ b/src/app/servicosInterface/sugestoes.service.ts @@ -1,7 +1,7 @@ import { Sugestoes } from './../modelosInterface/sugestoes'; import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; -import { delay, first, tap } from 'rxjs'; +import { delay, first, map, tap } from 'rxjs'; @Injectable({ providedIn: 'root' @@ -20,4 +20,13 @@ export class SugestoesService { tap(apiSugestoes => console.log(apiSugestoes)) ) } + + pesquisar(titulo: string){ + return this.listaSug.get(this.uriAPI) + .pipe( + first(), + delay(200), + map(res => res.filter(i => (i.titulo.toLowerCase()).startsWith(titulo.toLowerCase()))), + ) + } } diff --git a/src/app/sugestoes/sugestoes.component.html b/src/app/sugestoes/sugestoes.component.html index 8cb281d8..750a5d83 100644 --- a/src/app/sugestoes/sugestoes.component.html +++ b/src/app/sugestoes/sugestoes.component.html @@ -1,24 +1,52 @@ -
- -
-

SUGESTÕES DE LIVROS

+
+
+
+ + Busque uma sugestão de livro + + search +
- - - - - - - {{card.titulo}} - {{card.subtitulo}} - - - -
{{card.conteudo}}
-
-
-
-
+
+ + + + + {{busca.titulo}} + {{busca.subtitulo}} + + + +
{{busca.conteudo}}
+
+
+
+
+
+
+ +
+ +
+

SUGESTÕES DE LIVROS

+
+
+ + + + + + {{card.titulo}} + {{card.subtitulo}} + + + +
{{card.conteudo}}
+
+
+
+
+
diff --git a/src/app/sugestoes/sugestoes.component.ts b/src/app/sugestoes/sugestoes.component.ts index b8dbca6e..905a269e 100644 --- a/src/app/sugestoes/sugestoes.component.ts +++ b/src/app/sugestoes/sugestoes.component.ts @@ -1,9 +1,10 @@ +import { Sugestoes } from './../modelosInterface/sugestoes'; import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout'; -import { Component } from '@angular/core'; -import { catchError, Observable, of } from 'rxjs'; -import { map } from 'rxjs/operators'; +import { Component, ElementRef, ViewChild } from '@angular/core'; +import { catchError, fromEvent, Observable, of } from 'rxjs'; +import { debounceTime, distinctUntilChanged, filter, map, tap } from 'rxjs/operators'; + -import { Sugestoes } from '../modelosInterface/sugestoes'; import { SugestoesService } from '../servicosInterface/sugestoes.service'; @Component({ @@ -14,6 +15,9 @@ import { SugestoesService } from '../servicosInterface/sugestoes.service'; export class SugestoesComponent { cards$: Observable ; + result$?: Observable + value!: string; + cards = this.breakpointObserver.observe(Breakpoints.Handset).pipe( map(({ matches }) => { if (matches) { @@ -34,4 +38,24 @@ export class SugestoesComponent { }) ); } + + @ViewChild('searchInput') searchInput!: ElementRef + + ngAfterViewInit(): void { + fromEvent(this.searchInput.nativeElement, 'keyup').pipe( + filter(Boolean), + debounceTime(400), + distinctUntilChanged(), + tap(() => { + const query = this.searchInput.nativeElement.value + // console.log(query) + if(query) { + this.result$ = this.sugestoesService.pesquisar(query) + console.log(this.result$) + } else { + this.result$ = undefined + } + }) + ).subscribe() + } }