-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForYouTvSeeMore.js
More file actions
332 lines (328 loc) · 10.2 KB
/
ForYouTvSeeMore.js
File metadata and controls
332 lines (328 loc) · 10.2 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import React, { Component, useEffect, useState } from "react";
import {
View,
Text,
FlatList,
TouchableOpacity,
Image,
TextInput,
Alert,
Button,
} from "react-native";
import { Badge } from "@rneui/themed";
import axios from "axios";
import Loading from "./Loading";
import { useNavigation } from "@react-navigation/native";
const ForYouTvSeeMore = ({ route }) => {
const [isLoading, setIsLoading] = useState(true);
const [currentPage, setcurrentPage] = useState("1");
const [page, setPage] = useState(1);
function detectSpecialCharacters(str) {
const regex = /[.,+\-\s]/g; // Regular expression to match commas, dots, plus signs, minus signs, and whitespace
return regex.test(str);
}
const navigation = useNavigation();
const [results, setResults] = useState([]);
const GoToPage = () => {
if (detectSpecialCharacters(currentPage) || parseInt(currentPage) < 1) {
Alert.alert("Invalid", "Invalid page number");
return;
} else if (parseInt(currentPage) > maxPage) {
Alert.alert("Invalid", `Page number too high (<${maxPage})`);
return;
} else if (parseInt(currentPage) === page) {
Alert.alert("Notice", "You are at this page number");
return;
} else {
setPage(parseInt(currentPage));
}
};
const [maxPage, setMaxPage] = useState(0);
const GoNext = () => {
setPage((prev) => prev + 1);
};
const GoBack = () => {
setPage((prev) => prev - 1);
};
const { list, type, isVietnamese, orJoinType, actorId, isActor } =
route.params;
const fetchTvBasedOnActor = () => {
axios
.get(
`https://api.themoviedb.org/3/person/${actorId}/tv_credits?api_key=841da308423b4b64ea4d57d052583683`
)
.then((res) => {
setResults(res.data.cast);
})
.catch((err) => {
console.error(err);
})
.finally(() => {
setIsLoading(false);
});
};
const fetchTvBasedOnGenre = () => {
const joinText = orJoinType ? list.join("|") : list.join(",");
const language = isVietnamese ? "vi" : "en";
axios
.get(
`https://api.themoviedb.org/3/discover/tv?api_key=841da308423b4b64ea4d57d052583683&include_adult=false&include_video=false&with_genres=${joinText}&with_original_language=${language}&page=${page}`
)
.then((res) => {
setResults(res.data.results);
setcurrentPage(page.toString());
setMaxPage(res.data.total_pages);
})
.catch((err) => {
console.error(err);
})
.finally(() => {
setIsLoading(false);
});
};
useEffect(() => {
if (type === "genre") {
setIsLoading(true);
fetchTvBasedOnGenre();
} else {
setIsLoading(true);
fetchTvBasedOnActor();
}
}, [page]);
return isLoading ? (
<Loading />
) : type === "genre" ? (
<>
<FlatList
style={{ backgroundColor: "#14b8a6" }}
data={results}
numColumns={2}
columnWrapperStyle={{
justifyContent: "space-evenly",
}}
renderItem={({ item }) => (
<TouchableOpacity
onPress={() => {
navigation.navigate("MainShowDetail", {
series_id: item.id,
header: isVietnamese ? item.original_name : item.name,
origin: "tvmain",
});
}}
className="w-[48%] bg-teal-500 mb-0.5 rounded-2xl items-center p-2 border-2 border-violet-800"
>
{item.poster_path ? (
<Image
source={{
uri: `https://image.tmdb.org/t/p/w185/${item.poster_path}`,
}}
className="w-full border-2 rounded-lg h-60 flex-2"
/>
) : (
<Image
source={require("./assets/blank.png")}
className="w-full border-2 rounded-lg h-60 flex-2"
/>
)}
<Text className="text-base font-semibold text-center text-[#0d253f]">
{isVietnamese ? item.original_name : item.name}
</Text>
<View className="flex flex-row items-center w-full justify-evenly">
{item.first_air_date ? (
<Text className="font-medium text-violet-800">
Year: {item.first_air_date.split("-")[0]}
</Text>
) : null}
{item.vote_average ? (
<Badge
value={item.vote_average}
status={
item.vote_average > 8
? "success"
: item.vote_average > 4
? "warning"
: "error"
}
badgeStyle={{
height: 22,
}}
textStyle={{
fontSize: 14,
fontWeight: "bold",
color: "black",
}}
/>
) : null}
</View>
</TouchableOpacity>
)}
keyExtractor={(item) => item.id}
/>
<View className="flex flex-row items-center justify-between px-4 py-2 bg-teal-500">
{page === 1 ? (
<View></View>
) : (
// <Button
// onPress={() => GoBack()}
// title="<<"
// color="#841584"
// accessibilityLabel="Go back previous page"
// />
<TouchableOpacity
className="items-center justify-center w-12 rounded-md h-9 bg-violet-800"
onPress={() => GoBack()}
>
<Text className="font-extrabold text-center text-teal-500">
{"<<"}
</Text>
</TouchableOpacity>
)}
<View className="flex flex-row items-center justify-between">
<Text className="font-semibold text-blue-900">Page: </Text>
<TextInput
keyboardType="numeric"
onChangeText={setcurrentPage}
value={currentPage}
className="w-12 font-semibold text-center text-blue-900"
/>
{/* <Button
onPress={() => GoToPage()}
title="Go"
color="#841584"
accessibilityLabel="Go to specified page"
/> */}
<TouchableOpacity
className="items-center justify-center w-10 rounded-md h-9 bg-violet-800"
onPress={() => GoToPage()}
>
<Text className="font-extrabold text-center text-teal-500">Go</Text>
</TouchableOpacity>
</View>
{page === maxPage ? (
<View></View>
) : (
// <Button
// onPress={() => GoNext()}
// title=">>"
// color="#841584"
// accessibilityLabel="Go to next page"
// />
<TouchableOpacity
className="items-center justify-center w-12 rounded-md h-9 bg-violet-800"
onPress={() => GoNext()}
>
<Text className="font-extrabold text-center text-teal-500">
{">>"}
</Text>
</TouchableOpacity>
)}
</View>
</>
) : (
<>
<FlatList
style={{ backgroundColor: "#14b8a6" }}
data={results}
numColumns={2}
columnWrapperStyle={{
justifyContent: "space-evenly",
}}
renderItem={({ item }) => (
<TouchableOpacity
onPress={() => {
navigation.navigate("MainShowDetail", {
series_id: item.id,
header: isVietnamese ? item.original_name : item.name,
origin: "tvmain",
});
}}
className="w-[48%] bg-teal-500 mb-0.5 rounded-2xl items-center p-2 border-2 border-violet-800"
>
{item.poster_path ? (
<Image
source={{
uri: `https://image.tmdb.org/t/p/w185/${item.poster_path}`,
}}
className="w-full border-2 rounded-lg h-60 flex-2"
/>
) : (
<Image
source={require("./assets/blank.png")}
className="w-full border-2 rounded-lg h-60 flex-2"
/>
)}
<Text className="text-base font-semibold text-center text-[#0d253f]">
{isVietnamese ? item.original_name : item.name}
</Text>
<View className="flex flex-row items-center w-full justify-evenly">
{item.first_air_date ? (
<Text className="font-medium text-violet-800">
Year: {item.first_air_date.split("-")[0]}
</Text>
) : null}
{item.vote_average ? (
<Badge
value={item.vote_average}
status={
item.vote_average > 8
? "success"
: item.vote_average > 4
? "warning"
: "error"
}
badgeStyle={{
height: 22,
}}
textStyle={{
fontSize: 14,
fontWeight: "bold",
color: "black",
}}
/>
) : null}
</View>
</TouchableOpacity>
)}
keyExtractor={(item) => item.id}
/>
{/* <View className="flex flex-row items-center justify-between px-4 py-2">
{page === 1 ? (
<View></View>
) : (
<Button
onPress={() => GoBack()}
title="<<"
color="#841584"
accessibilityLabel="Go back previous page"
/>
)}
<View className="flex flex-row items-center justify-between">
<Text>Page: </Text>
<TextInput
keyboardType="numeric"
onChangeText={setcurrentPage}
value={currentPage}
className="w-12 text-center"
/>
<Button
onPress={() => GoToPage()}
title="Go"
color="#841584"
accessibilityLabel="Go to specified page"
/>
</View>
{page === maxPage ? (
<View></View>
) : (
<Button
onPress={() => GoNext()}
title=">>"
color="#841584"
accessibilityLabel="Go to next page"
/>
)}
</View> */}
</>
);
};
export default ForYouTvSeeMore;