Advertisement
Guest User

Untitled

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