socorro

mail@pastecode.io avatar
unknown
javascript
2 years ago
2.3 kB
13
Indexable
import { NavigationContainer } from '@react-navigation/native'
import { createStackNavigator } from '@react-navigation/stack'
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'
import { useState, useEffect } from 'react'

import axios from 'axios'

import Login from './src/screens/Auth/Login/'
import Recovery from './src/screens/Auth/Recovery/'
import SignUp from './src/screens/Auth/SignUp'
import HomeScreen from './src/screens/HomeScreen/'
import Settings from './src/screens/Settings/'
import Profile from './src/screens/Profile/'
import About from './src/screens/Profile/About/'
import History from './src/screens/Profile/History/'

const Stack = createStackNavigator()
const Tab = createBottomTabNavigator()

async function getUserData() {
  try {
    const response = await axios.get('https://my-json-server.typicode.com/higorpo/trilha-dev-json-server/profile');
    return response.data;
  } catch (error) {
    console.error(error);
  }
}

function HomeTabs(props) {
  return (
      <Tab.Navigator>
          <Tab.Screen name="HomeScreen" component={HomeScreen} />
          <Tab.Screen name="Profile" component={Profile} options={{headerShown: false}}/>
          <Tab.Screen name="Settings" component={Settings} userData={userData}/>
      </Tab.Navigator>
  )
}

export default function App(){
  const [userData, setUserData] = useState(null)
  const [updateData, setUpdateData] = useState(true)

  useEffect(() => {
    getUserData().then((data) => {
      setUserData(data)
      console.log('---')
      console.log(userData)
    })
    if(updateData && userData!==null){
      setUpdateData(false)
    }
  }, [updateData])
  
  return(
    
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen name="Login" component={Login} />
        <Stack.Screen name="SignUp" component={SignUp} />
        <Stack.Screen name="Recovery" component={Recovery} />
        <Stack.Screen name="HomeTabs" component={HomeTabs} options={{headerShown: false}} userData={userData}/>
        <Stack.Screen name="Profile" component={Profile} />
        <Stack.Screen name="About" component={About} />
        <Stack.Screen name="History" component={History} />
      </Stack.Navigator>
    </NavigationContainer>
  )
}