Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/components/SearchBox/SearchBox.component.jsx
Original file line number Diff line number Diff line change
@@ -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`
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions src/pages/Home/Home.page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

Expand All @@ -44,7 +44,7 @@ function HomePage() {
<Welcome>Welcome to the Challenge!</Welcome>
<Spinner className="spinner" data-testid="spinner" />
</Container>
)
);
}

if (error) {
Expand All @@ -53,7 +53,7 @@ function HomePage() {
<Welcome>Welcome to the Challenge!</Welcome>
<h5>Couldn&apos;t find any videos for your search :(</h5>
</Container>
)
);
}

return (
Expand Down
13 changes: 9 additions & 4 deletions src/providers/Search/Search.provider.jsx
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -19,8 +20,12 @@ function SearchProvider({ children }) {
storage.set('search', state);
}, [state]);

const setSearchTerm = (searchTerm) => {
dispatch({ type: types.QUERY, term: searchTerm });
};

return (
<SearchContext.Provider value={[state, dispatch]}>
<SearchContext.Provider value={{ searchTerm: state.query, setSearchTerm }}>
{children}
</SearchContext.Provider>
);
Expand Down