You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
spa_instat_tv/src/features/UserFavorites/hooks/index.tsx

47 lines
1.0 KiB

import { useCallback, useState } from 'react'
import find from 'lodash/find'
import type { UserFavorites } from 'requests'
import { modifyUserFavorites, getUserFavorites } from 'requests'
import { useToggle } from 'hooks/useToggle'
import { ProfileTypes } from 'config'
type Args = Parameters<typeof modifyUserFavorites>[0]
export const useUserFavorites = () => {
const [userFavorites, setUserFavorites] = useState<UserFavorites>([])
const {
close,
isOpen,
open,
} = useToggle()
const fetchFavorites = useCallback(() => {
getUserFavorites().then((value) => {
if (Array.isArray(value)) {
setUserFavorites(value)
}
})
}, [])
const addRemoveFavorite = (args: Args) => {
modifyUserFavorites(args).then(setUserFavorites, open)
}
const isInFavorites = (profileType: ProfileTypes, profileId: number) => (
Boolean(find(userFavorites, { id: profileId, type: profileType }))
)
return {
addRemoveFavorite,
close,
fetchFavorites,
isInFavorites,
isOpen,
open,
userFavorites,
}
}