-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSearchScreen.tsx
159 lines (148 loc) · 4.38 KB
/
SearchScreen.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
import React, {useEffect, useState} from 'react';
import {categoryService, searchService} from '../../api/services';
import {
Image,
Pressable,
SafeAreaView,
ScrollView,
Text,
View,
} from 'react-native';
import {NewsCardItem} from '../../services/models';
import NewsCard from '../../component/NewsCard';
import styles from './SearchScreen.style';
import Spinner from '../../component/Spinner';
import SearchBar from './component/SearchBar';
interface SearchScreenProps {}
interface cardItem {
item: NewsCardItem;
}
const SearchScreen: React.FC<SearchScreenProps> = () => {
const [isLoading, setIsLoading] = useState(false);
const [inputTerm, setInputTerm] = useState('');
const [newsData, setNewsData] = useState([]);
const searchInputTerm = () => {
if (inputTerm === '') return;
setIsLoading(true);
// console.log('inputTerm: ', inputTerm);
searchService(`${inputTerm}`)
.then(data => {
setNewsData(data);
setIsLoading(false);
// console.log('data: ', data);
})
.catch(error => {
console.log('SearchTerm error: ', error);
});
};
const searchByCategory = (searchCategory: string) => {
setIsLoading(true);
setInputTerm('');
// console.log('searchCategory: ', searchCategory, newsData.length);
categoryService(`${searchCategory}`)
.then(data => {
setNewsData(data);
setIsLoading(false);
// console.log('data: ', data);
})
.catch(error => {
console.log('SearchCategory error: ', error);
});
};
useEffect(() => {
// console.log('SearchScreen useEffect: ', newsData.length);
return () => {
console.log('SearchScreen Cleanup', newsData.length);
setNewsData([]);
setInputTerm('');
};
}, []);
const Categories = () => {
return (
<View>
<View style={styles.categoryContainer}>
<Pressable
onPress={() => {
searchByCategory('entertainment');
}}>
<View style={styles.category}>
<Image
resizeMode="cover"
style={styles.categoryImage}
source={require('../../assets/hd-movie.png')}
/>
<Text style={styles.categoryText}>{'Entertainment'}</Text>
</View>
</Pressable>
<Pressable
onPress={() => {
searchByCategory('science');
}}>
<View style={styles.category}>
<Image
resizeMode="cover"
style={styles.categoryImage}
source={require('../../assets/science.png')}
/>
<Text style={styles.categoryText}>{'Science'}</Text>
</View>
</Pressable>
</View>
<View style={styles.categoryContainer}>
<Pressable>
<View style={styles.category}>
<Image
resizeMode="cover"
style={styles.categoryImage}
source={require('../../assets/bookmark.png')}
/>
<Text style={styles.categoryText}>{'Saved'}</Text>
</View>
</Pressable>
<Pressable
onPress={() => {
searchByCategory('sports');
}}>
<View style={styles.category}>
<Image
resizeMode="cover"
style={styles.categoryImage}
source={require('../../assets/game.png')}
/>
<Text style={styles.categoryText}>{'Sports'}</Text>
</View>
</Pressable>
</View>
</View>
);
};
return (
<>
<SafeAreaView style={styles.backgroundStyle}>
<ScrollView>
<SearchBar
inputTerm={inputTerm}
setInputTerm={setInputTerm}
searchInputTerm={searchInputTerm}
/>
<Categories />
<View>
{isLoading ? (
<Spinner />
) : (
<>
{/* <Text>{newsData.length}</Text> */}
{newsData.map(
(item, index) =>
// show only 24 serach result out of 100
index < 25 && <NewsCard key={index} data={item} />,
)}
</>
)}
</View>
</ScrollView>
</SafeAreaView>
</>
);
};
export default SearchScreen;