diff --git a/src/components/SearchBox/SearchBox.component.jsx b/src/components/SearchBox/SearchBox.component.jsx index 90fe7d528..a5cf11961 100644 --- a/src/components/SearchBox/SearchBox.component.jsx +++ b/src/components/SearchBox/SearchBox.component.jsx @@ -1,7 +1,6 @@ import React, { useState } from 'react'; import styled from 'styled-components'; import { createHashHistory } from 'history'; -import { types } from '../../utils/constants'; import { useSearch } from '../../providers/Search/Search.provider'; const TextBox = styled.input` @@ -35,15 +34,15 @@ const TextBox = styled.input` `; function SearchBox() { - const [state, dispatch] = useSearch(); - const [value, setValue] = useState(""); + const { searchTerm, setSearchTerm } = useSearch(); + const [value, setValue] = useState(''); const history = createHashHistory(); const ENTER_KEY = 13; function searchIt(e) { - let search = e.target.value.trim() - if (state.query !== search) { - dispatch({ type: types.QUERY, term: search }); + let search = e.target.value.trim(); + if (searchTerm !== search) { + setSearchTerm(search); } const currentPath = window.location.hash; diff --git a/src/pages/Home/Home.page.jsx b/src/pages/Home/Home.page.jsx index 66671191e..ffbf86b0f 100644 --- a/src/pages/Home/Home.page.jsx +++ b/src/pages/Home/Home.page.jsx @@ -25,16 +25,16 @@ const Welcome = styled.h1` `; function HomePage() { - const [state] = useSearch(); - const query = state.query; + const { searchTerm } = useSearch(); + const query = searchTerm; let params = { q: query, type: 'video', part: ['id', 'snippet'], order: 'relevance', - maxResults: 12 - } + maxResults: 12, + }; const [videos, isLoading, error] = useYTApi({ endpoint: 'search', params: params }); @@ -44,7 +44,7 @@ function HomePage() { Welcome to the Challenge! - ) + ); } if (error) { @@ -53,7 +53,7 @@ function HomePage() { Welcome to the Challenge!
Couldn't find any videos for your search :(
- ) + ); } return ( diff --git a/src/providers/Search/Search.provider.jsx b/src/providers/Search/Search.provider.jsx index 8db607496..b914d8720 100644 --- a/src/providers/Search/Search.provider.jsx +++ b/src/providers/Search/Search.provider.jsx @@ -1,6 +1,7 @@ -import React, { createContext, useContext, useEffect, useReducer } from "react"; -import { storage } from "../../utils/fns"; -import searchReducer, { initialSearch } from "./searchReducer"; +import React, { createContext, useContext, useEffect, useReducer } from 'react'; +import { storage } from '../../utils/fns'; +import searchReducer, { initialSearch } from './searchReducer'; +import { types } from '../../utils/constants'; const SearchContext = createContext(null); @@ -19,8 +20,12 @@ function SearchProvider({ children }) { storage.set('search', state); }, [state]); + const setSearchTerm = (searchTerm) => { + dispatch({ type: types.QUERY, term: searchTerm }); + }; + return ( - + {children} );