Skip to content

Commit

Permalink
Merge pull request #6 from AlekseiBestuzhev/delay_for_requests
Browse files Browse the repository at this point in the history
feat: delay for requests
  • Loading branch information
AlekseiBestuzhev committed Nov 23, 2023
2 parents bf87456 + b651197 commit 1ee6f6e
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 7 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/common/consts/email-recovering-template.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { appDeploy } from '@/common/consts/appDeploy.ts'
import { appDeploy } from '@/common/consts/app-deploy.ts'
import { ROUTES } from '@/common/consts/routes.ts'

export const emailRecoveringTemplate = `
Expand Down
2 changes: 1 addition & 1 deletion src/common/consts/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { emailRecoveringTemplate } from './email-recovering-template.ts'
export { ROUTES } from './routes.ts'
export { appDeploy } from './appDeploy.ts'
export { appDeploy } from './app-deploy.ts'
1 change: 1 addition & 0 deletions src/common/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { useAppDispatch } from './use-app-dispatch.ts'
export { useAppSelector } from './use-app-selector.ts'
export { useDebounce } from './use-debounce.ts'
15 changes: 15 additions & 0 deletions src/common/hooks/use-debounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useEffect, useState } from 'react'

export function useDebounce<T>(value: T, delay?: number): T {
const [debouncedValue, setDebouncedValue] = useState<T>(value)

useEffect(() => {
const timer = setTimeout(() => setDebouncedValue(value), delay || 1000)

return () => {
clearTimeout(timer)
}
}, [value, delay])

return debouncedValue
}
5 changes: 4 additions & 1 deletion src/pages/pack/pack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Link, useNavigate } from 'react-router-dom'
import s from './pack.module.scss'

import { ROUTES } from '@/common/consts'
import { useDebounce } from '@/common/hooks'
import { getSortedString, requestHandler } from '@/common/utils'
import { BackButton } from '@/components/ui/back-button'
import { Button } from '@/components/ui/button'
Expand All @@ -25,6 +26,8 @@ export const Pack = () => {
const { packId, currentPage, pageSize, setCurrentPage, setPageSize, searchName, setSearchName } =
usePackData()

const debouncedSearchName = useDebounce(searchName)

const navigate = useNavigate()

const { data: pack, isLoading: packLoading } = useGetDeckInfoQuery({ id: packId })
Expand All @@ -42,7 +45,7 @@ export const Pack = () => {
const { data } = useGetCardsQuery({
id: packId as string,
params: {
question: searchName,
question: debouncedSearchName,
orderBy: sortedString,
currentPage,
itemsPerPage: pageSize,
Expand Down
12 changes: 8 additions & 4 deletions src/pages/packs/packs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useEffect, useState } from 'react'

import s from './packs.module.scss'

import { useDebounce } from '@/common/hooks'
import { getSortedString, requestHandler } from '@/common/utils'
import { PackForm } from '@/components/forms/pack'
import { Button } from '@/components/ui/button'
Expand All @@ -19,6 +20,9 @@ export const Packs = () => {
const { searchName, tabValue, sliderValue, setSearchName, setTabValue, setSliderValue } =
usePacksFilter()

const debouncedSearchName = useDebounce(searchName)
const debouncedSliderValue = useDebounce(sliderValue)

const [open, setOpen] = useState(false)

const [sort, setSort] = useState<Sort>({ key: 'updated', direction: 'desc' })
Expand All @@ -29,18 +33,18 @@ export const Packs = () => {
const userId = (data as UserResponse).id

const packs = useGetDecksQuery({
name: searchName,
name: debouncedSearchName,
authorId: tabValue,
minCardsCount: sliderValue[0],
maxCardsCount: sliderValue[1],
minCardsCount: debouncedSliderValue[0],
maxCardsCount: debouncedSliderValue[1],
orderBy: sortedString,
currentPage,
itemsPerPage: pageSize,
})

useEffect(() => {
setCurrentPage(1)
}, [searchName, sliderValue, pageSize, tabValue])
}, [debouncedSearchName, debouncedSliderValue, pageSize, tabValue])

const [createDeck] = useCreateDeckMutation()

Expand Down

0 comments on commit 1ee6f6e

Please sign in to comment.