Skip to content

Commit df6b218

Browse files
committed
feat: search bar feedback
now it tells you if it's loading or if there are no results closes #224
1 parent 3083c78 commit df6b218

File tree

2 files changed

+185
-25
lines changed

2 files changed

+185
-25
lines changed

src/components/light-search-bar.js

Lines changed: 103 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ OptionItem.propTypes = {
157157
const LightSearchBar = () => {
158158
const [value, setValue] = useState()
159159
const [data, setData] = useState([])
160+
const [empty, setEmpty] = useState(true)
161+
const [writing, setWriting] = useState(true)
160162
const { tcrAddress } = useContext(LightTCRViewContext)
161163
const [makeItemSearchQuery, itemSearchQuery] = useLazyQuery(ITEM_SEARCH_QUERY)
162164

@@ -171,6 +173,7 @@ const LightSearchBar = () => {
171173
first: MAX_ITEM_COUNT
172174
}
173175
})
176+
setWriting(false)
174177
}, 700)
175178

176179
useEffect(() => {
@@ -197,30 +200,108 @@ const LightSearchBar = () => {
197200
)
198201
})
199202

200-
const onSearch = value => debouncedCallback(value)
203+
const onSearch = value => {
204+
debouncedCallback(value)
205+
setWriting(true)
206+
if (value === '') setEmpty(true)
207+
else setEmpty(false)
208+
}
201209
const onChange = itemID => setValue(itemID)
202210

203-
return (
204-
<StyledSelect
205-
id="items-search-bar"
206-
showSearch
207-
value={value}
208-
defaultActiveFirstOption={false}
209-
showArrow={false}
210-
filterOption={false}
211-
onSearch={onSearch}
212-
onChange={onChange}
213-
optionLabelProp="label"
214-
notFoundContent={null}
215-
placeholder={
216-
<>
217-
<FontAwesomeIcon icon="search" /> Search...
218-
</>
219-
}
220-
>
221-
{options}
222-
</StyledSelect>
223-
)
211+
if ((writing || itemSearchQuery.loading) && !empty)
212+
return (
213+
<StyledSelect
214+
id="items-search-bar"
215+
showSearch
216+
value={value}
217+
defaultActiveFirstOption={false}
218+
showArrow={false}
219+
filterOption={false}
220+
onSearch={onSearch}
221+
onChange={onChange}
222+
optionLabelProp="label"
223+
notFoundContent={null}
224+
placeholder={
225+
<>
226+
<FontAwesomeIcon icon="search" /> Search...
227+
</>
228+
}
229+
>
230+
<Select.Option key="1" label="Loading...">
231+
Loading...
232+
</Select.Option>
233+
</StyledSelect>
234+
)
235+
236+
if (!writing && options.length === 0 && !empty)
237+
return (
238+
<StyledSelect
239+
id="items-search-bar"
240+
showSearch
241+
value={value}
242+
defaultActiveFirstOption={false}
243+
showArrow={false}
244+
filterOption={false}
245+
onSearch={onSearch}
246+
onChange={onChange}
247+
optionLabelProp="label"
248+
notFoundContent={null}
249+
placeholder={
250+
<>
251+
<FontAwesomeIcon icon="search" /> Search...
252+
</>
253+
}
254+
>
255+
<Select.Option key="2" label="No results">
256+
No results
257+
</Select.Option>
258+
</StyledSelect>
259+
)
260+
261+
if (!writing && options.length > 0)
262+
return (
263+
<StyledSelect
264+
id="items-search-bar"
265+
showSearch
266+
value={value}
267+
defaultActiveFirstOption={false}
268+
showArrow={false}
269+
filterOption={false}
270+
onSearch={onSearch}
271+
onChange={onChange}
272+
optionLabelProp="label"
273+
notFoundContent={null}
274+
placeholder={
275+
<>
276+
<FontAwesomeIcon icon="search" /> Search...
277+
</>
278+
}
279+
>
280+
{options}
281+
</StyledSelect>
282+
)
283+
else
284+
return (
285+
<StyledSelect
286+
id="items-search-bar"
287+
showSearch
288+
value={value}
289+
defaultActiveFirstOption={false}
290+
showArrow={false}
291+
filterOption={false}
292+
onSearch={onSearch}
293+
onChange={onChange}
294+
optionLabelProp="label"
295+
notFoundContent={null}
296+
placeholder={
297+
<>
298+
<FontAwesomeIcon icon="search" /> Search...
299+
</>
300+
}
301+
>
302+
{options}
303+
</StyledSelect>
304+
)
224305
}
225306

226307
export default LightSearchBar

src/components/search-bar.js

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ OptionItem.propTypes = {
165165
const SearchBar = () => {
166166
const [value, setValue] = useState()
167167
const [data, setData] = useState([])
168+
const [empty, setEmpty] = useState(true)
169+
const [writing, setWriting] = useState(true)
168170
const [enhancedDataSource, setEnhancedDataSource] = useState([])
169171
const {
170172
itemSubmissionLogs: dataSource,
@@ -264,10 +266,14 @@ const SearchBar = () => {
264266
: []
265267

266268
setData(results)
269+
setWriting(false)
267270
}, 700)
268-
const onSearch = useCallback(value => debouncedCallback(value), [
269-
debouncedCallback
270-
])
271+
const onSearch = value => {
272+
debouncedCallback(value)
273+
setWriting(true)
274+
if (value === '') setEmpty(true)
275+
else setEmpty(false)
276+
}
271277

272278
const options = data.map(d => {
273279
// Iterate through the item fields and find the first text field
@@ -294,6 +300,79 @@ const SearchBar = () => {
294300

295301
const onChange = useCallback(itemID => setValue(itemID), [])
296302

303+
if (writing && !empty)
304+
return (
305+
<StyledSelect
306+
id="items-search-bar"
307+
showSearch
308+
value={value}
309+
defaultActiveFirstOption={false}
310+
showArrow={false}
311+
filterOption={false}
312+
onSearch={onSearch}
313+
onChange={onChange}
314+
optionLabelProp="label"
315+
notFoundContent={null}
316+
placeholder={
317+
<>
318+
<FontAwesomeIcon icon="search" /> Search...
319+
</>
320+
}
321+
>
322+
<Select.Option key="1" label="Loading...">
323+
Loading...
324+
</Select.Option>
325+
</StyledSelect>
326+
)
327+
328+
if (!writing && options.length === 0 && !empty)
329+
return (
330+
<StyledSelect
331+
id="items-search-bar"
332+
showSearch
333+
value={value}
334+
defaultActiveFirstOption={false}
335+
showArrow={false}
336+
filterOption={false}
337+
onSearch={onSearch}
338+
onChange={onChange}
339+
optionLabelProp="label"
340+
notFoundContent={null}
341+
placeholder={
342+
<>
343+
<FontAwesomeIcon icon="search" /> Search...
344+
</>
345+
}
346+
>
347+
<Select.Option key="2" label="No results">
348+
No results
349+
</Select.Option>
350+
</StyledSelect>
351+
)
352+
353+
if (!writing && options.length > 0)
354+
return (
355+
<StyledSelect
356+
id="items-search-bar"
357+
showSearch
358+
value={value}
359+
defaultActiveFirstOption={false}
360+
showArrow={false}
361+
filterOption={false}
362+
onSearch={onSearch}
363+
onChange={onChange}
364+
optionLabelProp="label"
365+
notFoundContent={null}
366+
placeholder={
367+
<>
368+
<FontAwesomeIcon icon="search" /> Search...
369+
</>
370+
}
371+
>
372+
{options}
373+
</StyledSelect>
374+
)
375+
297376
return (
298377
<StyledSelect
299378
id="items-search-bar"

0 commit comments

Comments
 (0)