mirror of
https://github.com/ershisan99/flashcards-example-project.git
synced 2025-12-16 12:33:18 +00:00
feat: add debounce to search
This commit is contained in:
@@ -37,6 +37,7 @@
|
|||||||
"react-toastify": "^9.1.3",
|
"react-toastify": "^9.1.3",
|
||||||
"remeda": "^1.33.0",
|
"remeda": "^1.33.0",
|
||||||
"storybook-addon-react-router-v6": "^2.0.10",
|
"storybook-addon-react-router-v6": "^2.0.10",
|
||||||
|
"usehooks-ts": "^3.1.0",
|
||||||
"zod": "^3.22.4"
|
"zod": "^3.22.4"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
5099
pnpm-lock.yaml
generated
5099
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -1,3 +1,5 @@
|
|||||||
|
import { useCallback } from 'react'
|
||||||
|
|
||||||
import { isNil } from 'remeda'
|
import { isNil } from 'remeda'
|
||||||
|
|
||||||
export function useQueryParam<T extends boolean | number | string>(
|
export function useQueryParam<T extends boolean | number | string>(
|
||||||
@@ -9,14 +11,17 @@ export function useQueryParam<T extends boolean | number | string>(
|
|||||||
const paramValue = searchParams.get(param)
|
const paramValue = searchParams.get(param)
|
||||||
const convertedValue = getConvertedValue<T>(paramValue, defaultValue)
|
const convertedValue = getConvertedValue<T>(paramValue, defaultValue)
|
||||||
|
|
||||||
const setParamValue = (value: T | null): void => {
|
const setParamValue = useCallback(
|
||||||
if (isNil(value) || value === '') {
|
(value: T | null): void => {
|
||||||
searchParams.delete(param)
|
if (isNil(value) || value === '') {
|
||||||
} else {
|
searchParams.delete(param)
|
||||||
searchParams.set(param, String(value))
|
} else {
|
||||||
}
|
searchParams.set(param, String(value))
|
||||||
setSearchParams(searchParams)
|
}
|
||||||
}
|
setSearchParams(searchParams)
|
||||||
|
},
|
||||||
|
[searchParams, setSearchParams]
|
||||||
|
)
|
||||||
|
|
||||||
return [convertedValue, setParamValue]
|
return [convertedValue, setParamValue]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import {
|
|||||||
useGetDecksQuery,
|
useGetDecksQuery,
|
||||||
useUpdateDeckMutation,
|
useUpdateDeckMutation,
|
||||||
} from '@/services/decks'
|
} from '@/services/decks'
|
||||||
|
import { useDebounceCallback } from 'usehooks-ts'
|
||||||
|
|
||||||
import s from './decks-page.module.scss'
|
import s from './decks-page.module.scss'
|
||||||
|
|
||||||
@@ -21,7 +22,6 @@ export const DecksPage = () => {
|
|||||||
const [showCreateModal, setShowCreateModal] = useState(false)
|
const [showCreateModal, setShowCreateModal] = useState(false)
|
||||||
const [deckToDeleteId, setDeckToDeleteId] = useState<null | string>(null)
|
const [deckToDeleteId, setDeckToDeleteId] = useState<null | string>(null)
|
||||||
const [deckToEditId, setDeckToEditId] = useState<null | string>(null)
|
const [deckToEditId, setDeckToEditId] = useState<null | string>(null)
|
||||||
|
|
||||||
const showEditModal = !!deckToEditId
|
const showEditModal = !!deckToEditId
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@@ -40,6 +40,7 @@ export const DecksPage = () => {
|
|||||||
setSort,
|
setSort,
|
||||||
sort,
|
sort,
|
||||||
} = useDeckSearchParams()
|
} = useDeckSearchParams()
|
||||||
|
const [searchInputValue, setSearchInputValue] = useState<null | string>(search)
|
||||||
|
|
||||||
const currentUserId = me?.id
|
const currentUserId = me?.id
|
||||||
const authorId = currentTab === 'my' ? currentUserId : undefined
|
const authorId = currentTab === 'my' ? currentUserId : undefined
|
||||||
@@ -54,6 +55,7 @@ export const DecksPage = () => {
|
|||||||
const resetFilters = () => {
|
const resetFilters = () => {
|
||||||
setCurrentPage(null)
|
setCurrentPage(null)
|
||||||
setSearch(null)
|
setSearch(null)
|
||||||
|
setSearchInputValue(null)
|
||||||
setMinCards(null)
|
setMinCards(null)
|
||||||
setMaxCards(null)
|
setMaxCards(null)
|
||||||
setRangeValue([0, decks?.maxCardsCount ?? null])
|
setRangeValue([0, decks?.maxCardsCount ?? null])
|
||||||
@@ -72,9 +74,10 @@ export const DecksPage = () => {
|
|||||||
|
|
||||||
const openCreateModal = () => setShowCreateModal(true)
|
const openCreateModal = () => setShowCreateModal(true)
|
||||||
|
|
||||||
|
const updateSearch = useDebounceCallback(setSearch, 500)
|
||||||
const handleSearch = (search: null | string) => {
|
const handleSearch = (search: null | string) => {
|
||||||
setCurrentPage(null)
|
setCurrentPage(null)
|
||||||
setSearch(search)
|
setSearchInputValue(search)
|
||||||
}
|
}
|
||||||
const handleSliderCommitted = (value: number[]) => {
|
const handleSliderCommitted = (value: number[]) => {
|
||||||
setCurrentPage(null)
|
setCurrentPage(null)
|
||||||
@@ -132,10 +135,13 @@ export const DecksPage = () => {
|
|||||||
</div>
|
</div>
|
||||||
<div className={s.filters}>
|
<div className={s.filters}>
|
||||||
<TextField
|
<TextField
|
||||||
onValueChange={handleSearch}
|
onValueChange={value => {
|
||||||
|
updateSearch(value)
|
||||||
|
handleSearch(value)
|
||||||
|
}}
|
||||||
placeholder={'Search'}
|
placeholder={'Search'}
|
||||||
search
|
search
|
||||||
value={search ?? ''}
|
value={searchInputValue ?? ''}
|
||||||
/>
|
/>
|
||||||
<Tabs asChild onValueChange={handleTabChange} value={currentTab ?? undefined}>
|
<Tabs asChild onValueChange={handleTabChange} value={currentTab ?? undefined}>
|
||||||
<TabsList>
|
<TabsList>
|
||||||
|
|||||||
Reference in New Issue
Block a user