1
- import { useState } from "react" ;
1
+ import { useEffect , useState } from "react" ;
2
2
import { useFormContext } from "react-hook-form" ;
3
3
4
4
import { Button } from "@cooper/ui/button" ;
5
- import { FormControl , FormField , FormItem } from "@cooper/ui/form" ;
5
+ import { FormControl , FormField , FormItem , FormLabel } from "@cooper/ui/form" ;
6
6
import { useCallback } from "react" ;
7
7
import {
8
8
Select ,
@@ -18,6 +18,7 @@ import type { IndustryType, LocationType } from "@cooper/db/schema";
18
18
import { api } from "~/trpc/react" ;
19
19
import { usePathname , useRouter } from "next/navigation" ;
20
20
import { z } from "zod" ;
21
+ import ComboBox from "../combo-box" ;
21
22
22
23
interface SearchBarProps {
23
24
industry ?: IndustryType ;
@@ -39,10 +40,37 @@ export function CompanySearchBar({ industry, location }: SearchBarProps) {
39
40
const [ selectedIndustry , setSelectedIndustry ] = useState < string | undefined > (
40
41
industry ,
41
42
) ;
42
- const [ selectedLocation , setSelectedLocation ] = useState < string | undefined > (
43
- location ?. city ,
43
+
44
+ const [ locationLabel , setLocationLabel ] = useState < string > (
45
+ location ? `${ location . city } , ${ location . state } ` : "Location" ,
46
+ ) ;
47
+ const [ searchTerm , setSearchTerm ] = useState < string > ( "" ) ;
48
+ const [ prefix , setPrefix ] = useState < string > ( "" ) ;
49
+
50
+ useEffect ( ( ) => {
51
+ const newPrefix =
52
+ searchTerm . length === 3 ? searchTerm . slice ( 0 , 3 ) . toLowerCase ( ) : null ;
53
+ if ( newPrefix && newPrefix !== prefix ) {
54
+ setPrefix ( newPrefix ) ;
55
+ }
56
+ } , [ prefix , searchTerm ] ) ;
57
+ const locationsToUpdate = api . location . getByPrefix . useQuery (
58
+ { prefix } ,
59
+ { enabled : searchTerm . length === 3 } ,
44
60
) ;
45
- const { data : locations = [ ] } = api . location . list . useQuery ( ) ;
61
+
62
+ const locationValuesAndLabels = locationsToUpdate . data
63
+ ? locationsToUpdate . data . map ( ( location ) => {
64
+ return {
65
+ value : location . id ,
66
+ label :
67
+ location . city +
68
+ ( location . state ? `, ${ location . state } ` : "" ) +
69
+ ", " +
70
+ location . country ,
71
+ } ;
72
+ } )
73
+ : [ ] ;
46
74
47
75
const onSubmit = ( data : FormSchema ) => {
48
76
router . push ( pathName + "?" + createQueryString ( data ) ) ;
@@ -109,32 +137,30 @@ export function CompanySearchBar({ industry, location }: SearchBarProps) {
109
137
render = { ( { field } ) => (
110
138
< FormItem className = "col-span-5 lg:col-span-2" >
111
139
< FormControl >
112
- < Select
113
- onValueChange = { ( value ) => {
114
- setSelectedLocation ( value ) ;
115
- const finalValue = value === "LOCATION" ? undefined : value ;
140
+ < ComboBox
141
+ { ...field }
142
+ variant = "filtering"
143
+ defaultLabel = { locationLabel || "Location" }
144
+ searchPlaceholder = "Type to begin..."
145
+ searchEmpty = "No location found."
146
+ valuesAndLabels = { locationValuesAndLabels }
147
+ currLabel = { locationLabel }
148
+ onChange = { ( value ) => {
149
+ setSearchTerm ( value ) ;
150
+ } }
151
+ onSelect = { ( currentValue ) => {
152
+ setLocationLabel ( currentValue ) ;
153
+ const selectedLoc = locationsToUpdate . data ?. find (
154
+ ( loc ) =>
155
+ `${ loc . city } ${ loc . state ? `, ${ loc . state } ` : "" } ${ loc . country ? `, ${ loc . country } ` : "" } ` ===
156
+ currentValue ,
157
+ ) ;
158
+ const finalValue =
159
+ currentValue === "LOCATION" ? undefined : selectedLoc ?. id ;
116
160
setValue ( field . name , finalValue ) ;
117
161
void handleSubmit ( onSubmit ) ( ) ;
118
162
} }
119
- value = { selectedLocation }
120
- >
121
- < SelectTrigger className = "h-12 w-[340px] rounded-none border-2 border-l-0 border-t-0 border-[#9A9A9A] text-lg placeholder:opacity-50 focus:ring-0 active:ring-0 lg:rounded-md lg:border-2" >
122
- < SelectValue placeholder = "Location" />
123
- </ SelectTrigger >
124
- < SelectContent >
125
- < SelectGroup >
126
- < SelectItem className = "font-bold" value = "LOCATION" >
127
- Location
128
- </ SelectItem >
129
- < SelectSeparator />
130
- { locations . map ( ( location : LocationType ) => (
131
- < SelectItem key = { location . id } value = { location . id } >
132
- { location . city + ", " + location . state }
133
- </ SelectItem >
134
- ) ) }
135
- </ SelectGroup >
136
- </ SelectContent >
137
- </ Select >
163
+ />
138
164
</ FormControl >
139
165
</ FormItem >
140
166
) }
@@ -143,7 +169,7 @@ export function CompanySearchBar({ industry, location }: SearchBarProps) {
143
169
className = "bg-white hover:bg-white hover:text-[#9A9A9A] border-white text-black"
144
170
onClick = { ( ) => {
145
171
setSelectedIndustry ( "INDUSTRY" ) ;
146
- setSelectedLocation ( "LOCATION ") ;
172
+ setLocationLabel ( " ") ;
147
173
form . setValue ( "searchIndustry" , undefined ) ;
148
174
form . setValue ( "searchLocation" , undefined ) ;
149
175
} }
0 commit comments