Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes: #2062: Search bar fires a request on every entered letter #2066

Merged
merged 1 commit into from
Apr 7, 2021
Merged
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
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