skolplattformen-backup/packages/app/components/navigation.component.tsx

126 lines
3.5 KiB
TypeScript
Raw Normal View History

2021-03-26 08:38:15 +00:00
import { NavigationContainer } from '@react-navigation/native'
import { useApi } from '@skolplattformen/api-hooks'
2021-03-26 08:38:15 +00:00
import {
Child as ChildType,
NewsItem as NewsItemType,
} from '@skolplattformen/embedded-api'
import { useTheme } from '@ui-kitten/components'
import React, { useEffect } from 'react'
import { StatusBar, useColorScheme } from 'react-native'
import { createNativeStackNavigator } from 'react-native-screens/native-stack'
import { schema } from '../app.json'
import {
darkNavigationTheme,
lightNavigationTheme,
} from '../design/navigationThemes'
import { useAppState } from '../hooks/useAppState'
import Absence, { absenceRouteOptions } from './absence.component'
import { Auth, authRouteOptions } from './auth.component'
import { Child, childRouteOptions } from './child.component'
import { childenRouteOptions, Children } from './children.component'
import { NewsItem, newsItemRouteOptions } from './newsItem.component'
import { SetLanguage, setLanguageRouteOptions } from './setLanguage.component'
2021-03-26 08:38:15 +00:00
export type RootStackParamList = {
Login: undefined
Children: undefined
Child: {
child: ChildType
color: string
initialRouteName?: string
}
NewsItem: { newsItem: NewsItemType; child: ChildType }
Absence: { child: ChildType }
SetLanguage: undefined
2021-03-26 08:38:15 +00:00
}
const { Navigator, Screen } = createNativeStackNavigator<RootStackParamList>()
2021-03-26 08:38:15 +00:00
const linking = {
prefixes: [schema],
config: {
screens: {
Login: 'login',
},
},
}
2021-03-26 08:38:15 +00:00
export const AppNavigator = () => {
const { isLoggedIn, api } = useApi()
const colorScheme = useColorScheme()
const colors = useTheme()
const currentAppState = useAppState()
useEffect(() => {
const checkUser = async () => {
if (currentAppState === 'active' && isLoggedIn) {
const { isAuthenticated } = await api.getUser()
if (!isAuthenticated) {
await api.logout()
}
}
}
checkUser()
}, [currentAppState, isLoggedIn, api])
2021-03-26 08:38:15 +00:00
return (
<NavigationContainer
linking={linking}
theme={
colorScheme === 'dark' ? darkNavigationTheme : lightNavigationTheme
}
>
2021-03-26 08:38:15 +00:00
<StatusBar />
<Navigator
screenOptions={() => ({
2021-09-01 08:57:27 +00:00
headerLargeTitle: false,
headerLargeTitleHideShadow: true,
headerStyle: {
backgroundColor: colors['background-basic-color-2'],
},
headerLargeTitleStyle: {
fontFamily: 'Poppins-ExtraBold',
},
})}
>
{isLoggedIn ? (
<>
<Screen
name="Children"
component={Children}
2021-09-12 20:29:26 +00:00
options={childenRouteOptions(colorScheme === 'dark')}
/>
<Screen
name="Child"
component={Child}
2021-09-12 20:29:26 +00:00
options={childRouteOptions(colorScheme === 'dark')}
/>
<Screen
name="NewsItem"
component={NewsItem}
2021-09-12 20:29:26 +00:00
options={newsItemRouteOptions(colorScheme === 'dark')}
/>
<Screen
name="Absence"
component={Absence}
2021-09-12 20:29:26 +00:00
options={absenceRouteOptions(colorScheme === 'dark')}
/>
</>
) : (
2021-04-14 14:13:43 +00:00
<>
<Screen name="Login" component={Auth} options={authRouteOptions} />
<Screen
name="SetLanguage"
component={SetLanguage}
options={setLanguageRouteOptions}
/>
2021-04-14 14:13:43 +00:00
</>
)}
</Navigator>
2021-03-26 08:38:15 +00:00
</NavigationContainer>
)
}