created search component

This commit is contained in:
andres alcocer
2020-07-10 13:18:26 -04:00
parent aaa168147f
commit ff18a5e7df
4 changed files with 128 additions and 51 deletions

View File

@@ -1,16 +1,21 @@
import React from 'react'; import React from 'react';
import { BrowserRouter, Route, Switch } from 'react-router-dom'; import { BrowserRouter, Route, Switch } from 'react-router-dom';
import Layout from './Layout'; import Home from './Home';
import NotFound from './NotFound'; import NotFound from './NotFound';
import Search from './Search';
import Navbar from './Navbar';
const AppRouter = () => ( const AppRouter = () => (
<BrowserRouter> <BrowserRouter>
<>
<Navbar />
<Switch> <Switch>
<Route path="/" exact component={Layout} /> <Route path="/" exact component={Home} />
<Router component={Search} /> <Route path="/search" component={Search} />
<Route component={NotFound} /> <Route component={NotFound} />
</Switch> </Switch>
</>
</BrowserRouter> </BrowserRouter>
); );

View File

@@ -7,13 +7,12 @@ import Movie from '../components/Movie/Movie';
import Modal from '../components/UI/Modal'; import Modal from '../components/UI/Modal';
import MovieDetails from '../components/Movie/MovieDetails'; import MovieDetails from '../components/Movie/MovieDetails';
class Layout extends Component { class Home extends Component {
state = { state = {
/** Toggles the movie list when the user starts typing. */ /** Toggles the movie list when the user starts typing. */
toggleMovieList: true, toggleMovieList: true,
/** An array that will hold all of our movie Components. */ /** An array that will hold all of our movie Components. */
MovieList: [], // MovieList: [],
/** Toggles the modal when a movie is clicked. */ /** Toggles the modal when a movie is clicked. */
toggleModal: false, toggleModal: false,
/** Holds the movie information for a single movie. */ /** Holds the movie information for a single movie. */
@@ -58,9 +57,12 @@ class Layout extends Component {
/** Get the user input */ /** Get the user input */
onSearchHandler = (event) => { onSearchHandler = (event) => {
/** Display the movie list. */ /** Display the movie list. */
this.setState({ // navigate to search comp
toggleMovieList: false // this.props.history.push('/search)
}); this.props.history.push('/search')
// this.setState({
// toggleMovieList: false
// });
const userInput = event.target.value; const userInput = event.target.value;
/** Pass in the user input to make the API call. */ /** Pass in the user input to make the API call. */
@@ -106,8 +108,11 @@ class Layout extends Component {
render() { render() {
return ( return (
<div> <div>
<Navbar showMovies={this.onSearchHandler} /> {/* <Navbar showMovies={this.onSearchHandler} /> */}
<MainContent /> {
this.state.toggleMovieList ? <MainContent /> : <div
className="search-container">{this.state.MovieList}</div>
}
<Modal show={this.state.toggleModal} <Modal show={this.state.toggleModal}
modalClosed={this.closeModal} modalClosed={this.closeModal}
movie={this.state.movieOverview}> movie={this.state.movieOverview}>
@@ -118,4 +123,4 @@ class Layout extends Component {
} }
} }
export default Layout; export default Home;

View File

@@ -1,5 +1,6 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { NavLink } from 'react-router-dom'; import { NavLink } from 'react-router-dom';
import { withRouter } from 'react-router-dom';
import NavigationItem from '../components/NavigationItem' import NavigationItem from '../components/NavigationItem'
import SearchLogo from '../static/images/search-icon.svg'; import SearchLogo from '../static/images/search-icon.svg';
@@ -9,7 +10,7 @@ import DropdownArrow from '../static/images/drop-down-arrow.svg';
import DropdownContent from "../components/DropdownContent"; import DropdownContent from "../components/DropdownContent";
class navigation extends Component { class Navbar extends Component {
state = { state = {
scrolling: false scrolling: false
} }
@@ -32,9 +33,64 @@ class navigation extends Component {
} }
} }
/** Get the user input */
onSearchHandler = (event) => {
/** Display the movie list. */
// navigate to search comp
// this.props.history.push('/search)
this.props.history.push('/search')
// this.setState({
// toggleMovieList: false
// });
const userInput = event.target.value;
console.log('user input', userInput)
/** Pass in the user input to make the API call. */
// this.makeAipCall(userInput);
/** If the input is empty don't display the movie list. */
if (userInput === "") {
this.props.history.push('/')
}
}
/** Make API call as soon as the user starts typing. */
makeAipCall = (searchItem) => {
const url = `/search/multi?api_key=${process.env.API_KEY}&language=en-US&include_adult=false&query=${searchItem}`;
axios.get(url)
.then(res => {
const results = res.data.results;
let movieImageUrl;
/** Will hold all our movies Components */
let movieRows = [];
/** Loop through all the movies */
results.forEach((movie) => {
/** Manually build our image url and set it on the Movie component. */
if (movie.poster_path !== null && movie.media_type !== "person") {
movieImageUrl = "https://image.tmdb.org/t/p/w500" + movie.poster_path;
/** Set the movie object to our Movie component */
const movieComponent = <Movie
movieDetails={() => this.selectMovieHandler(movie)}
key={movie.id}
movieImage={movieImageUrl}
movie={movie} />
/** Push our movie component to our movieRows array */
movieRows.push(movieComponent);
}
})
/** Set our MovieList array to the movieRows array */
this.setState({ MovieList: movieRows });
}).catch(error => {
console.log(error);
});
render() { render() {
const { scrolling } = this.state; const { scrolling, showMovies } = this.state;
const { showMovies } = this.props;
return ( return (
<nav className={"navigation " + (scrolling ? "black" : "")} > <nav className={"navigation " + (scrolling ? "black" : "")} >
@@ -52,7 +108,7 @@ class navigation extends Component {
<div className="navigation__container--left"> <div className="navigation__container--left">
<SearchLogo className="logo" /> <SearchLogo className="logo" />
<input <input
onChange={showMovies} onChange={() => this.onSearchHandler(event)}
className="navigation__container--left__input" className="navigation__container--left__input"
type="text" type="text"
placeholder="Title, genres, people" /> placeholder="Title, genres, people" />
@@ -68,6 +124,6 @@ class navigation extends Component {
</nav> </nav>
) )
} }
} }
export default navigation; export default withRouter(Navbar);

11
src/containers/Search.js Normal file
View File

@@ -0,0 +1,11 @@
import React, { Component } from 'react';
export default class Search extends Component {
state = {
movieList: [];
}
render() {
return <div className="search-container">{this.state.movieList}</div>;
}
}