Skip to content

Commit

Permalink
FIX: Got serch page to render
Browse files Browse the repository at this point in the history
  • Loading branch information
HarrisSte committed Aug 30, 2023
1 parent 9835d4c commit 17899ed
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 145 deletions.
50 changes: 25 additions & 25 deletions client/src/components/Header/NavLinks.jsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
import Nav from "react-bootstrap/Nav";
import { useCurrentUserContext } from "../../context/CurrentUser";
import logo from "../../assets/BUGbytes-white.png";
import Search from "../Search";
import "./header.css";
import Nav from 'react-bootstrap/Nav';
import { useCurrentUserContext } from '../../context/CurrentUser';
import logo from '../../assets/BUGbytes-white.png';
import Search from '../Search';
import './header.css';

function NavLinks() {
const { isLoggedIn, logoutUser } = useCurrentUserContext();

return (
<div className="header">
<div className="logo">
<a href="/">
<div className='header'>
<div className='logo'>
<a href='/'>
<img src={logo} width={70} height={80} />
</a>
</div>
<div className="search-buttons">
<div className='search-buttons'>
<Search />
{isLoggedIn() ? (
<>
<div className="header-buttons">
<Nav className="justify-content-center">
<Nav.Item className="profile">
<div className='header-buttons'>
<Nav className='justify-content-center'>
<Nav.Item className='profile'>
<Nav.Link
href="/profile"
href='/profile'
style={{
color: "white",
color: 'white',
}}
>
Profile
</Nav.Link>
</Nav.Item>
<Nav.Item className="log-out">
<Nav.Item className='log-out'>
<Nav.Link
onClick={logoutUser}
style={{
color: "white",
color: 'white',
}}
>
Log Out
Expand All @@ -45,23 +45,23 @@ function NavLinks() {
</>
) : (
<>
<div className="header-login-buttons">
<Nav className="login-signin justify-content-center">
<Nav.Item className="log-in">
<div className='header-login-buttons'>
<Nav className='login-signin justify-content-center'>
<Nav.Item className='log-in'>
<Nav.Link
href="/login"
href='/login'
style={{
color: "white",
color: 'white',
}}
>
Log In
</Nav.Link>
</Nav.Item>
<Nav.Item className="sign-up">
<Nav.Item className='sign-up'>
<Nav.Link
href="/register"
href='/register'
style={{
color: "white",
color: 'white',
}}
>
Sign Up
Expand All @@ -76,4 +76,4 @@ function NavLinks() {
);
}

export default NavLinks;
export default NavLinks;
83 changes: 15 additions & 68 deletions client/src/components/Results.jsx
Original file line number Diff line number Diff line change
@@ -1,78 +1,25 @@
import React from 'react';
import { Link } from 'react-router-dom';
import { useParams } from 'react-router-dom';

const Results = (props) => {

const { gameId } = useParams();

const Results = ({ game }) => {
return (
<div className="results-container">
<ul>
<div className='results-container'>
{
props.gameResults.map(game => (
<li key={game.id}>

<Link to={{
pathname: `/game/${gameId}`,
gameProps:{
game: game
}
}}>
<div>
<Link
to={{
pathname: `/game/${game.id}`,
gameProps: {
game: game,
},
}}
>
<h3>{game.name}</h3>
<img src={game.background_image} alt="game"/>
</Link>

</li>
))
<img src={game.background_image} alt='game' />
</Link>
</div>
}
</ul>
</div>
);
}
};

export default Results;

// import { useParams } from 'react-router-dom';
// import { useEffect, useState } from 'react';
// import ResultsPage from '../pages/Results/ResultsPage'
// import ResultsBackground from '../assets/results-background.png'

// const Results = () => {

// const { gameId } = useParams();

// useEffect(() => {
// fetchGame();
// }, []);

// const [game, setGame] = useState([]);

// const fetchGame = async () => {
// await fetch(
// `https://api.rawg.io/api/games/${gameId}?key=bf22dc51e531456db8bc42a19dac9897`
// )
// .then((resp) => resp.json())
// .then((results) => setGame(results));
// };

// return (
// <div
// className='results'
// style={{
// width: '100%',
// minHeight: '100vh',
// backgroundImage: `url(${ResultsBackground})`,
// backgroundSize: 'cover',
// backgroundPosition: 'center',
// overflow: 'hidden',
// }}
// >
// <div className='results-info'>
// <ResultsPage game={game} />
// </div>
// </div>
// );
// }

// export default Results;
32 changes: 19 additions & 13 deletions client/src/components/Search.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { useState } from 'react';
import Results from './Results';
import { BsSearchHeart } from "react-icons/bs";
import { BsSearchHeart } from 'react-icons/bs';
import { useNavigate } from 'react-router-dom';

const Search = () => {
const [searchTerm, setSearchTerm] = useState('');
const [gameResults, setGameResults] = useState([]);

const navigate = useNavigate();
const handleChange = (e) => {
setSearchTerm(e.target.value);
};
Expand All @@ -14,29 +13,36 @@ const Search = () => {
e.preventDefault();
let slug = searchTerm.split(' ').join('-').toLowerCase();

setGameResults([]);
fetch(
`https://rawg.io/api/games?key=bf22dc51e531456db8bc42a19dac9897&search=${slug}`
)
.then((resp) => resp.json())
.then(({ results }) => {
results === undefined
.then((apiResponse) => {
apiResponse === undefined
? alert('no games found')
: setGameResults(results);
: navigate('/results', { state: { results: apiResponse.results } });
});

setSearchTerm('');
};

return (
<div className='search'>
<form onSubmit={onSubmit}>
<BsSearchHeart size={30} color="white" />
<input className='search-bar' type='text' placeholder='Search for games!' value={searchTerm} onChange={handleChange} />
<button className='submit-button' type='submit'>Submit</button>
<BsSearchHeart size={30} color='white' />
<input
className='search-bar'
type='text'
placeholder='Search for games!'
value={searchTerm}
onChange={handleChange}
/>
<button className='submit-button' type='submit'>
Submit
</button>
</form>
<Results gameResults={gameResults} />
</div>
);
};

export default Search;
export default Search;
4 changes: 4 additions & 0 deletions client/src/components/SingleGame/SingleGame.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@ h5 {
margin-top: 2rem;
margin-bottom: 2rem;
border-radius: 1rem;
border: solid 6px #D4655A;
}

.card-header {
background-color: #C8A954ff;
border-bottom: solid black;
font-size: 2rem;

}

.card-title {
background-color: #C8A954ff;
padding: .5rem;

}

.game-description {
Expand All @@ -33,6 +36,7 @@ h5 {
margin-top: 2rem;
border-radius: 1rem;
margin-bottom: 2rem;
border: solid 6px #D4655A;

}

Expand Down
4 changes: 2 additions & 2 deletions client/src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Profile from "./pages/Profile";
import Login from "./pages/Login/Login";
import Register from "./pages/Register/Register";
import SingleGame from './pages/SingleGame';
import Search from './components/Search';
import Results from './pages/Results/ResultsPage';

import ProtectedRoute from './components/ProtectedRoute';

Expand All @@ -30,7 +30,7 @@ const router = createBrowserRouter(

<Route path="login" element={<Login />} />
<Route path="register" element={<Register />} />
<Route path='/search' element={<Search />} />
<Route path='/results' element={<Results />} />
<Route path="/game/:gameId" element={<SingleGame />} />

<Route
Expand Down
62 changes: 25 additions & 37 deletions client/src/pages/Results/ResultsPage.jsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,30 @@
import Results from '../../components/Results';
import { useParams } from 'react-router-dom';
import { useEffect, useState } from 'react';
import { useLocation } from 'react-router-dom';
import ResultsBackground from '../../assets/results-background.png';

const GameResults = () => {
const { gameId } = useParams();

useEffect(() => {
fetchGame();
}, []);

const [game, setGame] = useState([]);

const fetchGame = async () => {
await fetch(
`https://api.rawg.io/api/games/${gameId}?key=bf22dc51e531456db8bc42a19dac9897`
)
.then((resp) => resp.json())
.then((results) => setGame(results));
};

return (
<div
className='results'
style={{
width: '100%',
minHeight: '100vh',
backgroundImage: `url(${ResultsBackground})`,
backgroundSize: 'cover',
backgroundPosition: 'center',
overflow: 'hidden',
}}
>
<div className='game-info'>
<Results game={game} />
</div>
const location = useLocation();
const gameResults = location.state.results;

return (
<div
className='results'
style={{
width: '100%',
minHeight: '100vh',
backgroundImage: `url(${ResultsBackground})`,
backgroundSize: 'cover',
backgroundPosition: 'center',
overflow: 'hidden',
}}
>
<div className='game-info'>
{gameResults.map((game, index) => (
<Results key={index} game={game} />
))}
</div>
);
};

export default GameResults;
</div>
);
};

export default GameResults;

0 comments on commit 17899ed

Please sign in to comment.