-
Notifications
You must be signed in to change notification settings - Fork 0
/
Collection.tsx
290 lines (268 loc) · 8.52 KB
/
Collection.tsx
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//////////////////////////////collection stack////////////////////////////////
import React, { useState, useEffect } from "react";
import {
View,
Text,
Image,
StatusBar,
FlatList,
TouchableOpacity,
ImageBackground,
} from "react-native";
import { StackNavigationProp } from "@react-navigation/stack";
import { SafeAreaView } from "react-native-safe-area-context";
import LottieView from "lottie-react-native";
import axios from "axios";
import ItemContainer from "./components/ItemContainer";
import { NotFound } from "./assets";
/////////////////////////////////////////////////////////////////////////////////
//typography from the stack navigation
type RootStackParamList = {
Collection: undefined;
Details: { imageUrl: string };
};
//typography from stacknavigationprop for the nav. to "Collection"
type CollectionScreenNavigationProp = StackNavigationProp<
RootStackParamList,
"Collection"
>;
//object typography for the cat images as object (json)
interface CollectionProps {
navigation: CollectionScreenNavigationProp;
}
///////////////////////////////////////////////////////////////////////////////////
//base URL for fetching images from the backend server
const API_URL = "http://192.168.178.26:8021";
const Collection: React.FC<CollectionProps> = ({ navigation }) => {
//change state to load the images
const [load, setLoad] = useState<number>(1);
const [catImages, setCatImages] = useState<{ id: string; url: string }[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
//function to fetch the images with axios
const fetchImages = async (
load: number
): Promise<{ id: string; url: string }[]> => {
try {
const response = await axios.get(API_URL, {
params: {
load,
limit: 24, //change the limit to load more images from api
},
});
return response.data;
} catch (error) {
console.error("Error fetching images", error);
throw error;
}
};
//useEffect for initial data fetching on component mount
useEffect(() => {
fetchImagesData();
}, []);
//function to handle the data loading and error catching
const fetchImagesData = async (): Promise<void> => {
try {
setIsLoading(true);
setError(null);
const images = await fetchImages(load);
console.log("API response", images);
setCatImages(images);
setIsLoading(false);
} catch (error) {
console.error("Error fetching images", error);
setCatImages([]);
setError("Failed to fetch ImageBase. Please try again.");
} finally {
setIsLoading(false);
}
};
//function to load morge images if the user scrolled to the end of screen
const moreImages = async () => {
try {
setIsLoading(true);
const moreImagesData = await fetchImages(load + 1);
setCatImages((prevImages) => [...prevImages, ...moreImagesData]);
setIsLoading(false);
} catch (error) {
console.error("Error loading more images", error);
} finally {
setIsLoading(false);
}
};
//function for the refresh button
const handleRefresh = () => {
setLoad(load + 1); //change value for a new request
fetchImagesData();
};
//navigation function to send the image data to the detailscreen
const navigateToDetails = (imageUrl: string) => {
console.log("Navigating to details with imageUrl:", imageUrl);
navigation.navigate("Details", {
imageUrl,
});
};
return (
<ImageBackground
source={require("./assets/background_dark.png")}
style={{ flex: 1, justifyContent: "center", width: "100%" }}
>
<SafeAreaView>
<View
style={{
width: "100%",
height: "100%",
}}
>
{/*--HEADING SECTION--*/}
<View
style={{
height: 150,
backgroundColor: "transparent",
justifyContent: "center",
alignItems: "center",
gap: 10,
}}
>
{/*--H1--*/}
<View
style={{
flex: 1,
justifyContent: "center",
alignItems: "center",
}}
>
<Text
style={{
marginTop: 20,
fontSize: 38,
fontFamily: "MontserratAlternatesBold",
color: "pink",
}}
>
Co<Text style={{ color: "hotpink" }}>ll</Text>ection
</Text>
</View>
{/*--H3--*/}
<Text
style={{
marginBottom: 50,
fontSize: 17,
fontFamily: "MontserratAlternatesRegular",
color: "pink",
}}
>
click any picture, vote, comment and smile
</Text>
</View>
{/*--NO DATA FOUND SECTION--*/}
{catImages.length === 0 && !isLoading ? ( //no data found
<View
style={{
width: "100%",
height: 800,
justifyContent: "center",
alignItems: "center",
}}
>
<Image source={NotFound} style={{ width: 120, height: 120 }} />
<Text
style={{
fontSize: 25,
color: "hotpink",
fontFamily: "MontserratAlternatesRegular",
}}
>
Oops, found no data
</Text>
<View
style={{
marginTop: 120,
paddingHorizontal: 110,
justifyContent: "center",
alignItems: "center",
}}
>
{/*--refresh button--*/}
<TouchableOpacity onPress={handleRefresh}>
<Text
style={{
marginBottom: 100,
fontFamily: "MontserratAlternatesBold",
fontSize: 26,
color: "pink",
paddingHorizontal: 28,
paddingVertical: 10,
borderWidth: 2,
borderColor: "lightgray",
borderRadius: 12,
backgroundColor: "transparent",
}}
>
Refresh
</Text>
</TouchableOpacity>
</View>
</View>
) : ///////////////LOADING SECTION
isLoading ? (
<View
style={{
height: 700,
width: "100%",
alignItems: "center",
}}
>
<Text
style={{
marginTop: 130,
color: "hotpink",
fontSize: 15,
fontFamily: "MontserratAlternatesRegular",
}}
>
LOADING
</Text>
<LottieView
source={require("./assets/lottie_animation/cat-climber.json")}
autoPlay
loop
style={{ width: 400, height: 400 }}
/>
</View>
) : (
/////////////IMAGE LIST SECTION
<View style={{ flex: 1, justifyContent: "center" }}>
<FlatList
data={catImages}
numColumns={2}
renderItem={({ item }) => (
<View style={{ flex: 1, padding: 12, alignItems: "center" }}>
<ItemContainer
onPress={() => navigateToDetails(item.url)}
key={item.id}
imageSrc={item.url}
/>
</View>
)}
keyExtractor={(item) => item.id.toString()} //importent for fetch data from API
onEndReached={moreImages} //importent to load more data if the list ended
initialNumToRender={5}
onEndReachedThreshold={0.1}
contentContainerStyle={{ justifyContent: "center" }}
style={{ flex: 1 }}
/>
</View>
)}
</View>
{/*--statusbar styleing--*/}
<StatusBar
translucent={true}
backgroundColor="transparent"
barStyle="light-content"
/>
</SafeAreaView>
</ImageBackground>
);
};
export default Collection;