11import React from 'react' ;
22import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' ;
33import { faSearch , faBug , faLock , faUserSecret , faNetworkWired , faBrain , faTerminal } from '@fortawesome/free-solid-svg-icons' ;
4- import { useState , useEffect , useRef } from 'react' ;
4+ import { useState , useEffect } from 'react' ;
55import request from '@/utils/request' ;
6+ import { useRouter } from 'next/router' ;
67
78const SearchModal = ( { showSearchModal, setShowSearchModal } ) => {
89 const [ search , setSearch ] = useState ( '' ) ;
9- const [ results , setResults ] = useState ( [ ] ) ;
10- const debouncedSearchTerm = useDebounce ( search , 300 ) ; // 300ms delay
10+ const [ results , setResults ] = useState ( null ) ;
11+ const router = useRouter ( ) ;
12+ const debouncedSearchTerm = useDebounce ( search , 100 ) ; // 300ms delay
1113
1214 useEffect ( ( ) => {
1315 if ( debouncedSearchTerm ) {
1416 const endpoint = process . env . NEXT_PUBLIC_API_URL ;
1517 const url = `${ endpoint } /search/${ debouncedSearchTerm } ` ;
1618 console . log ( url ) ;
1719 request ( url , "GET" , null ) . then ( ( res ) => {
18- setResults ( res ) ;
19- console . log ( res )
20+ setResults ( res . results ) ;
21+ console . log ( res ) ;
2022 } ) . catch ( ( err ) => { console . log ( err ) ; } ) ;
2123 }
2224 } , [ debouncedSearchTerm ] ) ;
2325
26+ const routeToChallenge = ( id ) => router . push ( "/challenges/" + id ) ;
27+ const routeToUser = ( username ) => router . push ( "/users/" + username ) ;
28+
2429 return (
2530 < >
2631 { showSearchModal && (
@@ -37,8 +42,31 @@ const SearchModal = ({ showSearchModal, setShowSearchModal }) => {
3742 < div className = "px-4 py-5 sm:px-6 mx-auto" >
3843 < div className = 'flex items-center mx-auto' >
3944 < FontAwesomeIcon icon = { faSearch } className = "w-5 h-5 mr-1 text-white" />
40- < input onChange = { e => setSearch ( e . target . value ) } placeholder = "Search for challenges, users, or competitions" className = 'w-full border-0 text-xl focus:ring-0 bg-transparent text-white' autoFocus />
45+ < input onChange = { e => {
46+ setSearch ( e . target . value )
47+ if ( e . target . value === '' ) setResults ( null ) ;
48+ } } placeholder = "Search for challenges, users, or competitions" className = 'w-full border-0 text-xl focus:ring-0 bg-transparent text-white' autoFocus />
4149 </ div >
50+
51+ {
52+ results && search && (
53+ < div className = 'mt-5' >
54+ < div className = 'grid grid-cols-1 sm:grid-cols-2 gap-4' >
55+ { results . challenges . map ( ( result , index ) => (
56+ < div style = { { cursor : "pointer" } } key = { index } className = 'p-3 bg-neutral-700 rounded-lg' onClick = { ( ) => routeToChallenge ( result . id ) } >
57+ < h1 className = 'text-md text-white font-semibold' > { result . title } </ h1 >
58+ </ div >
59+ ) ) }
60+ { results . users . map ( ( result , index ) => (
61+ < div style = { { cursor : "pointer" } } key = { index } className = 'p-3 bg-neutral-700 rounded-lg' onClick = { ( ) => routeToUser ( result . username ) } >
62+ < h1 className = 'text-md text-white font-semibold' > { result . username } </ h1 >
63+ </ div >
64+ ) ) }
65+ </ div >
66+ </ div >
67+ )
68+ }
69+
4270 < h1 className = 'mt-10 text-xl text-white font-semibold mb-4' > Search by Category</ h1 >
4371 < div className = "flex flex-wrap gap-2" >
4472 < button className = "px-4 py-2 rounded bg-blue-500 bg-opacity-50 border border-blue-800 hover:brightness-110 text-white flex items-center gap-2" >
0 commit comments