@@ -3,10 +3,14 @@ import {
33 Component ,
44 computed ,
55 inject ,
6+ OnDestroy ,
67 OnInit ,
78 signal ,
89} from '@angular/core' ;
910import { Router } from '@angular/router' ;
11+ import { Subject , Subscription } from 'rxjs' ;
12+ import { debounceTime , distinctUntilChanged } from 'rxjs/operators' ;
13+
1014import { PageHeaderComponent } from '../../../design-system/page-header/page-header' ;
1115import { ToolbarComponent } from '../../../design-system/toolbar/toolbar' ;
1216import { SearchInputComponent } from '../../../design-system/input/search-input' ;
@@ -43,18 +47,22 @@ import { initialValuesPagination } from '../../../design-system/pagination/utils
4347 templateUrl : './categories.html' ,
4448 styleUrl : './categories.css' ,
4549} )
46- export class CategoriesPageComponent implements OnInit {
50+ export class CategoriesPageComponent implements OnInit , OnDestroy {
4751 private router = inject ( Router ) ;
4852 private toastService = inject ( ToastService ) ;
4953 private readonly categoryService = inject ( CategoryService ) ;
5054
51- private readonly perPage = signal < number > ( 25 ) ;
55+ private readonly perPage = signal < number > ( 10 ) ;
5256 private readonly currentPage = signal < number > ( 1 ) ;
5357
5458 readonly loading = signal < boolean > ( false ) ;
5559 readonly searchQuery = signal < string > ( '' ) ;
5660 readonly selectedStatus = signal < string > ( '' ) ;
5761
62+ // Subject para gerenciar o debounce da digitação no campo de busca
63+ private searchSubject = new Subject < string > ( ) ;
64+ private searchSubscription ?: Subscription ;
65+
5866 // Category Form Drawer State
5967 readonly isFormOpen = signal < boolean > ( false ) ;
6068 readonly formMode = signal < 'create' | 'edit' | 'view' > ( 'create' ) ;
@@ -82,15 +90,33 @@ export class CategoriesPageComponent implements OnInit {
8290
8391 readonly totalItens = signal < number > ( 0 ) ;
8492 query : CategoryDefaultQuery = {
85- name : null ,
93+ search : null ,
8694 active : null ,
8795 parent_id : null ,
8896 page : this . currentPage ( ) ,
8997 per_page : this . perPage ( ) ,
9098 } ;
9199
92100 ngOnInit ( ) : void {
101+ // 1. Busca inicial
93102 this . getPaginationAllCategories ( this . query ) ;
103+
104+ // 2. Inscrição para escutar a busca com debounce de 400ms
105+ this . searchSubscription = this . searchSubject
106+ . pipe ( debounceTime ( 400 ) , distinctUntilChanged ( ) )
107+ . subscribe ( ( queryText ) => {
108+ this . currentPage . set ( 1 ) ;
109+ this . query = {
110+ ...this . query ,
111+ search : queryText || null ,
112+ page : 1 ,
113+ } ;
114+ this . getPaginationAllCategories ( this . query ) ;
115+ } ) ;
116+ }
117+
118+ ngOnDestroy ( ) : void {
119+ this . searchSubscription ?. unsubscribe ( ) ;
94120 }
95121
96122 getPaginationAllCategories ( filters : CategoryDefaultQuery ) : void {
@@ -111,30 +137,24 @@ export class CategoriesPageComponent implements OnInit {
111137 } ) ;
112138 }
113139
114- readonly filteredCategories = computed ( ( ) => {
115- const q = this . searchQuery ( ) . toLowerCase ( ) . trim ( ) ;
116- const st = this . selectedStatus ( ) ;
117-
118- return this . allCategories ( ) . filter ( ( c ) => {
119- const matchesQ =
120- ! q ||
121- c . name . toLowerCase ( ) . includes ( q ) ||
122- c . slug . toLowerCase ( ) . includes ( q ) ||
123- ( c . description || '' ) . toLowerCase ( ) . includes ( q ) ;
124- const matchesSt = ! st || ( st === 'active' ? c . active : ! c . active ) ;
125- return matchesQ && matchesSt ;
126- } ) ;
127- } ) ;
128-
129140 onSearchChange ( query : string ) : void {
130- this . query = { ...this . query , name : query } ;
131141 this . searchQuery . set ( query ) ;
132- this . getPaginationAllCategories ( this . query ) ;
142+ this . searchSubject . next ( query ) ;
133143 }
134144
135145 onStatusChange ( status : string ) : void {
136- this . query = { ...this . query , active : status === 'active' } ;
146+ let activeValue : boolean | null = null ;
147+ if ( status === 'active' ) activeValue = true ;
148+ if ( status === 'inactive' ) activeValue = false ;
149+
137150 this . selectedStatus . set ( status ) ;
151+ this . currentPage . set ( 1 ) ;
152+
153+ this . query = {
154+ ...this . query ,
155+ active : activeValue ,
156+ page : 1 ,
157+ } ;
138158 this . getPaginationAllCategories ( this . query ) ;
139159 }
140160
@@ -165,13 +185,16 @@ export class CategoriesPageComponent implements OnInit {
165185 resetFilters ( ) : void {
166186 this . searchQuery . set ( '' ) ;
167187 this . selectedStatus . set ( '' ) ;
188+ this . currentPage . set ( 1 ) ;
189+
168190 this . query = {
169- name : null ,
191+ search : null ,
170192 active : null ,
171193 parent_id : null ,
172- page : this . currentPage ( ) ,
194+ page : 1 ,
173195 per_page : this . perPage ( ) ,
174196 } ;
197+ this . getPaginationAllCategories ( this . query ) ;
175198 this . toastService . info ( 'Filtros limpos' , 'Todos os parâmetros de busca foram resetados.' ) ;
176199 }
177200
0 commit comments