11"use client" ;
22
33import { IProfessor } from "@/utils/interfaces" ;
4+ import { getContent } from "@/utils/contentful" ;
5+ import { PROFESSOR_QUERY } from "@/utils/queries" ;
46import React , { useState } from "react" ;
57import Professor from "./Professor" ;
68import { FilterBar } from "./FilterBar" ;
@@ -12,26 +14,33 @@ export const ProfessorGrid: React.FC<{
1214 initProfessors : IProfessor [ ] ;
1315} > = ( { tags = [ ] , initProfessors = [ ] } ) => {
1416 const [ selectedTags , setSelectedTags ] = useState < string [ ] > ( [ ] ) ;
17+ const [ professors , setProfessors ] = useState < IProfessor [ ] > ( initProfessors ) ;
1518 const [ currentPage , setCurrentPage ] = useState ( 0 ) ;
19+ const [ isLoading , setIsLoading ] = useState ( false ) ;
1620
17- const handleUpdate = ( tagName : string ) => {
18- setSelectedTags ( ( prev ) =>
19- prev . includes ( tagName ) ? prev . filter ( ( t ) => t !== tagName ) : [ ...prev , tagName ] ,
20- ) ;
21+ const handleUpdate = async ( tagName : string ) => {
22+ const next = selectedTags . includes ( tagName )
23+ ? selectedTags . filter ( ( t ) => t !== tagName )
24+ : [ ...selectedTags , tagName ] ;
25+ setSelectedTags ( next ) ;
2126 setCurrentPage ( 0 ) ;
22- } ;
2327
24- const filtered =
25- selectedTags . length === 0
26- ? initProfessors
27- : initProfessors . filter ( ( prof ) =>
28- selectedTags . some ( ( tag ) =>
29- prof . workingFieldsCollection . items . some ( ( f ) => f . name === tag ) ,
30- ) ,
31- ) ;
28+ if ( next . length === 0 ) {
29+ setProfessors ( initProfessors ) ;
30+ } else {
31+ setIsLoading ( true ) ;
32+ const data = await getContent < { docentesCollection : { items : IProfessor [ ] } } > (
33+ PROFESSOR_QUERY ,
34+ { workingField : next } ,
35+ ) ;
36+ setProfessors ( data . docentesCollection . items ) ;
37+ await new Promise ( ( resolve ) => setTimeout ( resolve , 300 ) ) ;
38+ setIsLoading ( false ) ;
39+ }
40+ } ;
3241
33- const totalPages = Math . ceil ( filtered . length / PAGE_SIZE ) ;
34- const professors = filtered . slice ( currentPage * PAGE_SIZE , ( currentPage + 1 ) * PAGE_SIZE ) ;
42+ const totalPages = Math . ceil ( professors . length / PAGE_SIZE ) ;
43+ const paginated = professors . slice ( currentPage * PAGE_SIZE , ( currentPage + 1 ) * PAGE_SIZE ) ;
3544
3645 return (
3746 < div className = "container" >
@@ -41,13 +50,15 @@ export const ProfessorGrid: React.FC<{
4150 onTagSelect = { handleUpdate }
4251 />
4352 < div className = "my-6 h-px w-full bg-gray-200" aria-hidden = "true" />
44- { professors . length === 0 ? (
53+ { isLoading ? (
54+ < p className = "py-20 text-center text-gray-500" > Carregando...</ p >
55+ ) : professors . length === 0 ? (
4556 < p className = "py-20 text-center text-gray-500" >
4657 Nenhum professor foi encontrado para os filtros selecionados.
4758 </ p >
4859 ) : (
4960 < div className = "grid grid-cols-1 gap-8 sm:grid-cols-2 lg:grid-cols-4" >
50- { professors . map ( ( prof ) => (
61+ { paginated . map ( ( prof ) => (
5162 < Professor key = { prof . name } professor = { prof } />
5263 ) ) }
5364 </ div >
0 commit comments