Skip to content

Commit

Permalink
Fixed Search results request on every entered letter
Browse files Browse the repository at this point in the history
  • Loading branch information
HyperTHD committed Apr 4, 2021
1 parent f0bc060 commit c3b5625
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/web/src/components/SearchProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type FilterProp = {

export interface SearchContextInterface {
text: string;
textParam: string;
filter: FilterProp['filter'];
showHelp: boolean;
onTextChange: (value: string) => void;
Expand All @@ -16,6 +17,7 @@ export interface SearchContextInterface {

const SearchContext = createContext<SearchContextInterface>({
text: '',
textParam: '',
filter: 'post',
showHelp: true,
onTextChange() {
Expand Down Expand Up @@ -72,7 +74,7 @@ const SearchProvider = ({ children }: Props) => {

return (
<SearchContext.Provider
value={{ text, showHelp, filter, onTextChange, onFilterChange, onSubmitHandler }}
value={{ text, textParam, showHelp, filter, onTextChange, onFilterChange, onSubmitHandler }}
>
{children}
</SearchContext.Provider>
Expand Down
8 changes: 4 additions & 4 deletions src/web/src/components/SearchResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ const useStyles = makeStyles(() => ({

const SearchResults = () => {
const classes = useStyles();
const { text, filter } = useSearchValue();
const { textParam, filter } = useSearchValue();

const prepareUrl = (index: number) =>
`${telescopeUrl}/query?text=${encodeURIComponent(text)}&filter=${filter}&page=${index}`;
`${telescopeUrl}/query?text=${encodeURIComponent(textParam)}&filter=${filter}&page=${index}`;

// We only bother doing the request if we have something to search for.
const shouldFetch = () => text.length > 0;
const shouldFetch = () => textParam.length > 0;
const { data, size, setSize, error } = useSWRInfinite(
(index) => (shouldFetch() ? prepareUrl(index) : null),
async (u) => {
Expand Down Expand Up @@ -92,7 +92,7 @@ const SearchResults = () => {
);
}

if (text.length && loading) {
if (textParam.length && loading) {
return (
<Container className={classes.searchResults}>
<h1 className={classes.spinner}>
Expand Down

0 comments on commit c3b5625

Please sign in to comment.