-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement search using query string
- Loading branch information
Showing
6 changed files
with
154 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,15 @@ | ||
import { Header } from "components"; | ||
import { SearchContainer } from "./search/components"; | ||
|
||
import { SearchInput, SearchResultTable } from "./search/components"; | ||
|
||
export default function SearchPage() { | ||
const SearchPage = () => { | ||
return ( | ||
<> | ||
<Header>Companies</Header> | ||
<main className="flex flex-col items-center w-full px-4 sm:px-6 lg:px-8"> | ||
<div className="w-full max-w-6xl mt-8"> | ||
<h2 className="text-2xl font-semibold text-gray-800 mb-6"> | ||
Search for a Company | ||
</h2> | ||
<SearchInput /> | ||
<SearchResultTable /> | ||
</div> | ||
<SearchContainer /> | ||
</main> | ||
</> | ||
); | ||
} | ||
}; | ||
|
||
export default SearchPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { default as SearchResultTable } from "./search-result-table"; | ||
export { default as SearchInput } from "./search-input"; | ||
export { default as SearchContainer } from "./search-container"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
"use client"; | ||
import { createQueryString } from "lib/utils"; | ||
import SearchInput from "./search-input"; | ||
import SearchResultTable from "./search-result-table"; | ||
import { usePathname, useRouter, useSearchParams } from "next/navigation"; | ||
import { useDebouncedCallback } from "@react-hookz/web"; | ||
|
||
interface SearchResults { | ||
content: Content[]; | ||
pageable: Pageable; | ||
} | ||
|
||
interface Pageable { | ||
size: number; | ||
pageNumber: number; | ||
offset: number; | ||
} | ||
|
||
interface Content { | ||
name: string; | ||
city: string; | ||
county: string; | ||
type: string; | ||
route: string; | ||
} | ||
|
||
const mockData = { | ||
content: [ | ||
{ | ||
name: "ABC company", | ||
city: "London", | ||
county: "London", | ||
type: "Worker", | ||
route: "Skilled Worker", | ||
}, | ||
{ | ||
name: "ABC company", | ||
city: "London", | ||
county: "London", | ||
type: "Worker", | ||
route: "Skilled Worker", | ||
}, | ||
{ | ||
name: "ABC company", | ||
city: "London", | ||
county: "London", | ||
type: "Worker", | ||
route: "Skilled Worker", | ||
}, | ||
{ | ||
name: "ABC company", | ||
city: "London", | ||
county: "London", | ||
type: "Worker", | ||
route: "Skilled Worker", | ||
}, | ||
], | ||
pageable: { | ||
size: 10, | ||
pageNumber: 0, | ||
offset: 0, | ||
}, | ||
}; | ||
|
||
const SearchContainer = () => { | ||
const router = useRouter(); | ||
const pathname = usePathname(); | ||
const searchParams = useSearchParams(); | ||
|
||
const keyword = (searchParams.get("keyword") as string) ?? ""; | ||
|
||
const changeKeyword = (value: string) => { | ||
const keywordQueryString = createQueryString({ | ||
searchParams, | ||
name: "keyword", | ||
value, | ||
}); | ||
router.replace(`${pathname}?${keywordQueryString}`, { | ||
scroll: true, | ||
}); | ||
}; | ||
|
||
const onChangeKeyword = useDebouncedCallback( | ||
changeKeyword, | ||
[changeKeyword], | ||
500 | ||
); | ||
|
||
const onSubmit = (e: React.FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
const formData = new FormData(e.currentTarget); | ||
const value = formData.get("search-input") as string; | ||
changeKeyword(value); | ||
}; | ||
|
||
return ( | ||
<div className="w-full max-w-6xl mt-8"> | ||
<h2 className="text-2xl font-semibold text-gray-800 mb-6"> | ||
Search for a Company | ||
</h2> | ||
<SearchInput | ||
value={keyword} | ||
onSubmit={onSubmit} | ||
onChange={onChangeKeyword} | ||
/> | ||
<SearchResultTable searchResults={mockData} /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SearchContainer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters