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 50
Expand file tree
/
Copy pathHome.tsx
More file actions
171 lines (147 loc) · 3.97 KB
/
Home.tsx
File metadata and controls
171 lines (147 loc) · 3.97 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import React, { useState, useEffect } from 'react';
import {icons} from '../../assets/icons/index';
import { useSelector, useDispatch } from 'react-redux';
import { itemFavourite, itemRejected } from '../../redux/actions';
import styled from '@emotion/styled';
import Favorites from '../Favourites/Favourites';
export default function Home() {
const [searchItem, setSearchItem] = useState('');
const favoriteImages = useSelector((state) => state) as string[];
const dispatch = useDispatch();
const [dogImages, setDogImages] = useState([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState('');
useEffect(() => {
setLoading(true);
setError('');
fetch('https://dog.ceo/api/breed/hound/images')
.then((res) => res.json())
.then((res) => {
setLoading(false);
setDogImages(res.message.slice(0, 10));
})
.catch(() => {
setLoading(false);
setError('Failed to fetch initial dog images');
});
}, []);
function solve() {
setLoading(true);
setDogImages([]);
setError('');
fetch(`https://dog.ceo/api/breed/${searchItem}/images`)
.then((res) => res.json())
.then((res) => {
if (res.status === 'error') {
setError('No Data Found For The Given Breed Name');
} else {
setDogImages(res.message.slice(0, 10));
}
})
.catch(() => {
setError('An error occurred while fetching data.');
})
.finally(() => {
setLoading(false);
});
}
return (
<div>
<SearchContainer>
<Input
type='text'
onChange={(e) => setSearchItem(e.target.value.toLowerCase())}
/>
<Button onClick={solve}>
<SeachIcons src={icons.searchIcon} alt="Search Icon" />
Search
</Button>
</SearchContainer>
{loading && <DefaultMess><h1>Loading...</h1></DefaultMess>}
{error && (
<DefaultMess>
<h1>{error}</h1>
</DefaultMess>
)}
{!error && dogImages.length > 0 && (
<Grid>
{dogImages.map((image, idx) => (
<ImageContainer key={idx}>
<DogPhoto src={image} alt="Dog Photo" />
<Icon
src={favoriteImages.includes(image) ? icons.redHeartIcon : icons.whiteHeartIcon}
alt={favoriteImages.includes(image) ? "Red Heart Icon" : "White Heart Icon"}
onClick={() => {
if (favoriteImages.includes(image)) {
dispatch(itemRejected(image));
} else {
dispatch(itemFavourite(image));
}
}}
/>
</ImageContainer>
))}
</Grid>
)}
<HorizontalLine />
<Favorites />
</div>
);
}
const SearchContainer = styled.div({
textAlign: 'center',
marginTop: '30px',
marginBottom: '40px',
});
const DefaultMess = styled.div({
textAlign: 'center',
});
const DogPhoto = styled.img({
width: '160px',
height: '160px'
})
const Grid = styled.div({
display: 'grid',
gridTemplateColumns: 'repeat(auto-fill, minmax(160px, 1fr))',
gap: '20px',
marginTop: '25px',
marginBottom: '30px',
width: '100%',
placeItems: 'center',
})
const Icon = styled.img({
position: 'absolute',
right: '5px',
bottom: '10px'
})
const ImageContainer = styled.div({
position:'relative',
})
const Input = styled.input({
// display:'block',
width:'80%',
height : '40px',
paddingLeft: '10px',
backgroundColor: '#F7F7F7',
borderRadius: '5px',
border: 'none'
})
const Button = styled.button({
width:'105px',
height:'40px',
backgroundColor: '#0794E3',
border: 'none',
borderRadius: '3px',
position: 'relative'
})
const SeachIcons = styled.img({
position: 'absolute',
top: '13px',
left: '11px',
height: '15px',
})
const HorizontalLine = styled.hr({
border: '1px solid #E5E5E5',
marginTop: '30px',
marginBottom: '30px',
})