|
|
|
|
@ -1,10 +1,11 @@ |
|
|
|
|
import type { BaseSyntheticEvent, MouseEvent } from 'react' |
|
|
|
|
import type { MouseEvent } from 'react' |
|
|
|
|
import { useEffect, useState } from 'react' |
|
|
|
|
|
|
|
|
|
import find from 'lodash/find' |
|
|
|
|
|
|
|
|
|
import type { Tournaments } from 'requests' |
|
|
|
|
import { getSportTournaments } from 'requests' |
|
|
|
|
import { useToggle } from 'hooks' |
|
|
|
|
import { useRequest, useToggle } from 'hooks' |
|
|
|
|
|
|
|
|
|
import { useHeaderFiltersStore } from 'features/HeaderFilters' |
|
|
|
|
import { useLexicsStore } from 'features/LexicsStore' |
|
|
|
|
@ -26,25 +27,26 @@ export const useTournamentFilter = () => { |
|
|
|
|
isOpen, |
|
|
|
|
open, |
|
|
|
|
} = useToggle() |
|
|
|
|
const { |
|
|
|
|
isFetching, |
|
|
|
|
request: requestTournaments, |
|
|
|
|
} = useRequest(getSportTournaments) |
|
|
|
|
const [page, setPage] = useState(0) |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
setList([]) |
|
|
|
|
getSportTournaments(selectedSportTypeId).then(setList) |
|
|
|
|
}, [selectedSportTypeId]) |
|
|
|
|
|
|
|
|
|
const [requestSent, setRequestSent] = useState(false) |
|
|
|
|
requestTournaments(selectedSportTypeId, 0).then((tournaments) => { |
|
|
|
|
setList(tournaments) |
|
|
|
|
setPage(1) |
|
|
|
|
}) |
|
|
|
|
}, [selectedSportTypeId, requestTournaments]) |
|
|
|
|
|
|
|
|
|
const handleScroll = (e: BaseSyntheticEvent) => { |
|
|
|
|
const scrollPositionBottom = e.target.scrollHeight - e.target.scrollTop |
|
|
|
|
- e.target.clientHeight - 400 <= 0 |
|
|
|
|
const fetchMore = async () => { |
|
|
|
|
if (isFetching) return |
|
|
|
|
|
|
|
|
|
if (scrollPositionBottom && !requestSent) { |
|
|
|
|
setRequestSent(true) |
|
|
|
|
getSportTournaments(selectedSportTypeId).then((res) => { |
|
|
|
|
setList([...list, ...res]) |
|
|
|
|
setRequestSent(false) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
const newTournaments = await requestTournaments(selectedSportTypeId, page) |
|
|
|
|
setList([...list, ...newTournaments]) |
|
|
|
|
setPage(page + 1) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const tournaments = normalizeTournaments(list, suffix) |
|
|
|
|
@ -66,7 +68,7 @@ export const useTournamentFilter = () => { |
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
close, |
|
|
|
|
handleScroll, |
|
|
|
|
fetchMore, |
|
|
|
|
isOpen, |
|
|
|
|
onResetSelectedTournament, |
|
|
|
|
onTournamentSelect, |
|
|
|
|
|