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

[CT-3469] Refactor: add autocomplete to navbar, fix redirect #124

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
39 changes: 27 additions & 12 deletions components/Autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,31 @@ import { useEffect, useState } from "react";
import { API_URL } from "../lib/utils";

type Props = {
searchInput: string
searchInput: string,
onSearch: () => void,
};

export default function AutoComplete({ searchInput }: Props) {

export default function AutoComplete({ searchInput, onSearch }: Props) {
const [packageSuggestions, setPackageSuggestions] = useState([]);
const [topicSuggestions, setTopicSuggestions] = useState([]);

function onClick(query) {
router.push(`/search?q=${encodeURIComponent(query)}`);
function onClick(query, type) {
switch (type) {
case "topic":
router.push(`/packages/${encodeURIComponent(query?.fields?.package_name)}/versions/${encodeURIComponent(query?.fields?.version)}/topics/${encodeURIComponent(query?.fields?.name)}`);
onSearch();
break
case "package":
router.push(`/search?q=${encodeURIComponent(query)}`);
onSearch();
break
case "search":
router.push(`/search?q=${encodeURIComponent(query)}`);
onSearch();
break
default:
break
Comment on lines +21 to +30
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can just do:

case "package":
case "search":
          router.push(`/search?q=${encodeURIComponent(query)}`);
          onSearch();
          break;
default:
          break;

}
};

async function autoComplete(query) {
Expand Down Expand Up @@ -61,7 +76,7 @@ export default function AutoComplete({ searchInput }: Props) {
searchInput
&&
<div
onClick={()=>onClick(searchInput)}
onClick={()=>onClick(searchInput, "search")}
className="flex items-center px-4 py-4 cursor-pointer hover:bg-dc-beige200 hover:opacity-0.5"
>
<Paragraph className="pl-2 py-2">{`View results for "${searchInput}"`}</Paragraph>
Expand All @@ -82,10 +97,10 @@ export default function AutoComplete({ searchInput }: Props) {
return (
<li
key={p?.fields?.package_name}
onClick={()=>onClick(p?.fields?.package_name)}
onClick={()=>onClick(p?.fields?.package_name, "package")}
className="flex items-center px-4 py-2 cursor-pointer hover:bg-dc-beige200 hover:opacity-0.5"
>
<Paragraph className="pl-2 text-lg">{p?.fields?.package_name}</Paragraph>
<Paragraph className="pl-2 sm:text-lg">{p?.fields?.package_name}</Paragraph>
</li>
)
})}
Expand All @@ -107,14 +122,14 @@ export default function AutoComplete({ searchInput }: Props) {
return (
<li
key={t?.fields?.package_name+t?.fields?.name}
onClick={()=>onClick(t?.fields?.name)}
className="flex items-center px-4 py-2 cursor-pointer hover:bg-dc-beige200 hover:opacity-0.5"
onClick={()=>onClick(t, "topic")}
className="flex flex-wrap items-center px-4 py-2 cursor-pointer hover:bg-dc-beige200 hover:opacity-0.5"
>
<div>
<Paragraph className="px-2 text-lg">{`${t?.fields?.name}`}</Paragraph>
<Paragraph className="pl-2 pr-1 sm:pr-2 sm:text-lg">{`${t?.fields?.name}`}</Paragraph>
</div>
<div>
<Paragraph className="text-lg text-dc-red">{`(${t?.fields?.package_name})`}</Paragraph>
<Paragraph className="sm:text-lg">{`(${t?.fields?.package_name})`}</Paragraph>
</div>
</li>
)
Expand Down
45 changes: 29 additions & 16 deletions components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useContext, useState } from 'react';
import { FaGithub } from 'react-icons/fa';

import { ThemeContext } from '../pages/_app';
import AutoComplete from './Autocomplete';

export default function Navbar() {
const [searchInput, setSearchInput] = useState('');
Expand Down Expand Up @@ -58,23 +59,35 @@ export default function Navbar() {

{/* show search bar if relevant */}
{showSearch && (
<form onSubmit={onSubmitSearch}>
<div className="dc-input">
<label className="sr-only" htmlFor="searchBarNav">
Search all packages and functions
</label>
<Input
className="w-full md:w-80 lg:w-96"
id="searchBarNav"
name="searchBarNav"
onChange={setSearchInput}
placeholder="Search all packages and functions"
size="small"
type="search"
value={searchInput}
/>
<div className="flex flex-col max-h-4 max-w-4xl">
<form onSubmit={onSubmitSearch}>
<div className="dc-input">
<label className="sr-only" htmlFor="searchBarNav">
Search all packages and functions
</label>
<Input
className="w-full"
id="searchBarNav"
name="searchBarNav"
onChange={setSearchInput}
placeholder="Search all packages and functions"
size="small"
type="search"
value={searchInput}
/>
</div>
</form>
<div className="md:w-80 lg:w-96">
{
searchInput
&&
<AutoComplete
onSearch={()=>setSearchInput("")}
searchInput={searchInput}
/>
}
</div>
</form>
</div>
)}
</header>
);
Expand Down
9 changes: 7 additions & 2 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ export default function HomePage({ packageCount }: { packageCount?: number }) {
value={searchInput}
/>
</form>
<AutoComplete
{
searchInput
&&
<AutoComplete
onSearch={()=>setSearchInput("")}
searchInput={searchInput}
/>
/>
}
</div>
</Layout>
);
Expand Down