change to React.useState and encode/decode URI
This commit is contained in:
parent
595d1771d5
commit
b28dbb6702
2 changed files with 6 additions and 18 deletions
|
@ -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
|
||||||
|
|
|
@ -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],
|
||||||
|
|
Loading…
Reference in a new issue