-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
99 lines (84 loc) · 2.82 KB
/
App.tsx
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
import SignUp from "./components/signup/SignUp";
import Home from "./components/Home";
import { createBottomTabNavigator } from "@react-navigation/bottom-tabs";
import { NavigationContainer } from "@react-navigation/native";
import Login from "./components/login/Login";
import { useState } from "react";
import "./global.css"
import { AxiosResponse } from 'axios';
import { Ionicons } from '@expo/vector-icons';
import { BottomTabNavigationOptions } from '@react-navigation/bottom-tabs';
type TabBarIconProps = {
focused: boolean;
color: string;
size: number;
};
type Route = {
name: string
}
const Tab = createBottomTabNavigator();
export default function App() {
const [signedIn, setSignedIn] = useState(false);
const [token, setToken] = useState("");
function getToken(tokenResponse: AxiosResponse) {
setToken(tokenResponse.data.access);
setSignedIn(true);
}
function signOut() {
setSignedIn(false);
//TODO: Invalidate token when possible on the backend.
}
function displayIcons() {
return ({ route }: { route: Route }): BottomTabNavigationOptions => ({
tabBarIcon: ({ focused, color, size }: TabBarIconProps) => {
const iconMap: Record<string, string> = {
"Heim": focused ? "home" : "home-outline",
"Skrá út": focused ? "log-out" : "log-out-outline",
"Skrá inn": focused ? "log-in" : "log-in-outline",
"Nýskrá": focused ? "person-add" : "person-add-outline",
};
//casting to Ionicons.glypMap as that seems to be the right type for valid icon names.
const iconName = iconMap[route.name] as keyof typeof Ionicons.glyphMap;
return <Ionicons name={iconName} size={size} color={color} />;
},
})
}
return (
<NavigationContainer>
{!signedIn ? (
<Tab.Navigator screenOptions={displayIcons()}>
<Tab.Screen
name="Heim"
children={() => <Home signedIn={signedIn} />}
/>
<Tab.Screen
name="Skrá inn"
children={() => <Login getToken={getToken} />}
/>
<Tab.Screen
name="Nýskrá"
children={() => <SignUp getToken={getToken} />}
/>
</Tab.Navigator>
) : (
<Tab.Navigator screenOptions={displayIcons()}>
<Tab.Screen
name="Heim"
children={() => <Home signedIn={signedIn} token={token} />}
/>
<Tab.Screen
name="Skrá út"
children={() => <Home signedIn={signedIn} token={token} />}
listeners={({ navigation }) => ({
tabPress: (e) => {
e.preventDefault();
signOut();
navigation.navigate("Heim");
},
})}
/>
</Tab.Navigator>
)}
</NavigationContainer>
);
}