Skip to content

Commit 5f5df59

Browse files
committed
The start of a Search UI!
1 parent 83ec52f commit 5f5df59

File tree

4 files changed

+97
-11
lines changed

4 files changed

+97
-11
lines changed

src/client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ApolloClient, InMemoryCache } from "@apollo/client";
22

33
export const client = new ApolloClient({
4+
uri: "/graphql",
45
cache: new InMemoryCache(),
56
});

src/components/SearchResults.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { QueryResult } from "@apollo/client";
2+
import { Search } from "../pages/__generated__/Search";
3+
4+
export const SearchResults = ({
5+
loading,
6+
error,
7+
data,
8+
}: QueryResult<Search>) => {
9+
if (loading) return <div>Loading...</div>;
10+
if (error) return <div>Error: {JSON.stringify(error)}</div>;
11+
12+
return (
13+
<ul className="divide-y divide-gray-200">
14+
{data?.search.documents.edges?.map(edge => (
15+
<li className="py-4">{edge?.node?.name}</li>
16+
))}
17+
</ul>
18+
);
19+
};

src/pages/Home.tsx

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
1+
import gql from "graphql-tag";
12
import { FC, useState } from "react";
23
import { Menu, Transition } from "@headlessui/react";
34
import classNames from "classnames";
45
import { Link } from "react-router-dom";
6+
import { useQuery } from "@apollo/client";
7+
import { Search } from "./__generated__/Search";
8+
import { SearchResults } from "../components/SearchResults";
9+
10+
const SEARCH = gql`
11+
query Search($term: String!) {
12+
search(query: $term) {
13+
documents {
14+
edges {
15+
node {
16+
name
17+
description
18+
url
19+
}
20+
}
21+
}
22+
}
23+
}
24+
`;
525

626
export const Home: FC = () => {
727
const [isOpen, setIsOpen] = useState(false);
28+
const [searchTerm, setSearchTerm] = useState("");
29+
30+
const searchResults = useQuery<Search>(SEARCH, {
31+
variables: { term: searchTerm },
32+
});
833

934
return (
1035
<div style={{ minHeight: "712px" }} className="overflow-y-auto">
@@ -126,7 +151,7 @@ export const Home: FC = () => {
126151
{/* Search */}
127152
<div className="flex-1 min-w-0 px-12 lg:hidden">
128153
<div className="max-w-xs w-full mx-auto">
129-
<label htmlFor="search" className="sr-only">
154+
<label htmlFor="mobile-search" className="sr-only">
130155
Search
131156
</label>
132157
<div className="relative text-white focus-within:text-gray-600">
@@ -146,11 +171,13 @@ export const Home: FC = () => {
146171
</svg>
147172
</div>
148173
<input
149-
id="search"
150-
className="block w-full bg-white bg-opacity-20 py-2 pl-10 pr-3 border border-transparent rounded-md leading-5 text-gray-900 placeholder-white focus:outline-none focus:bg-opacity-100 focus:border-transparent focus:placeholder-gray-500 focus:ring-0 sm:text-sm"
174+
id="mobile-search"
175+
className="block w-full bg-white bg-opacity-20 py-2 pl-10 pr-3 border border-transparent rounded-md leading-5 focus:text-gray-900 placeholder-white focus:outline-none focus:bg-opacity-100 focus:border-transparent focus:placeholder-gray-500 focus:ring-0 sm:text-sm"
151176
placeholder="Search"
152177
type="search"
153178
name="search"
179+
value={searchTerm}
180+
onChange={event => setSearchTerm(event.target.value)}
154181
/>
155182
</div>
156183
</div>
@@ -272,10 +299,12 @@ export const Home: FC = () => {
272299
</div>
273300
<input
274301
id="search"
275-
className="block w-full bg-white bg-opacity-20 py-2 pl-10 pr-3 border border-transparent rounded-md leading-5 text-gray-900 placeholder-white focus:outline-none focus:bg-opacity-100 focus:border-transparent focus:placeholder-gray-500 focus:ring-0 sm:text-sm"
302+
className="block w-full bg-white bg-opacity-20 py-2 pl-10 pr-3 border border-transparent rounded-md leading-5 focus:text-gray-900 placeholder-white focus:outline-none focus:bg-opacity-100 focus:border-transparent focus:placeholder-gray-500 focus:ring-0 sm:text-sm"
276303
placeholder="Search"
277304
type="search"
278305
name="search"
306+
value={searchTerm}
307+
onChange={event => setSearchTerm(event.target.value)}
279308
/>
280309
</div>
281310
</div>
@@ -440,29 +469,28 @@ export const Home: FC = () => {
440469
{/* Main 3 column grid */}
441470
<div className="grid grid-cols-1 gap-4 items-start lg:grid-cols-3 lg:gap-8">
442471
{/* Left column */}
443-
<div className="grid grid-cols-1 gap-4 lg:col-span-2">
472+
{/* <div className="grid grid-cols-1 gap-4 lg:col-span-2">
444473
<section aria-labelledby="section-1-title">
445474
<h2 className="sr-only" id="section-1-title">
446475
Section title
447476
</h2>
448477
<div className="rounded-lg bg-white overflow-hidden shadow">
449478
<div className="p-6">
450-
{/* <x-placeholder>
451-
<div className="h-96 border-4 border-dashed border-gray-200 rounded-lg"></div>
452-
</x-placeholder> */}
479+
<div className="h-96 border-4 border-dashed border-gray-200 rounded-lg"></div>
453480
</div>
454481
</div>
455482
</section>
456-
</div>
483+
</div> */}
457484

458485
{/* Right column */}
459-
<div className="grid grid-cols-1 gap-4">
486+
<div className="grid grid-cols-1 lg:col-span-3 gap-4">
460487
<section aria-labelledby="section-2-title">
461488
<h2 className="sr-only" id="section-2-title">
462489
Section title
463490
</h2>
464491
<div className="rounded-lg bg-white overflow-hidden shadow">
465-
<div className="p-6">
492+
<div className="px-6 py-2">
493+
<SearchResults {...searchResults} />
466494
{/* <x-placeholder>
467495
<div className="h-96 border-4 border-dashed border-gray-200 rounded-lg"></div>
468496
</x-placeholder> */}

src/pages/__generated__/Search.ts

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)