refactored Navbar component

This commit is contained in:
andres alcocer
2021-10-08 11:41:30 -04:00
parent 29a274048b
commit d77db5757b
4 changed files with 23 additions and 21 deletions

1
package-lock.json generated
View File

@@ -10,7 +10,6 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"axios": "^0.22.0", "axios": "^0.22.0",
"lodash": "^4.17.21",
"prop-types": "^15.7.2", "prop-types": "^15.7.2",
"react": "^17.0.2", "react": "^17.0.2",
"react-dom": "^17.0.2", "react-dom": "^17.0.2",

View File

@@ -17,7 +17,6 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"axios": "^0.22.0", "axios": "^0.22.0",
"lodash": "^4.17.21",
"prop-types": "^15.7.2", "prop-types": "^15.7.2",
"react": "^17.0.2", "react": "^17.0.2",
"react-dom": "^17.0.2", "react-dom": "^17.0.2",

View File

@@ -1,13 +1,8 @@
import React, { useState, useEffect } from 'react' import React, { useState, useEffect } from 'react'
import { NavLink } from 'react-router-dom' import { NavLink } from 'react-router-dom'
import _ from 'lodash'
import { withRouter } from 'react-router-dom' import { withRouter } from 'react-router-dom'
import { useDispatch, useSelector } from 'react-redux'
import { useScroll } from '../hooks/useScroll' import { useScroll } from '../hooks/useScroll'
import { useDebounce } from '../hooks/useDebounce'
import * as movieActions from '../store/actions'
// import axios from '../axios-movies'
import SearchLogo from '../static/images/search-icon.svg' import SearchLogo from '../static/images/search-icon.svg'
import NetflixLogo from '../static/images/Netflix_Logo_RGB.png' import NetflixLogo from '../static/images/Netflix_Logo_RGB.png'
import BellLogo from '../static/images/bell-logo.svg' import BellLogo from '../static/images/bell-logo.svg'
@@ -16,27 +11,17 @@ import DropdownContent from '../components/DropdownContent'
const Navbar = ({ history }) => { const Navbar = ({ history }) => {
const [userInput, setUserInput] = useState('') const [userInput, setUserInput] = useState('')
const searchResults = useSelector((state) => state.searchMovie)
const [scrollDimensions] = useScroll() const [scrollDimensions] = useScroll()
const { scrollY } = scrollDimensions const { scrollY } = scrollDimensions
const dispatch = useDispatch()
const debouncedUserInput = useDebounce(userInput, 500)
const onChange = async (event) => { const onChange = async (event) => {
setUserInput(event.target.value) setUserInput(event.target.value)
} }
// useEffect(() => {
// if (debouncedUserInput) {
// history.push(`/search?=${userInput}`)
// }
// }, [debouncedUserInput])
useEffect(() => { useEffect(() => {
userInput.length <= 0 userInput.length <= 0
? history.push('/') ? history.push('/')
: history.push(`/search?=${userInput}`) : history.push(`/search?q=${userInput}`)
}, [userInput]) }, [userInput])
const onLogoClick = () => { const onLogoClick = () => {

View File

@@ -1,12 +1,26 @@
import React, { useState, useEffect } from 'react' import React, { useState, useEffect } from 'react'
import { Redirect } from 'react-router-dom' import { useLocation } from 'react-router-dom'
import { useDispatch, useSelector } from 'react-redux'
import { useDebounce } from '../hooks/useDebounce'
import Modal from '../components/UI/Modal' import Modal from '../components/UI/Modal'
import MovieDetails from '../components/Movie/MovieDetails' import MovieDetails from '../components/Movie/MovieDetails'
import Movie from '../components/Movie/Movie' import Movie from '../components/Movie/Movie'
import axios from '../axios-movies' import axios from '../axios-movies'
const Search = ({ location: { searchResults, userInput }, history }) => { // A custom hook that builds on useLocation to parse
// the query string for you.
const useQuery = () => {
return new URLSearchParams(useLocation().search)
}
const Search = ({ history }) => {
let query = useQuery()
const debouncedSearchTerm = useDebounce(query.get('q'), 500)
const [isToggleModal, setIsToggleModal] = useState(false)
const searchResults = useSelector((state) => state.searchMovie)
const dispatch = useDispatch()
// constructor(props) { // constructor(props) {
// super(props) // super(props)
// this.state = { // this.state = {
@@ -16,8 +30,13 @@ const Search = ({ location: { searchResults, userInput }, history }) => {
// movieOverview: {}, // movieOverview: {},
// } // }
// } // }
useEffect(() => {
if (debouncedSearchTerm) {
console.log('maing api call....')
}
}, [debouncedSearchTerm])
const [isToggleModal, setIsToggleModal] = useState(false) console.log('location', query.get('q'))
// componentDidMount = async () => { // componentDidMount = async () => {