Skip to content

Commit

Permalink
Merge pull request #261 from mayconbenito/chore/remove-debounce-from-…
Browse files Browse the repository at this point in the history
…search

chore: remove debounce from search
  • Loading branch information
mayconbenito committed Oct 16, 2020
2 parents bed2ec7 + 71b68ce commit c451087
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 46 deletions.
32 changes: 22 additions & 10 deletions src/contexts/SearchContext.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useState } from 'react';
import React, { useEffect, useState } from 'react';

import isStringEmpty from '../helpers/isStringEmpty';
import api from '../services/api';

const SearchContext = React.createContext({
Expand All @@ -24,27 +25,38 @@ export const SearchProvider = ({ children }) => {
});

async function handleSearch(searchQuery) {
setQuery(searchQuery);
const response = await api.get(`/app/search/${searchQuery}`, {
params: {
limit: 20,
type: 'artist,album,track',
},
});

setResults(response.data.results);
try {
const response = await api.get(`/app/search/${searchQuery}`, {
params: {
limit: 20,
type: 'artist,album,track',
},
});

setResults(response.data.results);
// eslint-disable-next-line no-empty
} catch {}
}

function handleClearSearch() {
setQuery('');
setResults({ artists: [], albums: [], tracks: [] });
}

useEffect(() => {
if (!isStringEmpty(query)) {
handleSearch(query);
} else {
handleClearSearch();
}
}, [query]);

return (
<SearchContext.Provider
value={{
results,
query,
setQuery,
handleSearch,
handleClearSearch,
}}
Expand Down
19 changes: 0 additions & 19 deletions src/hooks/useDebounce.js

This file was deleted.

22 changes: 5 additions & 17 deletions src/pages/Search/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useEffect, useState } from 'react';
import React, { useContext } from 'react';
import { useTranslation } from 'react-i18next';
import { MdClear } from 'react-icons/md';

Expand All @@ -8,7 +8,6 @@ import SmallArtistItem from '../../components/SmallArtistItem';
import SmallTrackItem from '../../components/SmallTrackItem';
import SearchContext from '../../contexts/SearchContext';
import isStringEmpty from '../../helpers/isStringEmpty';
import useDebounce from '../../hooks/useDebounce';
import {
GlobalHeaderContainer,
Content,
Expand All @@ -24,27 +23,16 @@ import {
function Search({ history }) {
const { t } = useTranslation();
const searchContext = useContext(SearchContext);
const [query, setQuery] = useState(searchContext.query);

const debouncedQuery = useDebounce(query, 400);

function handleInput(e) {
setQuery(e.target.value);
searchContext.setQuery(e.target.value);
}

function handleClearSearch() {
setQuery('');
searchContext.setQuery('');
searchContext.handleClearSearch();
}

useEffect(() => {
if (!isStringEmpty(debouncedQuery)) {
searchContext.handleSearch(query);
} else {
handleClearSearch();
}
}, [debouncedQuery]);

return (
<Content>
<GlobalHeaderContainer>
Expand All @@ -53,13 +41,13 @@ function Search({ history }) {

<SearchInputContainer>
<SearchInput
value={query}
value={searchContext.query}
onChange={handleInput}
autoFocus
placeholder={t('search.input')}
/>

{!isStringEmpty(query) && (
{!isStringEmpty(searchContext.query) && (
<ClearSearchButton onClick={handleClearSearch}>
<MdClear size={26} color="#d99207" />
</ClearSearchButton>
Expand Down

0 comments on commit c451087

Please sign in to comment.