This repository was archived by the owner on Mar 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathImageBox.tsx
More file actions
58 lines (49 loc) · 1.44 KB
/
ImageBox.tsx
File metadata and controls
58 lines (49 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import React, { FC } from 'react'
import styled from '@emotion/styled'
import Heart from './Heart'
import { Dispatch } from 'redux'
import { useDispatch, useSelector } from 'react-redux'
import { addToFavorite, removeFromFavorite } from '../redux/actions'
import { StateProps } from '../redux/reducer'
const ImageBox: FC<{ dog: { id: number; url: string } }> = ({ dog }) => {
const { favorites } = useSelector((state: StateProps) => state)
const dispatch: Dispatch = useDispatch()
const isImageInFavorites = favorites.find((el) => el.id === dog.id)
const handelClick = () => {
if (isImageInFavorites) {
return dispatch(removeFromFavorite(dog))
} else dispatch(addToFavorite(dog))
}
return (
<ImageContainer onClick={handelClick}>
<Image src={dog.url} alt="dog image" />
<HeartBtn>
<Heart icon={isImageInFavorites ? 'redHeartIcon' : 'whiteHeartIcon'} alt="" />
</HeartBtn>
</ImageContainer>
)
}
const ImageContainer = styled.div({
cursor: 'pointer',
width: '160px',
height: '160px',
position: 'relative',
borderRadius: '10px',
overflow: 'hidden',
transition: 'all 0.3s ease-in-out',
':hover': {
boxShadow: '0 0 10px rgba(0, 0, 0, 0.5)',
transform: 'translateY(-10px)',
},
})
const Image = styled.img({
width: '100%',
height: '100%',
objectFit: 'cover',
})
const HeartBtn = styled.div({
position: 'absolute',
bottom: '10px',
right: '10px',
})
export default ImageBox