Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useEffect, useState } from 'react';
  2. import { useDispatch, useSelector } from 'react-redux';
  3. import Geolocation from '@react-native-community/geolocation';
  4. import * as Apollo from 'apollo-boost';
  5. import { useQuery } from '@apollo/react-hooks';
  6. import { withNavigationFocus } from 'react-navigation';
  7.  
  8. import { Text, ActivityIndicator, FlatList } from 'react-native';
  9.  
  10. import { Container, ContainerUser } from './styles'
  11.  
  12. import Icon from 'react-native-vector-icons/MaterialIcons';
  13.  
  14. import { getCityRequest } from '../../store/modules/user/actions';
  15. import {
  16.   getDevsByCitySuccess,
  17.   setActiveDevRequest,
  18. } from '../../store/modules/devs/actions';
  19.  
  20. import Header from '../../components/Header';
  21. import UserList from '../../components/UserList';
  22. import DevDescriptions from '../DevDescriptions';
  23. import Card from '../../components/Card';
  24.  
  25.  
  26.  
  27. function Dashboard({ navigation }) {
  28.   const dispatch = useDispatch();
  29.  
  30.   const [modalVisible, setModalVisible] = useState('');
  31.  
  32.   const [devs, setDevs] = useState([]);
  33.   const [existDevInCity, setExistDevInCity] = useState(true);
  34.  
  35.   const city = useSelector(state => state.user.address);
  36.  
  37.   const get_users = Apollo.gql(`
  38.   query gitUsers($location: String!, $first: Int!,  $cursor: String){
  39.     search(query: $location, type: USER, first: $first, after: $cursor) {
  40.       edges {
  41.         cursor
  42.         node {  
  43.           ... on User {
  44.               id
  45.               avatarUrl
  46.               name
  47.               login
  48.               email  
  49.               followers{totalCount}                    
  50.             }          
  51.       }
  52.       }
  53.       pageInfo {
  54.         startCursor
  55.         endCursor
  56.     }
  57.     }
  58.   }
  59.   `);
  60.  
  61.   const loc = 'location:'.concat(city);
  62.  
  63.   //const loc = "location:brasil";
  64.   const { loading, error, data, fetchMore } = useQuery(get_users, {
  65.     variables: { location: loc, first: 20 },
  66.   });
  67.  
  68.   useEffect(() => {
  69.     dispatch(getDevsByCitySuccess(devs));
  70.   }, [devs, dispatch]);
  71.  
  72.   useEffect(() => {
  73.     Geolocation.getCurrentPosition(position => {
  74.       dispatch(
  75.         getCityRequest(position.coords.latitude, position.coords.longitude),
  76.       );
  77.     });
  78.     // eslint-disable-next-line react-hooks/exhaustive-deps
  79.   }, []);
  80.  
  81.   if (error) {
  82.     return <Text>Error!!</Text>;
  83.   }
  84.  
  85.   if (data) {
  86.     if (devs.length === 0 && existDevInCity) {
  87.       setDevs(data);
  88.       setExistDevInCity(false);
  89.     }
  90.   }
  91.  
  92.   function closeModal() {
  93.     setModalVisible(false);
  94.   }
  95.  
  96.   function openModal(dev) {
  97.     dispatch(setActiveDevRequest(dev));
  98.     setModalVisible(true);
  99.   }
  100.  
  101.   return (
  102.     <>
  103.       <Container>
  104.         <Header navigation={navigation} />
  105.       </Container>
  106.       {loading ? (
  107.         <ActivityIndicator size="large" color="#EE2D58" />
  108.       ) : (
  109.           <>
  110.             {!existDevInCity ? (
  111.               <ContainerUser>
  112.                 <FlatList
  113.                   data={data.search.edges}
  114.                   keyExtractor={item => item.node.id}
  115.                   refreshing={data.networkStatus === 4}
  116.                   onEndReachedThreshold={0.5}
  117.                   onEndReached={() => {
  118.                     console.log('end');
  119.  
  120.                     console.log("FetchMore --> " + fetchMore);
  121.                     fetchMore({
  122.                       variables: { cursor: data.search.pageInfo.endCursor },
  123.                       updateQuery: (previousResult, { fetchMoreResult }) => {
  124.                         const newEdges = fetchMoreResult.search.edges;
  125.                         const pageInfo = fetchMoreResult.search.pageInfo;
  126.  
  127.                         console.log('Tamanho lista ->' + newEdges.length);
  128.                         return newEdges.length
  129.                           ?
  130.                           {
  131.                             search: {
  132.                               __typename: previousResult.search.__typename,
  133.                               edges: [...previousResult.search.edges, ...newEdges],
  134.                               pageInfo
  135.                             }
  136.                           }
  137.                           : previousResult;
  138.                       }
  139.                     })
  140.                   }
  141.                   }
  142.  
  143.                   renderItem={({ item }) => <Card item={item.node} openModal={openModal} />}
  144.  
  145.                 />
  146.               </ContainerUser>
  147.             ) : (
  148.                 <Text>Não encontramos nenhum desenvolvedor na cidade</Text>
  149.               )}
  150.           </>
  151.         )}
  152.       <DevDescriptions modalVisible={modalVisible} closeModal={closeModal} />
  153.     </>
  154.   );
  155. }
  156.  
  157. Dashboard.navigationOptions = {
  158.   tabBarLabel: 'Home',
  159.   tabBarIcon: ({ tintColor }) => <Icon name="home" size={20} color={tintColor} />,
  160. };
  161.  
  162. export default withNavigationFocus(Dashboard);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement