From 8943dcadcb96e846590a71fc4158fe6d345693a2 Mon Sep 17 00:00:00 2001 From: Arnaud Vergnet Date: Mon, 20 Apr 2020 23:12:58 +0200 Subject: [PATCH] Rewrote navigation to show back button when navigating away from home --- src/components/Sidebar/SideBarSection.js | 9 +- src/components/Sidebar/Sidebar.js | 10 +- src/navigation/DrawerNavigator.js | 672 +++++------------------ src/screens/Amicale/LoginScreen.js | 2 +- 4 files changed, 142 insertions(+), 551 deletions(-) diff --git a/src/components/Sidebar/SideBarSection.js b/src/components/Sidebar/SideBarSection.js index 1f73ef0..28c8296 100644 --- a/src/components/Sidebar/SideBarSection.js +++ b/src/components/Sidebar/SideBarSection.js @@ -5,6 +5,7 @@ import {FlatList} from "react-native"; import {Drawer, withTheme} from 'react-native-paper'; import {Linking} from "expo"; import AnimatedAccordion from "../Animations/AnimatedAccordion"; +import {StackActions} from '@react-navigation/native'; type Props = { navigation: Object, @@ -55,8 +56,14 @@ class SideBarSection extends React.PureComponent { Linking.openURL(item.link); else if (item.action !== undefined) item.action(); - else + else if (this.props.activeRoute === "main") this.props.navigation.navigate(item.route); + else { + this.props.navigation.dispatch( + StackActions.replace(item.route) + ); + this.props.navigation.closeDrawer(); + } } /** diff --git a/src/components/Sidebar/Sidebar.js b/src/components/Sidebar/Sidebar.js index 0d87ed6..b3260e0 100644 --- a/src/components/Sidebar/Sidebar.js +++ b/src/components/Sidebar/Sidebar.js @@ -188,11 +188,15 @@ class SideBar extends React.Component { } shouldComponentUpdate(nextProps: Props, nextState: State): boolean { - const nextNavigationState = nextProps.state; + const nextNavigationState = nextProps.state.routes[0].state; const nextRoute = nextNavigationState.routes[nextNavigationState.index].name; - const currentNavigationState = this.props.state; - const currentRoute = currentNavigationState.routes[currentNavigationState.index].name; + let currentRoute = "main"; + const currentNavigationState = this.props.state.routes[0].state; + if (currentNavigationState != null) { + currentRoute = currentNavigationState.routes[currentNavigationState.index].name; + } + this.activeRoute = nextRoute; return (nextState !== this.state) diff --git a/src/navigation/DrawerNavigator.js b/src/navigation/DrawerNavigator.js index 6d11f98..846369d 100644 --- a/src/navigation/DrawerNavigator.js +++ b/src/navigation/DrawerNavigator.js @@ -2,7 +2,6 @@ import * as React from 'react'; import {createDrawerNavigator, DrawerNavigationProp} from '@react-navigation/drawer'; -import TabNavigator from './MainTabNavigator'; import SettingsScreen from '../screens/Other/SettingsScreen'; import AboutScreen from '../screens/About/AboutScreen'; import AboutDependenciesScreen from '../screens/About/AboutDependenciesScreen'; @@ -21,13 +20,13 @@ import ClubDisplayScreen from "../screens/Amicale/Clubs/ClubDisplayScreen"; import ClubAboutScreen from "../screens/Amicale/Clubs/ClubAboutScreen"; import VoteScreen from "../screens/Amicale/VoteScreen"; import AmicaleContactScreen from "../screens/Amicale/AmicaleContactScreen"; -import MaterialHeaderButtons, {Item} from "../components/Overrides/CustomHeaderButton"; import {AmicaleWebsiteScreen} from "../screens/Websites/AmicaleWebsiteScreen"; import {TutorInsaWebsiteScreen} from "../screens/Websites/TutorInsaWebsiteScreen"; import {WiketudWebsiteScreen} from "../screens/Websites/WiketudWebsiteScreen"; import {ElusEtudiantsWebsiteScreen} from "../screens/Websites/ElusEtudiantsWebsiteScreen"; import {createCollapsibleStack} from "react-navigation-collapsible"; import {useTheme} from "react-native-paper"; +import TabNavigator from "./MainTabNavigator"; const defaultScreenOptions = { gestureEnabled: true, @@ -35,496 +34,137 @@ const defaultScreenOptions = { ...TransitionPresets.SlideFromRightIOS, }; -function getDrawerButton(navigation: DrawerNavigationProp) { - return ( - - - - ); +function createScreenCollapsibleStack (name: string, component: any, title: string, useNativeDriver?: boolean) { + const {colors} = useTheme(); + return createCollapsibleStack( + , + { + collapsedColor: 'transparent', + useNativeDriver: useNativeDriver != null ? useNativeDriver : true, // native driver does not work with webview + } + ) } -const AboutStack = createStackNavigator(); - -function AboutStackComponent() { - return ( - - { - const openDrawer = getDrawerButton.bind(this, navigation); - return { - title: i18n.t('screens.about'), - headerLeft: openDrawer - }; - }} - /> - - - - ); +function getWebsiteStack(name: string, component: any, title: string) { + return createScreenCollapsibleStack(name, component, title, false); } -const SettingsStack = createStackNavigator(); +const DrawerStack = createStackNavigator(); -function SettingsStackComponent() { - return ( - - { - const openDrawer = getDrawerButton.bind(this, navigation); - return { +function DrawerStackComponent(props) { + return ( + + + - - ); -} - -const SelfMenuStack = createStackNavigator(); - -function SelfMenuStackComponent() { - const {colors} = useTheme(); - return ( - - {createCollapsibleStack( - { - const openDrawer = getDrawerButton.bind(this, navigation); - return { - title: i18n.t('screens.menuSelf'), - headerLeft: openDrawer, - headerStyle: { - backgroundColor: colors.surface, - }, - }; }} - />, - { - collapsedColor: 'transparent', - useNativeDriver: true, - } - )} - - ); -} - -const AvailableRoomStack = createStackNavigator(); - -function AvailableRoomStackComponent() { - const {colors} = useTheme(); - return ( - - {createCollapsibleStack( - { - const openDrawer = getDrawerButton.bind(this, navigation); - return { - title: i18n.t('screens.availableRooms'), - headerLeft: openDrawer, - headerStyle: { - backgroundColor: colors.surface, - }, - }; + /> + , - { - collapsedColor: 'transparent', - useNativeDriver: false, // native driver does not work with webview - } - )} - - ); -} - -const BibStack = createStackNavigator(); - -function BibStackComponent() { - const {colors} = useTheme(); - return ( - - {createCollapsibleStack( - { - const openDrawer = getDrawerButton.bind(this, navigation); - return { - title: i18n.t('screens.bib'), - headerLeft: openDrawer, - headerStyle: { - backgroundColor: colors.surface, - }, - }; + /> + , - { - collapsedColor: 'transparent', - useNativeDriver: false, // native driver does not work with webview - } - )} - - ); -} - -const AmicaleWebsiteStack = createStackNavigator(); - -function AmicaleWebsiteStackComponent() { - const {colors} = useTheme(); - return ( - - {createCollapsibleStack( - { - const openDrawer = getDrawerButton.bind(this, navigation); - return { - title: "Amicale", - headerLeft: openDrawer, - headerStyle: { - backgroundColor: colors.surface, - }, - }; + /> + , - { - collapsedColor: 'transparent', - useNativeDriver: false, // native driver does not work with webview - } - )} - - ); -} - -const ElusEtudiantsStack = createStackNavigator(); - -function ElusEtudiantsStackComponent() { - const {colors} = useTheme(); - return ( - - {createCollapsibleStack( - { - const openDrawer = getDrawerButton.bind(this, navigation); - return { - title: "Élus Étudiants", - headerLeft: openDrawer, - headerStyle: { - backgroundColor: colors.surface, - }, - }; - }} - />, - { - collapsedColor: 'transparent', - useNativeDriver: false, // native driver does not work with webview - } - )} - - ); -} - -const WiketudStack = createStackNavigator(); - -function WiketudStackComponent() { - const {colors} = useTheme(); - return ( - - {createCollapsibleStack( - { - const openDrawer = getDrawerButton.bind(this, navigation); - return { - title: "Wiketud", - headerLeft: openDrawer, - headerStyle: { - backgroundColor: colors.surface, - }, - }; - }} - />, - { - collapsedColor: 'transparent', - useNativeDriver: false, // native driver does not work with webview - } - )} - - ); -} - -const TutorInsaStack = createStackNavigator(); - -function TutorInsaStackComponent() { - const {colors} = useTheme(); - return ( - - {createCollapsibleStack( - { - const openDrawer = getDrawerButton.bind(this, navigation); - return { - title: "Tutor'INSA", - headerLeft: openDrawer, - headerStyle: { - backgroundColor: colors.surface, - }, - }; - }} - />, - { - collapsedColor: 'transparent', - useNativeDriver: false, // native driver does not work with webview - } - )} - - ); -} - - -const TetrisStack = createStackNavigator(); - -function TetrisStackComponent() { - return ( - - { - const openDrawer = getDrawerButton.bind(this, navigation); - return { + /> + {createScreenCollapsibleStack("self-menu", SelfMenuScreen, i18n.t('screens.menuSelf'))} + {getWebsiteStack("available-rooms", AvailableRoomScreen, i18n.t('screens.availableRooms'))} + {getWebsiteStack("bib", BibScreen, i18n.t('screens.bib'))} + {getWebsiteStack("amicale-website", AmicaleWebsiteScreen, "Amicale")} + {getWebsiteStack("elus-etudiants", ElusEtudiantsWebsiteScreen, "Élus Étudiants")} + {getWebsiteStack("wiketud", WiketudWebsiteScreen, "Wiketud")} + {getWebsiteStack("tutorinsa", TutorInsaWebsiteScreen, "Tutor'INSA")} + - - ); -} - -const LoginStack = createStackNavigator(); - -function LoginStackComponent() { - return ( - - { - const openDrawer = getDrawerButton.bind(this, navigation); - return { - title: i18n.t('screens.login'), - headerLeft: openDrawer - }; - }} - /> - - ); -} - -const ProfileStack = createStackNavigator(); - -function ProfileStackComponent() { - return ( - - { - const openDrawer = getDrawerButton.bind(this, navigation); - return { - title: i18n.t('screens.profile'), - headerLeft: openDrawer - }; - }} - /> - - - ); -} - - -const VoteStack = createStackNavigator(); - -function VoteStackComponent() { - return ( - - { - const openDrawer = getDrawerButton.bind(this, navigation); - return { - title: i18n.t('screens.vote'), - headerLeft: openDrawer - }; - }} - /> - - ); -} - -const AmicaleContactStack = createStackNavigator(); - -function AmicaleContactStackComponent() { - return ( - - { - const openDrawer = getDrawerButton.bind(this, navigation); - return { - title: i18n.t('screens.amicaleAbout'), - headerLeft: openDrawer - }; - }} - /> - - ); -} - - -const ClubStack = createStackNavigator(); - -function ClubStackComponent() { - const {colors} = useTheme(); - return ( - - {createCollapsibleStack( - { - const openDrawer = getDrawerButton.bind(this, navigation); - return { - title: i18n.t('clubs.clubList'), - headerLeft: openDrawer, - headerStyle: { - backgroundColor: colors.surface, - }, - }; }} - />, - { - collapsedColor: 'transparent', - useNativeDriver: true, - } - )} - - - - ); + /> + + + {createScreenCollapsibleStack("club-list", ClubListScreen, i18n.t('clubs.clubList'))} + + + + + + ); } - const Drawer = createDrawerNavigator(); type Props = { @@ -534,92 +174,32 @@ type Props = { export default class DrawerNavigator extends React.Component { - createTabNavigator: () => React.Element; + createDrawerStackComponent: () => React.Node; constructor(props: Props) { super(props); - this.createTabNavigator = () => + const createTabNavigator = () => + this.createDrawerStackComponent = () => ; } getDrawerContent = (props: { navigation: DrawerNavigationProp, - state: {[key: string] : any} + state: { [key: string]: any } }) => render() { return ( - - - - - - - - - - - - - - - - ); diff --git a/src/screens/Amicale/LoginScreen.js b/src/screens/Amicale/LoginScreen.js index 1712ad8..77e046e 100644 --- a/src/screens/Amicale/LoginScreen.js +++ b/src/screens/Amicale/LoginScreen.js @@ -75,7 +75,7 @@ class LoginScreen extends React.Component { hideErrorDialog = () => this.setState({dialogVisible: false}); - handleSuccess = () => this.props.navigation.navigate(this.nextScreen); + handleSuccess = () => this.props.navigation.replace(this.nextScreen); onResetPasswordClick = () => this.props.navigation.navigate('amicale-website', { screen: 'amicale-website',