|
|
|
|
@ -1,14 +1,25 @@ |
|
|
|
|
import { useCallback, useState } from 'react' |
|
|
|
|
import { |
|
|
|
|
useCallback, |
|
|
|
|
useMemo, |
|
|
|
|
useState, |
|
|
|
|
} from 'react' |
|
|
|
|
|
|
|
|
|
import find from 'lodash/find' |
|
|
|
|
import forEach from 'lodash/forEach' |
|
|
|
|
import { reduce } from 'lodash' |
|
|
|
|
|
|
|
|
|
import type { ObjectWithName } from 'features/Name' |
|
|
|
|
|
|
|
|
|
import type { UserFavorites } from 'requests' |
|
|
|
|
import { modifyUserFavorites, getUserFavorites } from 'requests' |
|
|
|
|
import { |
|
|
|
|
FavoritesActions, |
|
|
|
|
UserFavorites, |
|
|
|
|
modifyUserFavorites, |
|
|
|
|
getUserFavorites, |
|
|
|
|
} from 'requests' |
|
|
|
|
|
|
|
|
|
import { useToggle } from 'hooks/useToggle' |
|
|
|
|
import { ProfileTypes } from 'config' |
|
|
|
|
|
|
|
|
|
import { ProfileTypes, SportTypes } from 'config' |
|
|
|
|
|
|
|
|
|
type ProfileType = { |
|
|
|
|
profile: ObjectWithName & { |
|
|
|
|
@ -21,6 +32,11 @@ type ProfileType = { |
|
|
|
|
sportType: number, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export type SuperTournament = ObjectWithName & { |
|
|
|
|
sport: SportTypes, |
|
|
|
|
type: ProfileTypes, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type Args = Parameters<typeof modifyUserFavorites>[0] |
|
|
|
|
|
|
|
|
|
export const useUserFavorites = () => { |
|
|
|
|
@ -33,6 +49,29 @@ export const useUserFavorites = () => { |
|
|
|
|
open, |
|
|
|
|
} = useToggle() |
|
|
|
|
|
|
|
|
|
const superTournaments = useMemo(() => { |
|
|
|
|
const uniqueTournamnetIds: Record<number, number> = {} |
|
|
|
|
|
|
|
|
|
return reduce( |
|
|
|
|
userFavorites, |
|
|
|
|
(acc, item) => { |
|
|
|
|
if ( |
|
|
|
|
item.info.super_tournament |
|
|
|
|
&& !uniqueTournamnetIds[item.info.super_tournament.id] |
|
|
|
|
) { |
|
|
|
|
uniqueTournamnetIds[item.info.super_tournament.id] = item.info.super_tournament.id |
|
|
|
|
acc.push({ |
|
|
|
|
...item.info.super_tournament, |
|
|
|
|
sport: item.sport, |
|
|
|
|
type: item.type, |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
return acc |
|
|
|
|
}, |
|
|
|
|
[] as Array<SuperTournament>, |
|
|
|
|
) |
|
|
|
|
}, [userFavorites]) |
|
|
|
|
|
|
|
|
|
const fetchFavorites = useCallback(() => { |
|
|
|
|
getUserFavorites().then((value) => { |
|
|
|
|
if (Array.isArray(value)) { |
|
|
|
|
@ -41,6 +80,19 @@ export const useUserFavorites = () => { |
|
|
|
|
}) |
|
|
|
|
}, []) |
|
|
|
|
|
|
|
|
|
const removeSuperTournament = (id: number) => { |
|
|
|
|
forEach(userFavorites, (item) => { |
|
|
|
|
if (item.info.super_tournament?.id === id) { |
|
|
|
|
addRemoveFavorite({ |
|
|
|
|
action: FavoritesActions.REMOVE, |
|
|
|
|
id: item.id, |
|
|
|
|
sport: item.sport, |
|
|
|
|
type: item.type, |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const addRemoveFavorite = (args: Args) => { |
|
|
|
|
modifyUserFavorites(args).then(setUserFavorites, open) |
|
|
|
|
} |
|
|
|
|
@ -57,7 +109,9 @@ export const useUserFavorites = () => { |
|
|
|
|
isOpen, |
|
|
|
|
open, |
|
|
|
|
playerHighlight, |
|
|
|
|
removeSuperTournament, |
|
|
|
|
setPlayerHighlight, |
|
|
|
|
superTournaments, |
|
|
|
|
userFavorites, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|