Skip to content

Commit 8db3f89

Browse files
committed
Merge in pr #46
1 parent def1da9 commit 8db3f89

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

components/CollapsibleSearch.tsx

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { ReceivedArticle } from "../ts_types/db_types";
77
const CollapsibleSearch = () => {
88
const [searchValue, setSearchValue] = useState("");
99
const [searchBar, setSearchBar] = useState(false);
10-
const [suggestions, setSuggestions] = useState<string[]>([]);
10+
const [suggestions, setSuggestions] = useState<ReceivedArticle[]>([]);
1111
const textInput = useRef<HTMLInputElement>(null);
1212

1313
const onSearchBlur = () => {
@@ -23,28 +23,20 @@ const CollapsibleSearch = () => {
2323
const value = e.target.value;
2424
setSearchValue(value);
2525

26-
2726
const newSuggestions = await getSuggestions(value);
28-
// TODO: FOR ABIDUR, MAKE SUGGESTIONS A LIST OF RECIEVEDARTICLE
29-
// setSuggestions(newSuggestions);
30-
31-
// but for demo purposes
32-
setSuggestions(newSuggestions.map(v => v.title));
27+
setSuggestions(newSuggestions);
3328
};
3429

35-
const onSuggestionClick = (suggestion: string) => {
36-
setSearchValue(suggestion);
30+
const onSuggestionClick = (suggestion: ReceivedArticle) => {
31+
setSearchValue(suggestion.title);
3732
setSuggestions([]);
38-
setSearchBar(true);
33+
setSearchBar(true);
3934
textInput.current?.focus();
4035

41-
42-
Router.push({
43-
pathname: '/search',
44-
query: { query: encodeURIComponent(suggestion) },
45-
});
36+
Router.push(`/article/<slug>`);
4637
};
4738

39+
4840
const submitSearchRequest = (e: React.FormEvent) => {
4941
e.preventDefault();
5042
Router.push(`/search?query=${searchValue}`);
@@ -59,7 +51,7 @@ const CollapsibleSearch = () => {
5951
className={styles.suggestionItem}
6052
onClick={() => onSuggestionClick(suggestion)}
6153
>
62-
{suggestion}
54+
{suggestion.title}
6355
</div>
6456
))}
6557
</div>
@@ -80,20 +72,19 @@ const CollapsibleSearch = () => {
8072
}, []);
8173

8274
const getSuggestions = async (input: string) => {
83-
if (!input){
75+
if (!input) {
8476
return [];
8577
}
8678

8779
const request = await fetch(`/api/articles/search?q=${input}&max=5`);
8880
const rjson = await request.json()
89-
if (!rjson.articles){
81+
if (!rjson.articles) {
9082
throw new Error("Articles were not returned from search");
9183
}
9284
const articles = rjson.articles as ReceivedArticle[];
9385

9486
return articles;
9587
};
96-
9788

9889
return (
9990
<div id={styles.collapsible_search_parent}>
@@ -131,5 +122,4 @@ const CollapsibleSearch = () => {
131122
);
132123
};
133124

134-
135125
export default CollapsibleSearch;

0 commit comments

Comments
 (0)