refactor(#329): added favorites toggle (#112)

Co-authored-by: mirlan.maksitaliev <mirlan.maksitaliev@instatsport.com>
keep-around/af30b88d367751c9e05a735e4a0467a96238ef47
Mirlan 5 years ago committed by GitHub
parent 3097cdd9f6
commit 6cbd99ac7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      src/features/ProfileCard/hooks.tsx
  2. 6
      src/features/ProfileCard/index.tsx
  3. 3
      src/features/ProfileCard/styled.tsx
  4. 6
      src/features/UserFavorites/index.tsx
  5. 16
      src/requests/modifyUserSportFavs.tsx

@ -1,8 +1,9 @@
import type { BaseSyntheticEvent } from 'react' import type { BaseSyntheticEvent } from 'react'
import { useCallback } from 'react' import { useCallback } from 'react'
import findIndex from 'lodash/findIndex' import find from 'lodash/find'
import { FavoritesActions } from 'requests'
import { handleImageError, getProfileLogo } from 'helpers' import { handleImageError, getProfileLogo } from 'helpers'
import { useSportNameParam, usePageId } from 'hooks' import { useSportNameParam, usePageId } from 'hooks'
@ -16,11 +17,11 @@ export const useProfileCard = ({ profileType }: ProfileCardProps) => {
const profileId = usePageId() const profileId = usePageId()
const { sportType } = useSportNameParam() const { sportType } = useSportNameParam()
const isFavorite = findIndex(userFavorites, { const isFavorite = Boolean(find(userFavorites, {
id: profileId, id: profileId,
sport: sportType, sport: sportType,
type: profileType, type: profileType,
}) !== -1 }))
const logo = getProfileLogo({ const logo = getProfileLogo({
id: profileId, id: profileId,
@ -28,14 +29,18 @@ export const useProfileCard = ({ profileType }: ProfileCardProps) => {
sportType, sportType,
}) })
const addToFavorites = useCallback(() => { const toggleFavorites = useCallback(() => {
const action = isFavorite
? FavoritesActions.REMOVE
: FavoritesActions.ADD
addRemoveFavorite({ addRemoveFavorite({
action: 1, action,
id: profileId, id: profileId,
sport: sportType, sport: sportType,
type: profileType, type: profileType,
}) })
}, [ }, [
isFavorite,
addRemoveFavorite, addRemoveFavorite,
profileId, profileId,
profileType, profileType,
@ -49,9 +54,9 @@ export const useProfileCard = ({ profileType }: ProfileCardProps) => {
}), [profileType, sportType]) }), [profileType, sportType])
return { return {
addToFavorites,
isFavorite, isFavorite,
logo, logo,
onError, onError,
toggleFavorites,
} }
} }

@ -27,10 +27,10 @@ export const ProfileCard = (props: ProfileCardProps) => {
} = props } = props
const { const {
addToFavorites,
isFavorite, isFavorite,
logo, logo,
onError, onError,
toggleFavorites,
} = useProfileCard(props) } = useProfileCard(props)
return ( return (
@ -45,12 +45,12 @@ export const ProfileCard = (props: ProfileCardProps) => {
<Name>{name}</Name> <Name>{name}</Name>
<Bottom> <Bottom>
{isFavorite ? ( {isFavorite ? (
<InFavorites as='div'> <InFavorites onClick={toggleFavorites}>
<StarIcon fill='#eacb6f' /> <StarIcon fill='#eacb6f' />
<T9n t='added_to_favorites' /> <T9n t='added_to_favorites' />
</InFavorites> </InFavorites>
) : ( ) : (
<AddToFavButton onClick={addToFavorites}> <AddToFavButton onClick={toggleFavorites}>
<StarIcon fill='#fff' /> <StarIcon fill='#fff' />
<T9n t='add_to_favorites' /> <T9n t='add_to_favorites' />
</AddToFavButton> </AddToFavButton>

@ -43,7 +43,7 @@ export const AddToFavButton = styled.button`
background: background:
linear-gradient( linear-gradient(
180deg, rgba(255, 255, 255, 0) 0%, 180deg, rgba(255, 255, 255, 0) 0%,
rgba(255, 255, 255, 0.1) 0.01%, rgba(255, 255, 255, 0.1) 0.01%,
rgba(0, 0, 0, 0.1) 99.99% rgba(0, 0, 0, 0.1) 99.99%
), ),
#0033CC; #0033CC;
@ -63,7 +63,6 @@ export const InFavorites = styled(AddToFavButton)`
color: #EACB6F; color: #EACB6F;
border: 1px solid currentColor; border: 1px solid currentColor;
background-color: transparent; background-color: transparent;
cursor: default;
` `
export const InfoItems = styled.div` export const InfoItems = styled.div`

@ -2,12 +2,13 @@ import React from 'react'
import map from 'lodash/map' import map from 'lodash/map'
import { FavoritesActions } from 'requests'
import { handleImageError } from 'helpers' import { handleImageError } from 'helpers'
import { Modal } from 'features/Modal' import { Modal } from 'features/Modal'
import { TooltipBlock } from './TooltipBlock' import { TooltipBlock } from './TooltipBlock'
import { useUserFavoritesStore } from './store'
import { import {
StyledLink, StyledLink,
UserSportFavItemLogoWrapper, UserSportFavItemLogoWrapper,
@ -20,7 +21,6 @@ import {
FavoriteModal, FavoriteModal,
Text, Text,
} from './styled' } from './styled'
import { useUserFavoritesStore } from './store'
export const UserFavorites = () => { export const UserFavorites = () => {
const { const {
@ -39,7 +39,7 @@ export const UserFavorites = () => {
<UserSportFavItemLogoWrapper key={`${item.type}_${item.sport}_${item.id}`}> <UserSportFavItemLogoWrapper key={`${item.type}_${item.sport}_${item.id}`}>
<UserSportFavXWrapper <UserSportFavXWrapper
onClick={() => addRemoveFavorite({ onClick={() => addRemoveFavorite({
action: 2, action: FavoritesActions.REMOVE,
id: item.id, id: item.id,
sport: item.sport, sport: item.sport,
type: item.type, type: item.type,

@ -15,8 +15,18 @@ enum FavoriteStatusTypes {
FAILURE = 2, FAILURE = 2,
} }
export enum FavoritesActions {
ADD = 1,
REMOVE = 2,
}
type Response = {
_p_data: UserFavorites,
_p_status: FavoriteStatusTypes,
}
type Args = { type Args = {
action: number, action: FavoritesActions,
id: number, id: number,
sport: SportTypes, sport: SportTypes,
type: ProfileTypes, type: ProfileTypes,
@ -28,7 +38,7 @@ export const modifyUserFavorites = async ({
id, id,
sport, sport,
type, type,
}: Args): Promise<UserFavorites> => { }: Args) => {
const config = { const config = {
body: { body: {
params: { params: {
@ -41,7 +51,7 @@ export const modifyUserFavorites = async ({
}, },
} }
const { _p_data, _p_status } = await callApi({ const { _p_data, _p_status }: Response = await callApi({
config, config,
url: DATA_URL, url: DATA_URL,
}) })

Loading…
Cancel
Save