|
|
|
|
@ -33,6 +33,7 @@ import { |
|
|
|
|
Match, |
|
|
|
|
TQueryParams, |
|
|
|
|
} from 'requests' |
|
|
|
|
import type { TournamentType } from 'requests/getMatches/types' |
|
|
|
|
|
|
|
|
|
const MATCHES_LIMIT = 1000 |
|
|
|
|
const OFFSET = 0 |
|
|
|
|
@ -47,23 +48,31 @@ const getDate = (date: Date) => format(date, 'yyyy-MM-dd') |
|
|
|
|
export type TActiveFilters = { |
|
|
|
|
[key: string]: Array<number>, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export type TDefaultType = { |
|
|
|
|
id: number, |
|
|
|
|
name_eng: string, |
|
|
|
|
name_rus: string, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export const useMatchFilters = (selectedDate: Date) => { |
|
|
|
|
const [filtersList, setFiltersList] = useState<Array<Match>>([]) |
|
|
|
|
type TUseMatchFilters = { |
|
|
|
|
selectedDate: Date, |
|
|
|
|
selectedTournament: TournamentType | null, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const DEFAULT_FILTERS = {} |
|
|
|
|
|
|
|
|
|
export const useMatchFilters = ({ |
|
|
|
|
selectedDate, |
|
|
|
|
selectedTournament, |
|
|
|
|
}: TUseMatchFilters) => { |
|
|
|
|
const [matchesList, setMatchesList] = useState<Array<Match>>([]) |
|
|
|
|
const [tournamentMatches, setTournamentMatches] = useState<Array<Match>>([]) |
|
|
|
|
const [isOpenList, setOpenList] = useState<string>('') |
|
|
|
|
const [activeFilters, setActiveFilters] = useState<TActiveFilters>({}) |
|
|
|
|
const [activeFilters, setActiveFilters] = useState<TActiveFilters>(DEFAULT_FILTERS) |
|
|
|
|
const [inputValue, setInputValue] = useState<string>('') |
|
|
|
|
const [openPopup, setOpenPopup] = useState(false) |
|
|
|
|
|
|
|
|
|
const setFilters = (filters: Array<Match>) => { |
|
|
|
|
setFiltersList(filters) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const openDropdownList = (title: string) => () => { |
|
|
|
|
setOpenList(title === isOpenList ? '' : title) |
|
|
|
|
} |
|
|
|
|
@ -71,6 +80,12 @@ export const useMatchFilters = (selectedDate: Date) => { |
|
|
|
|
const closeDropdownList = () => { |
|
|
|
|
setOpenList('') |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const clearAllFilters = () => { |
|
|
|
|
setInputValue('') |
|
|
|
|
setActiveFilters(DEFAULT_FILTERS) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const fetchMatches = useCallback( |
|
|
|
|
(limit: number, offset: number) => getHomeMatches({ |
|
|
|
|
date: getDate(selectedDate), |
|
|
|
|
@ -82,46 +97,84 @@ export const useMatchFilters = (selectedDate: Date) => { |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
fetchMatches(MATCHES_LIMIT, OFFSET).then((resp) => setFiltersList(resp.broadcast)) |
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
clearAllFilters() |
|
|
|
|
setMatchesList([]) |
|
|
|
|
setTournamentMatches([]) |
|
|
|
|
|
|
|
|
|
fetchMatches(MATCHES_LIMIT, OFFSET).then(({ broadcast }) => { |
|
|
|
|
setMatchesList(broadcast) |
|
|
|
|
}) |
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [selectedDate]) |
|
|
|
|
|
|
|
|
|
const getDropdownList = (filterName: string, queryString: string) => { |
|
|
|
|
useEffect(() => { |
|
|
|
|
if (selectedTournament) { |
|
|
|
|
setTournamentMatches( |
|
|
|
|
filter(matchesList, |
|
|
|
|
(match) => match.tournament.id === selectedTournament.id), |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
}, [ |
|
|
|
|
matchesList, |
|
|
|
|
selectedTournament, |
|
|
|
|
]) |
|
|
|
|
|
|
|
|
|
const matchesForFilters = useMemo(() => ( |
|
|
|
|
selectedTournament ? tournamentMatches : matchesList), |
|
|
|
|
[ |
|
|
|
|
matchesList, |
|
|
|
|
selectedTournament, |
|
|
|
|
tournamentMatches, |
|
|
|
|
]) |
|
|
|
|
|
|
|
|
|
const getDropdownList = useCallback((filterName: string, queryString: string) => { |
|
|
|
|
switch (filterName) { |
|
|
|
|
case 'gender': |
|
|
|
|
return getGender(filtersList) |
|
|
|
|
return getGender(matchesForFilters) |
|
|
|
|
case 'youth_age': |
|
|
|
|
return getAge(filtersList) |
|
|
|
|
return getAge(matchesForFilters) |
|
|
|
|
case 'division': |
|
|
|
|
return getDivision(filtersList) |
|
|
|
|
return getDivision(matchesForFilters) |
|
|
|
|
case 'main_team': |
|
|
|
|
return getMainTeam(filtersList) |
|
|
|
|
return getMainTeam(matchesForFilters) |
|
|
|
|
case 'arena': |
|
|
|
|
return getArena(filtersList, queryString) |
|
|
|
|
return getArena(matchesForFilters, queryString) |
|
|
|
|
case 'round': |
|
|
|
|
return getRound(filtersList) |
|
|
|
|
return getRound(matchesForFilters) |
|
|
|
|
case 'sport': |
|
|
|
|
return getSport(filtersList) |
|
|
|
|
return getSport(matchesForFilters) |
|
|
|
|
default: |
|
|
|
|
return [] |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}, [matchesForFilters]) |
|
|
|
|
|
|
|
|
|
const filters = useMemo(() => { |
|
|
|
|
const currentFilters = useMemo(() => { |
|
|
|
|
const arr = [] |
|
|
|
|
if (some(filtersList, (item) => !isNil(item.sport))) arr.push('sport') |
|
|
|
|
if (some(filtersList, (item) => !isNil(item.arena?.id))) arr.push('arena') |
|
|
|
|
if (some(filtersList, (item) => !isNil(item.round?.id))) arr.push('round') |
|
|
|
|
if (some(filtersList, (item) => !isNil(item.team1?.main_team) || !isNil(item.team2?.main_team))) arr.push('main_team') |
|
|
|
|
if (some(filtersList, (item) => !isNil(item.team1?.youth_age) || !isNil(item.team2?.youth_age))) arr.push('youth_age') |
|
|
|
|
if (some(filtersList, (item) => !isNil(item.team1?.gender) || !isNil(item.team2?.gender))) arr.push('gender') |
|
|
|
|
if (some(filtersList, (item) => !isNil(item.team1?.division?.id) || !isNil(item.team2?.division?.id))) arr.push('division') |
|
|
|
|
return arr |
|
|
|
|
}, [filtersList]) |
|
|
|
|
|
|
|
|
|
const filtersWitnMoreThenOneValue = filter(filters, (filterName) => ( |
|
|
|
|
getDropdownList(filterName, '').length > 1 |
|
|
|
|
)) |
|
|
|
|
|
|
|
|
|
const hasArena = some(matchesForFilters, (item) => !isNil(item.arena?.id)) |
|
|
|
|
const hasRound = some(matchesForFilters, (item) => !isNil(item.round?.id)) |
|
|
|
|
const hasSport = some(matchesForFilters, (item) => !isNil(item.sport)) |
|
|
|
|
const hasMainTeam = some(matchesForFilters, |
|
|
|
|
(item) => !isNil(item.team1?.main_team) || !isNil(item.team2?.main_team)) |
|
|
|
|
const hasGender = some(matchesForFilters, |
|
|
|
|
(item) => !isNil(item.team1?.gender) || !isNil(item.team2?.gender)) |
|
|
|
|
const hasDivision = some(matchesForFilters, |
|
|
|
|
(item) => !isNil(item.team1?.division?.id) || !isNil(item.team2?.division?.id)) |
|
|
|
|
const hasYouthAge = some(matchesForFilters, |
|
|
|
|
(item) => !isNil(item.team1?.youth_age) || !isNil(item.team2?.youth_age)) |
|
|
|
|
|
|
|
|
|
if (hasSport) arr.push('sport') |
|
|
|
|
if (hasArena) arr.push('arena') |
|
|
|
|
if (hasRound) arr.push('round') |
|
|
|
|
if (hasMainTeam) arr.push('main_team') |
|
|
|
|
if (hasYouthAge) arr.push('youth_age') |
|
|
|
|
if (hasGender) arr.push('gender') |
|
|
|
|
if (hasDivision) arr.push('division') |
|
|
|
|
|
|
|
|
|
return filter(arr, (filterName) => ( |
|
|
|
|
getDropdownList(filterName, '').length > 1 |
|
|
|
|
)) |
|
|
|
|
}, [getDropdownList, matchesForFilters]) |
|
|
|
|
|
|
|
|
|
const handleSetFilters = (filterName: string, value: number) => { |
|
|
|
|
const activeFilter = activeFilters[filterName as keyof typeof activeFilters] |
|
|
|
|
@ -138,6 +191,7 @@ export const useMatchFilters = (selectedDate: Date) => { |
|
|
|
|
[filterName]: currentValue, |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return setActiveFilters({ |
|
|
|
|
...activeFilters, |
|
|
|
|
[filterName]: [value], |
|
|
|
|
@ -147,17 +201,17 @@ export const useMatchFilters = (selectedDate: Date) => { |
|
|
|
|
const clearFilters = (filterName: string) => (e: MouseEvent | ChangeEvent<HTMLInputElement>) => { |
|
|
|
|
e.stopPropagation() |
|
|
|
|
e.preventDefault() |
|
|
|
|
return setActiveFilters({ |
|
|
|
|
|
|
|
|
|
if (filterName === 'arena') { |
|
|
|
|
setInputValue('') |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
setActiveFilters({ |
|
|
|
|
...activeFilters, |
|
|
|
|
[filterName]: [], |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const clearAllFilters = (e: MouseEvent) => { |
|
|
|
|
e.stopPropagation() |
|
|
|
|
setActiveFilters({}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const isEmptyFilters = every(activeFilters, (filterItem) => isEmpty(filterItem)) |
|
|
|
|
|
|
|
|
|
const queryParams: TQueryParams = useMemo(() => { |
|
|
|
|
@ -185,7 +239,7 @@ export const useMatchFilters = (selectedDate: Date) => { |
|
|
|
|
|
|
|
|
|
const confirmClear = (e: MouseEvent) => { |
|
|
|
|
setOpenPopup(false) |
|
|
|
|
clearAllFilters(e) |
|
|
|
|
clearAllFilters() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const clickCancel = () => { |
|
|
|
|
@ -198,6 +252,10 @@ export const useMatchFilters = (selectedDate: Date) => { |
|
|
|
|
value, |
|
|
|
|
) => result + size(value), 0) |
|
|
|
|
|
|
|
|
|
const isFiltersShown = useMemo(() => ( |
|
|
|
|
size(matchesList) < 12 || selectedTournament), |
|
|
|
|
[matchesList, selectedTournament]) |
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
activeFilters, |
|
|
|
|
changeInput, |
|
|
|
|
@ -207,17 +265,18 @@ export const useMatchFilters = (selectedDate: Date) => { |
|
|
|
|
clickClearAll, |
|
|
|
|
closeDropdownList, |
|
|
|
|
confirmClear, |
|
|
|
|
currentFilters: filtersWitnMoreThenOneValue, |
|
|
|
|
filtersList, |
|
|
|
|
currentFilters, |
|
|
|
|
filtersSize, |
|
|
|
|
getDropdownList, |
|
|
|
|
handleSetFilters, |
|
|
|
|
inputValue, |
|
|
|
|
isEmptyFilters, |
|
|
|
|
isFiltersShown, |
|
|
|
|
isOpenList, |
|
|
|
|
matchesList, |
|
|
|
|
openDropdownList, |
|
|
|
|
openPopup, |
|
|
|
|
queryParams, |
|
|
|
|
setFilters, |
|
|
|
|
setTournamentMatches, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|