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
|
import AsyncStorage from '@react-native-community/async-storage';
import React, {useEffect, useState} from 'react';
import {
Image,
Keyboard,
ScrollView,
StatusBar,
StyleSheet,
Text,
View,
} from 'react-native';
import Animated, {Easing, timing} from 'react-native-reanimated';
import {
RecentSearches,
SearchBackground,
SearchBar,
SearchHeader,
SearchResults,
SearchResultsBackground,
TabsGradient,
} from '../../components';
import {SEARCH_ENDPOINT} from '../../constants';
import {AuthContext} from '../../routes/authentication';
import {ProfilePreviewType, UserType} from '../../types';
import {SCREEN_HEIGHT, SCREEN_WIDTH, StatusBarHeight} from '../../utils';
const NO_USER: UserType = {
userId: '',
username: '',
};
/**
* Search Screen for user recommendations and a search
* tool to allow user to find other users
*/
const SearchScreen: React.FC = () => {
const {recentSearches} = React.useContext(AuthContext);
const [query, setQuery] = useState<string>('');
const [results, setResults] = useState<Array<ProfilePreviewType>>([]);
const [recents, setRecents] = useState<Array<ProfilePreviewType>>(
recentSearches,
);
const [searching, setSearching] = useState(false);
const top = Animated.useValue(-SCREEN_HEIGHT);
const [user, setUser] = useState<UserType>(NO_USER);
useEffect(() => {
if (query.length < 3) {
setResults([]);
return;
}
const loadResults = async (q: string) => {
try {
const token = await AsyncStorage.getItem('token');
if (!token) {
setUser(NO_USER);
return;
}
const response = await fetch(`${SEARCH_ENDPOINT}?query=${q}`, {
method: 'GET',
headers: {
Authorization: 'Token ' + token,
},
});
const status = response.status;
if (status === 200) {
let searchResults = await response.json();
setResults(searchResults);
return;
}
setResults([]);
} catch (error) {
console.log(error);
setResults([]);
}
};
loadResults(query);
}, [query]);
const handleFocus = () => {
const topInConfig = {
duration: 180,
toValue: 0,
easing: Easing.bezier(0.31, 0.14, 0.66, 0.82),
};
timing(top, topInConfig).start();
setSearching(true);
};
const handleBlur = () => {
Keyboard.dismiss();
const topOutConfig = {
duration: 180,
toValue: -SCREEN_HEIGHT,
easing: Easing.inOut(Easing.ease),
};
timing(top, topOutConfig).start();
setSearching(false);
};
const loadRecentlySearchedUsers = async () => {
try {
const asyncCache = await AsyncStorage.getItem('@recently_searched_users');
asyncCache != null ? setRecents(JSON.parse(asyncCache)) : setRecents([]);
} catch (e) {
console.log(e);
}
};
const clearRecentlySearched = async () => {
try {
await AsyncStorage.removeItem('@recently_searched_users');
loadRecentlySearchedUsers();
} catch (e) {
console.log(e);
}
};
const handleUpdate = async (val: string) => {
setQuery(val);
loadRecentlySearchedUsers();
};
return (
<SearchBackground>
<StatusBar />
<ScrollView
scrollEnabled={!searching}
keyboardShouldPersistTaps={'always'}
stickyHeaderIndices={[4]}
contentContainerStyle={styles.contentContainer}
showsVerticalScrollIndicator={false}>
<SearchHeader style={styles.header} {...{top}} />
<SearchBar
style={styles.searchBar}
onCancel={handleBlur}
onChangeText={handleUpdate}
onBlur={Keyboard.dismiss}
onFocus={handleFocus}
value={query}
{...{top, searching}}
/>
{/* Removed for Alpha for now */}
{/* <Explore /> */}
{/* <View>
<View style={styles.textContainer}>
<Text style={styles.headerText}>Coming Soon</Text>
<Text style={styles.subtext}>
We are working on constructing our explore suggestions. You can
still search users for now!
</Text>
</View>
<Image
source={require('../../assets/images/coming-soon.png')}
style={styles.image}
/>
</View> */}
<SearchResultsBackground {...{top}}>
{results.length === 0 && recents.length !== 0 ? (
<RecentSearches
sectionTitle="Recent"
sectionButtonTitle="Clear all"
onPress={clearRecentlySearched}
recents={recents}
/>
) : (
<SearchResults {...{results}} />
)}
</SearchResultsBackground>
</ScrollView>
<TabsGradient />
</SearchBackground>
);
};
const styles = StyleSheet.create({
contentContainer: {
paddingTop: StatusBarHeight,
paddingBottom: SCREEN_HEIGHT / 15,
},
searchBar: {
paddingHorizontal: '3%',
},
header: {
marginVertical: 20,
zIndex: 1,
},
recentsHeaderContainer: {
flexDirection: 'row',
},
recentsHeader: {
fontSize: 17,
fontWeight: 'bold',
flexGrow: 1,
},
clear: {
fontSize: 17,
fontWeight: 'bold',
color: '#698DD3',
},
image: {
width: SCREEN_WIDTH,
height: SCREEN_WIDTH,
},
textContainer: {
marginTop: '10%',
},
headerText: {
color: '#fff',
fontSize: 32,
fontWeight: '600',
textAlign: 'center',
marginBottom: '4%',
marginHorizontal: '10%',
},
subtext: {
color: '#fff',
fontSize: 16,
fontWeight: '600',
textAlign: 'center',
marginHorizontal: '10%',
},
});
export default SearchScreen;
|