11import './Navbar.css'
22import { useState , useEffect } from "react"
3- import { Link , useNavigate } from "react-router-dom"
3+ import { Link , useNavigate , useParams } from "react-router-dom"
44import { LogOut } from 'lucide-react' ;
55import { LogIn } from 'lucide-react' ;
66import Logo from "../assets/img/logo.png" ;
77import useGlobalReducer from "../hooks/useGlobalReducer" ;
88
9- export default function Navbar ( { showDrowpdown, setShowDrowpdown} ) {
9+ export default function Navbar ( { showDrowpdown, setShowDrowpdown } ) {
1010 const { store, dispatch } = useGlobalReducer ( )
11+ const { all_games } = store ;
12+ console . log ( all_games )
1113 const navigate = useNavigate ( )
1214 const token_user = localStorage . getItem ( 'jwt-token' ) ;
1315 const user = localStorage . getItem ( 'user' ) ;
14- const [ view , setview ] = useState ( false )
16+ const [ view , setview ] = useState ( false ) ;
17+ const [ searchGame , setSearchGame ] = useState ( ) /* Barra de buscar funcional*/
18+
19+
20+ const handleSearch = ( ) => {
21+ if ( searchGame . trim ( ) ) {
22+ navigate ( `/search?query=${ encodeURIComponent ( searchGame ) } ` ) ;
23+ }
24+
25+ } ;
1526
1627 const cerrarSesion = ( ) => {
1728 localStorage . removeItem ( "jwt-token" )
@@ -38,6 +49,19 @@ export default function Navbar({showDrowpdown, setShowDrowpdown}) {
3849 } , [ token_user ] ) ;
3950
4051
52+ const search = ( ) => {
53+
54+ const game = all_games . find ( game => game . name === searchGame )
55+ console . log ( game )
56+ console . log ( game . id )
57+ navigate ( `/DetailsGames/${ game . id } ` )
58+
59+ if ( ! game . name == searchGame )
60+ alert ( "Juego no encontrado" )
61+
62+ }
63+
64+
4165 return (
4266 < nav className = "bg-gray-900" >
4367 < div className = " navbar-bg mx-auto max-w-7xl px-2 sm:px-6 lg:px-8" >
@@ -72,42 +96,50 @@ export default function Navbar({showDrowpdown, setShowDrowpdown}) {
7296 Juegos
7397 </ button >
7498 { showDrowpdown && (
75- < ul className = "absolute z-10 mt-2 w-40 rounded-md bg-white shadow-lg ring-1 ring-black/5" >
76- < li > < Link to = "/games/platform/PS5" className = "block px-4 py-2 text-sm text-indigo-900 hover:bg-indigo-200" > PS5</ Link > </ li >
77- < li > < Link to = "/games/platform/PS4" className = "block px-4 py-2 text-sm text-indigo-900 hover:bg-indigo-200" > PS4</ Link > </ li >
78- < li > < Link to = "/games/platform/Xbox One" className = "block px-4 py-2 text-sm text-indigo-900 hover:bg-indigo-200" > Xbox One</ Link > </ li >
79- < li > < Link to = "/games/platform/Nintendo Switch" className = "block px-4 py-2 text-sm text-indigo-900 hover:bg-indigo-200" > Nintendo Switch</ Link > </ li >
80- < li > < Link to = "/games/platform/Nintendo Switch 2" className = "block px-4 py-2 text-sm text-indigo-900 hover:bg-indigo-200" > Nintendo Switch 2</ Link > </ li >
81- < li > < Link to = "/games/platform/PC" className = "block px-4 py-2 text-sm text-indigo-900 hover:bg-indigo-200" > PC</ Link > </ li >
82- </ ul >
99+ < ul className = "absolute z-10 mt-2 w-40 rounded-md bg-white shadow-lg ring-1 ring-black/5" >
100+ < li > < Link to = "/games/platform/PS5" className = "block px-4 py-2 text-sm text-indigo-900 hover:bg-indigo-200" > PS5</ Link > </ li >
101+ < li > < Link to = "/games/platform/PS4" className = "block px-4 py-2 text-sm text-indigo-900 hover:bg-indigo-200" > PS4</ Link > </ li >
102+ < li > < Link to = "/games/platform/Xbox One" className = "block px-4 py-2 text-sm text-indigo-900 hover:bg-indigo-200" > Xbox One</ Link > </ li >
103+ < li > < Link to = "/games/platform/Nintendo Switch" className = "block px-4 py-2 text-sm text-indigo-900 hover:bg-indigo-200" > Nintendo Switch</ Link > </ li >
104+ < li > < Link to = "/games/platform/Nintendo Switch 2" className = "block px-4 py-2 text-sm text-indigo-900 hover:bg-indigo-200" > Nintendo Switch 2</ Link > </ li >
105+ < li > < Link to = "/games/platform/PC" className = "block px-4 py-2 text-sm text-indigo-900 hover:bg-indigo-200" > PC</ Link > </ li >
106+ </ ul >
83107 ) }
84108 </ div >
85109
86- {
110+ {
87111 view && (
88112 < Link to = "/addgame" >
89113 < p href = "#" className = "rounded-md px-3 py-2 text-lg font-bold text-gray-300 hover:text-white" > Añadir Juego</ p >
90114 </ Link >
91115 ) }
92116
117+ { /* Barra de buscar funcional*/ }
118+
93119 < input
94120 type = "text"
121+ value = { searchGame }
122+ onChange = { ( e ) => setSearchGame ( e . target . value ) }
95123 placeholder = "Buscar..."
96- className = "px-1 py-1 border rounded-md focus:outline-none focus:ring focus:border-indigo -900"
124+ className = "px-1 py-1 border rounded-md focus:outline-none focus:ring focus:border-gray -900"
97125 />
126+ < button onClick = { search } >
127+ Buscar
128+ </ button >
129+
98130 </ div >
99131 </ div >
100132 </ div >
101133 < div className = "absolute inset-y-0 right-0 flex items-center pr-2 sm:static sm:inset-auto sm:ml-6 sm:pr-0" >
102-
134+
103135 { /* ---- carrito ---- */ }
104136 < Link to = "/Carro" className = "relative mr-4" >
105- {
137+ {
138+
139+ token_user && (
140+ < span className = "text-white text-2xl" > 🛒</ span >
141+ ) }
106142
107- token_user && (
108- < span className = "text-white text-2xl" > 🛒</ span >
109- ) }
110-
111143 { store . carro && store . carro . length > 0 && (
112144 < span className = "absolute -top-2 -right-2 bg-red-500 text-white rounded-full text-xs px-1" >
113145 { store . carro . length }
@@ -116,12 +148,12 @@ export default function Navbar({showDrowpdown, setShowDrowpdown}) {
116148 </ Link >
117149 {
118150
119- token_user && (
120- < Link to = "/historial" className = "rounded-md px-3 py-2 text-lg font-bold text-gray-300 hover:text-white" >
121- Historial
122- </ Link >
123- ) }
124-
151+ token_user && (
152+ < Link to = "/historial" className = "rounded-md px-3 py-2 text-lg font-bold text-gray-300 hover:text-white" >
153+ Historial
154+ </ Link >
155+ ) }
156+
125157
126158
127159 < button type = "button" className = "relative rounded-full p-1 text-gray-400 hover:text-white focus:ring-2 focus:ring-white focus:ring-offset-2 focus:ring-offset-gray-800 focus:outline-hidden" >
0 commit comments