Skip to content

Commit bceaa60

Browse files
authored
Merge pull request #29 from 4GeeksAcademy/Silvia
Silvia
2 parents 58b8006 + fd35d7c commit bceaa60

File tree

4 files changed

+109
-26
lines changed

4 files changed

+109
-26
lines changed

src/api/routes/games.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,20 @@ def get_games_by_platform(platform):
129129

130130
games = [g.serialize() for g in q.all()] #devuelve todos los juegos encontrados
131131
return jsonify({"games": games}), 200
132+
133+
# BUSCAR JUEGOS NAVBAR
134+
#@api.route('/SearchGames/<int:game_id>', methods=["GET"])
135+
#def get_games_by_search():
136+
#query = request.args.get("query")
137+
138+
#if not query:
139+
#return jsonify({"error": "Falta el parámetro 'query'"}), 400
140+
141+
#try:
142+
#results = Games.query.filter(Games.name.ilike(f"%{query}%")).all()
143+
#games_list = [{"id": game.id, "name": game.name} for game in results]
144+
#return jsonify(games_list)
145+
#except Exception as e:
146+
#print("Error en la búsqueda:", e)
147+
#return jsonify({"error": "Juego no encontrado"}), 500
148+

src/front/components/Navbar.jsx

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
import './Navbar.css'
22
import { useState, useEffect } from "react"
3-
import { Link, useNavigate } from "react-router-dom"
3+
import { Link, useNavigate, useParams } from "react-router-dom"
44
import { LogOut } from 'lucide-react';
55
import { LogIn } from 'lucide-react';
66
import Logo from "../assets/img/logo.png";
77
import 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">
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { useLocation } from "react-router-dom";
2+
import { useEffect, useState } from "react";
3+
4+
const backendUrl = "https://refactored-couscous-x5p76ppwgq5v3xxr-3001.app.github.dev/"; // Cada uno tiene que poner su url de su backend hasta tener BD común
5+
6+
export default function SearchResults() {
7+
const location = useLocation();
8+
const query = new URLSearchParams(location.search).get("query");
9+
const [results, setResults] = useState([]);
10+
11+
useEffect(() => {
12+
if (query) {
13+
fetch(`${backendUrl}api/games/search?query=${encodeURIComponent(query)}`)
14+
.then(res => res.json())
15+
.then(data => setResults(data))
16+
.catch(err => console.error("Error al buscar:", err));
17+
}
18+
}, [query]);
19+
20+
return (
21+
<div>
22+
<h2>Resultados para: {query}</h2>
23+
{results.length > 0 ? (
24+
<ul>
25+
{results.map(game => (
26+
<li key={game.id}>{game.name}</li>
27+
))}
28+
</ul>
29+
) : (
30+
<p> No se encontraron resultados </p>
31+
)}
32+
</div>
33+
);
34+
}

src/front/routes.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export const router = createBrowserRouter(
6060
<Route path="/historial" element={<Historial />} />
6161
<Route path="/recPassword" element={<RecPassword />} />
6262
<Route path="/resetPassword/:token" element={<ResetPassword />} />
63-
<Route path="/editgames/:id" element={<EditGames />} /> {/* PAGINA DETALLES GAMES*/}
63+
<Route path="/editgames/:id" element={<EditGames />} />
6464
<Route path="/games/platform/:platform" element={<GamesByPlatform />} />
6565

6666

0 commit comments

Comments
 (0)