Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. import React, { useEffect, useState } from 'react';
  2. import { StyleSheet, Image, View, Text, TextInput, TouchableOpacity } from 'react-native';
  3. import MapView, { Marker, Callout } from 'react-native-maps';
  4. import { requestPermissionsAsync, getCurrentPositionAsync } from 'expo-location';
  5. import { MaterialIcons } from '@expo/vector-icons';
  6.  
  7. import api from '../services/api'
  8. import { connect, disconnect } from '../services/socket'
  9.  
  10. function Main({ navigation }) {
  11.  
  12. const [devs, setDevs] = useState ([]);
  13. const [currentRegion, setCurrentRegion] = useState(null);
  14. const [techs, setTechs] = useState('');
  15.  
  16. useEffect(() => {
  17. async function loadInitialPosition () {
  18. const { granted } = await requestPermissionsAsync ();
  19.  
  20. if(granted) {
  21. const { coords } = await getCurrentPositionAsync({
  22. enableHighAccuracy: true,
  23. });
  24.  
  25. const { latitude, longitude } = coords;
  26.  
  27. setCurrentRegion({
  28. latitude,
  29. longitude,
  30. latitudeDelta: 0.04,
  31. longitudeDelta: 0.04,
  32. })
  33. }
  34. }
  35.  
  36. loadInitialPosition();
  37. }, []);
  38.  
  39. function setupWebsocket() {
  40. const { latitude, longitude } = currentRegion;
  41.  
  42. connect(
  43. latitude,
  44. longitude,
  45. techs,
  46. );
  47. }
  48.  
  49. async function loadDevs() {
  50. const { latitude, longitude } = currentRegion;
  51.  
  52. const response = await api.get('/search', {
  53. params: {
  54. latitude,
  55. longitude,
  56. techs
  57. }
  58. });
  59. setDevs(response.data);
  60. setupWebsocket();
  61. }
  62.  
  63. function handleRegionChanged(region) {
  64. setCurrentRegion(region);
  65. }
  66.  
  67. if(!currentRegion) {
  68. return null;
  69. }
  70.  
  71. return (
  72. <>
  73. <MapView
  74. onRegionChangeComplete={handleRegionChanged}
  75. initialRegion={currentRegion}
  76. style={{ flex: 1 }
  77. }
  78. >
  79. {devs.map(dev => (
  80. <Marker
  81. key={dev._id}
  82. coordinate={
  83. {
  84. latitude: dev.location.coordinates[1],
  85. longitude: dev.location.coordinates[0],
  86. }}
  87. >
  88. <Image
  89. style={styles.avatar}
  90. source={
  91. { uri: dev.avatar_url }}
  92. />
  93.  
  94. <Callout onPress={() => {
  95. navigation.navigate('Profile', { github_username: dev.github_username });
  96. }}>
  97. <View style={ styles.callout }>
  98. <Text style={styles.devName}>{dev.name}</Text>
  99. <Text style={styles.devBio}>{dev.bio}</Text>
  100. <Text style={styles.devTechs}>{dev.techs.join(', ')}</Text>
  101. </View>
  102. </Callout>
  103. </Marker>
  104. ))}
  105. </MapView>
  106.  
  107. <View style={styles.searchForm}>
  108. <TextInput
  109. style={styles.searchInput}
  110. placeholder="Buscar devs por tecnologias."
  111. placeholderTextColor="#999"
  112. autoCapitalize="words"
  113. autoCorrect={false}
  114. value={techs}
  115. onChangeText={setTechs}
  116. />
  117.  
  118. <TouchableOpacity onPress={loadDevs} style={styles.loadButton}>
  119. <MaterialIcons name="my-location" size={20} color="#FFF"/>
  120. </TouchableOpacity>
  121. </View>
  122. </>
  123. );
  124. }
  125.  
  126. const styles = StyleSheet.create({
  127. map: {
  128. flex: 1
  129. },
  130.  
  131. avatar: {
  132. width: 54,
  133. height: 54,
  134. borderRadius: 4,
  135. borderWidth: 4,
  136. borderColor: '#FFF'
  137. },
  138.  
  139. callout: {
  140. width: 260,
  141. },
  142.  
  143. devName: {
  144. fontWeight: 'bold',
  145. fontSize: 16,
  146. },
  147.  
  148. devBio: {
  149. color: '#666',
  150. marginTop: 5,
  151. },
  152.  
  153. devTechs: {
  154. marginTop: 5,
  155. },
  156.  
  157. searchForm: {
  158. position: 'absolute',
  159. top: 20,
  160. left: 20,
  161. right: 20,
  162. zIndex: 5,
  163. flexDirection: 'row',
  164. },
  165.  
  166. searchInput: {
  167. flex: 1,
  168. height: 50,
  169. backgroundColor: "#FFF",
  170. color: "#333",
  171. borderRadius: 25,
  172. paddingHorizontal: 20,
  173. fontSize: 16,
  174. shadowColor: "#000",
  175. shadowOpacity: 0.2,
  176. shadowOffset: {
  177. width: 4,
  178. height: 4,
  179. },
  180. elevation: 2,
  181. },
  182.  
  183. loadButton: {
  184. width: 50,
  185. height: 50,
  186. backgroundColor: "#8E4Dff",
  187. borderRadius: 25,
  188. justifyContent: 'center',
  189. alignItems: 'center',
  190. marginLeft: 15,
  191. },
  192. })
  193.  
  194. export default Main;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement