Merge pull request #4 from Team-1280/maybe-fix-search

change to React.useState and encode/decode URI
This commit is contained in:
Youwen Wu 2024-02-12 21:43:21 -08:00 committed by GitHub
commit 5dba4f918b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 6 additions and 18 deletions

View file

@ -1,32 +1,21 @@
'use client' 'use client'
import { create } from 'zustand'
import { navigate } from '@/app/actions' import { navigate } from '@/app/actions'
import { useState } from 'react'
interface SearchBarState {
searchInput: string
setSearchInput: (newInput: string) => void
}
const useSearchBarStore = create<SearchBarState>((set) => ({
searchInput: '',
setSearchInput: (newInput) => set({ searchInput: newInput }),
}))
export default function SearchBar() { export default function SearchBar() {
const searchBarStore = useSearchBarStore((state) => state.searchInput) const [searchInput, setSearchInput] = useState('')
const setSearchBarStore = useSearchBarStore((state) => state.setSearchInput)
const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => { const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
navigate(`/search?q=${searchBarStore.split(' ').join('+')}`) navigate(`/search?q=${searchInput.split(' ').join('+')}`)
} }
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setSearchBarStore(e.target.value) setSearchInput(e.target.value)
} }
const handleKeyPress = (e: React.KeyboardEvent<HTMLInputElement>) => { const handleKeyPress = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Enter') { if (e.key === 'Enter') {
navigate(`/search?q=${searchBarStore.split(' ').join('+')}`) navigate(`/search?q=${encodeURIComponent(searchInput)}`)
} }
} }
@ -38,7 +27,6 @@ export default function SearchBar() {
name='q' name='q'
placeholder='Search...' placeholder='Search...'
onChange={handleInputChange} onChange={handleInputChange}
value={searchBarStore}
onKeyDown={handleKeyPress} onKeyDown={handleKeyPress}
/> />
<button <button

View file

@ -53,7 +53,7 @@ const SearchResult = ({ result }: { result: CustomSearchResult }) => {
export default function Page() { export default function Page() {
const searchParams = useSearchParams() const searchParams = useSearchParams()
const search = searchParams.get('q') as string const search = decodeURIComponent(searchParams.get('q') as string)
const { data, error } = useSuspenseQuery({ const { data, error } = useSuspenseQuery({
queryKey: [search], queryKey: [search],