Skip to content

fixes #252 The search bar issue #256

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

Closed
wants to merge 3 commits into from
Closed
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
73 changes: 52 additions & 21 deletions components/searchBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,47 +32,48 @@ export default function SearchBar({
const router = useRouter();
const smallScreen = useMediaQuery("(max-width: 800px)");
const inputRef = useRef<HTMLInputElement>();

let debounceTimeout: NodeJS.Timeout;
let lastInputValue = ""; // Store the last input value

// Input validation
const [isEmpty, setIsEmpty] = useState(!query);
const [isError, setIsError] = useState(false);

function handleInput(event: FormEvent) {
setQuery((event.target as HTMLInputElement).value);
const inputValue = (event.target as HTMLInputElement).value;

setQuery(inputValue);

// When input value is null, set error & empty state to `true` and do nothing.
if (!(event.target as HTMLInputElement).value) {
if (!inputValue) {
setIsEmpty(true);
setIsError(true);
return;
}

// When input value is not null, reset error & empty state to `false`.
// And also debounce the router push.
setIsError(false);
setIsEmpty(false);

if (!smallScreen)
debounce(() => {
router.push(`/search?q=${(event.target as HTMLInputElement).value}`);
});
if (!smallScreen) {
// Store the debounced function in a variable to be able to cancel it later
const debouncedFunc = () => {
router.push(`/search?q=${inputValue}`);
};
debounceTimeout = setTimeout(debouncedFunc, 200);
}
}

function handleSubmit(event?: FormEvent) {
if (event) event.preventDefault();

// When input value is null, set error & empty state to `true` and do nothing.
if (!query || !inputRef.current.value) {
setIsError(true);
setIsEmpty(true);
return;
function handleKeyDown(event: React.KeyboardEvent<HTMLInputElement>) {
// Check if the Backspace key is pressed and the input value becomes empty
if (event.key === "Backspace" && lastInputValue === "") {
// Navigate back to the initial page
router.push("/");
}

// When input value is not null, reset error & empty state to `false` and push the route.
setIsError(false);
setIsEmpty(false);

// For performance reasons the input on small screens is not controlled
router.push(`/search?q=${smallScreen ? inputRef.current.value : query}`);
// Update the last input value
lastInputValue = (event.target as HTMLInputElement).value;
}

const searchAdornment = (
Expand All @@ -94,6 +95,24 @@ export default function SearchBar({
}
}, [router.route]);

function handleSubmit(event?: FormEvent) {
if (event) event.preventDefault();

// When input value is null, set error & empty state to `true` and do nothing.
if (!query || !inputRef.current.value) {
setIsError(true);
setIsEmpty(true);
return;
}

// When input value is not null, reset error & empty state to `false` and push the route.
setIsError(false);
setIsEmpty(false);

// For performance reasons the input on small screens is not controlled
router.push(`/search?q=${smallScreen ? inputRef.current.value : query}`);
}

return (
<form
noValidate
Expand All @@ -118,6 +137,9 @@ export default function SearchBar({
endAdornment={searchAdornment}
inputRef={inputRef}
error={isError}
onKeyDown={(event: React.KeyboardEvent<HTMLInputElement>) =>
handleKeyDown(event)
}
/>
) : (
<OutlinedInput
Expand All @@ -129,6 +151,9 @@ export default function SearchBar({
endAdornment={searchAdornment}
inputRef={inputRef}
error={isError}
onKeyDown={(event: React.KeyboardEvent<HTMLInputElement>) =>
handleKeyDown(event)
}
/>
)}
</FormControl>
Expand All @@ -143,6 +168,9 @@ export default function SearchBar({
endAdornment={searchAdornment}
inputRef={inputRef}
error={isError}
onKeyDown={(event: React.KeyboardEvent<HTMLInputElement>) =>
handleKeyDown(event)
}
/>
) : (
<FilledInput
Expand All @@ -153,6 +181,9 @@ export default function SearchBar({
endAdornment={searchAdornment}
inputRef={inputRef}
error={isError}
onKeyDown={(event: React.KeyboardEvent<HTMLInputElement>) =>
handleKeyDown(event)
}
/>
)}
</>
Expand Down
11 changes: 11 additions & 0 deletions public/locales/en/categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"complementaryfilter": "Complementary Filter",
"compression": "Compression",
"computervision": "Computer Vision",
"concurrency": "Concurrency",
"conversions": "Conversions",
"cosinesimilarity": "Cosine Similarity",
"cpuschedulingalgorithms": "CPU Scheduling Algorithms",
Expand Down Expand Up @@ -97,12 +98,15 @@
"heap": "Heap",
"heaps": "Heaps",
"histogramequalization": "Histogram Equalization",
"http": "Http",
"imageprocessing": "Image Processing",
"io": "Io",
"ipynbcheckpoints": ".Ipynb Checkpoints",
"knapsack": "Knapsack",
"leetcode": "Leetcode",
"levenshtein": "Levenshtein",
"linearalgebra": "Linear Algebra",
"linearprogramming": "Linear Programming",
"linearregression": "Linear Regression",
"linkedlist": "Linked List",
"list": "List",
Expand All @@ -112,6 +116,7 @@
"lstm": "Lstm",
"machinelearning": "Machine Learning",
"manacher": "Manacher",
"map": "Map",
"math": "Math",
"matrix": "Matrix",
"matrixexponentiation": "Matrix Exponentiation",
Expand Down Expand Up @@ -149,6 +154,7 @@
"password": "Password",
"patternsearch": "Pattern Search",
"permutation": "Permutation",
"permutations": "Permutations",
"physics": "Physics",
"pi": "PI",
"piecewise": "Piecewise",
Expand Down Expand Up @@ -178,6 +184,7 @@
"segmenttrees": "Segment Trees",
"sequences": "Sequences",
"series": "Series",
"set": "Set",
"shufflers": "Shufflers",
"sieveoferatosthenes": "Sieve of Eratosthenes",
"slidingwindow": "Sliding Window",
Expand All @@ -186,6 +193,7 @@
"sources": "Sources",
"spanningtree": "Spanning Tree",
"specializedstructure": "Specialized Structure",
"sqrt": "Sqrt",
"stablemarriage": "Stable Marriage",
"stacks": "Stacks",
"stackwithsinglylinkedlist": "Stack with Singly Linked List",
Expand All @@ -197,13 +205,16 @@
"test": "Test",
"tests": "Tests",
"textclassification": "Text Classification",
"threads": "Threads",
"timingfunctions": "Timing-Functions",
"tls": "Tls",
"tree": "Tree",
"trie": "Trie",
"tries": "Tries",
"uint53": "Uint 53",
"utils": "Utils",
"vectors": "Vectors",
"view": "View",
"web": "Web",
"webprogramming": "Web Programming"
}
Loading