From c500ae05e6ce13f56a9c94fbc251f7531c912d77 Mon Sep 17 00:00:00 2001 From: Arnaud Vergnet Date: Sun, 23 May 2021 15:43:50 +0200 Subject: [PATCH] Redirect to login screen if not logged in --- .../Amicale/Profile/ProfileWelcomeCard.tsx | 2 +- src/context/preferencesContext.tsx | 4 +- src/navigation/MainNavigator.tsx | 112 ++++++++------- .../Other/Settings/DashboardEditScreen.tsx | 4 +- src/screens/Services/ServicesScreen.tsx | 128 ++++++++---------- src/utils/Services.ts | 22 ++- 6 files changed, 142 insertions(+), 130 deletions(-) diff --git a/src/components/Amicale/Profile/ProfileWelcomeCard.tsx b/src/components/Amicale/Profile/ProfileWelcomeCard.tsx index ca564ca..66e413b 100644 --- a/src/components/Amicale/Profile/ProfileWelcomeCard.tsx +++ b/src/components/Amicale/Profile/ProfileWelcomeCard.tsx @@ -51,7 +51,7 @@ function ProfileWelcomeCard(props: Props) { {i18n.t('screens.profile.welcomeDescription')} + + + + + + + + + ); +} + function getRegularScreens(createTabNavigator: () => React.ReactElement) { return ( <> @@ -233,55 +290,6 @@ function getRegularScreens(createTabNavigator: () => React.ReactElement) { title: i18n.t('screens.proximo.title'), }} /> - - - - - - - React.ReactElement) { function MainStackComponent(props: { showIntro: boolean; + isloggedIn: boolean; createTabNavigator: () => React.ReactElement; }) { - const { showIntro, createTabNavigator } = props; + const { showIntro, isloggedIn, createTabNavigator } = props; return ( {showIntro ? getIntroScreens() : getRegularScreens(createTabNavigator)} + {isloggedIn ? getAmicaleScreens() : null} ); } @@ -322,6 +332,7 @@ type PropsType = { function MainNavigator(props: PropsType) { const { preferences } = usePreferences(); + const isloggedIn = useLoginState(); const showIntro = getPreferenceBool( GeneralPreferenceKeys.showIntro, preferences @@ -330,6 +341,7 @@ function MainNavigator(props: PropsType) { return ( ); diff --git a/src/screens/Other/Settings/DashboardEditScreen.tsx b/src/screens/Other/Settings/DashboardEditScreen.tsx index d521c17..1a9e883 100644 --- a/src/screens/Other/Settings/DashboardEditScreen.tsx +++ b/src/screens/Other/Settings/DashboardEditScreen.tsx @@ -32,6 +32,7 @@ import { } from '../../../utils/Services'; import { useNavigation } from '@react-navigation/core'; import { useCurrentDashboard } from '../../../context/preferencesContext'; +import { useLoginState } from '../../../context/loginContext'; const styles = StyleSheet.create({ dashboardContainer: { @@ -63,6 +64,7 @@ const styles = StyleSheet.create({ */ function DashboardEditScreen() { const navigation = useNavigation(); + const isLoggedIn = useLoginState(); const { currentDashboard, @@ -150,7 +152,7 @@ function DashboardEditScreen() { return ( . */ -import * as React from 'react'; +import React, { useLayoutEffect } from 'react'; import { Image, StyleSheet, View } from 'react-native'; import { Avatar, @@ -25,10 +25,9 @@ import { Divider, List, TouchableRipple, - withTheme, + useTheme, } from 'react-native-paper'; import i18n from 'i18n-js'; -import { StackNavigationProp } from '@react-navigation/stack'; import CardList from '../../components/Lists/CardList/CardList'; import MaterialHeaderButtons, { Item, @@ -41,11 +40,8 @@ import { ServiceCategoryType, SERVICES_CATEGORIES_KEY, } from '../../utils/Services'; - -type PropsType = { - navigation: StackNavigationProp; - theme: ReactNativePaper.Theme; -}; +import { useNavigation } from '@react-navigation/native'; +import { useLoginState } from '../../context/loginContext'; const styles = StyleSheet.create({ container: { @@ -61,37 +57,29 @@ const styles = StyleSheet.create({ }, }); -class ServicesScreen extends React.Component { - finalDataset: Array; +function ServicesScreen() { + const navigation = useNavigation(); + const theme = useTheme(); + const isLoggedIn = useLoginState(); - constructor(props: PropsType) { - super(props); - this.finalDataset = getCategories(props.navigation.navigate, [ - SERVICES_CATEGORIES_KEY.SPECIAL, - ]); - } + const finalDataset = getCategories(navigation.navigate, isLoggedIn, [ + SERVICES_CATEGORIES_KEY.SPECIAL, + ]); - componentDidMount() { - const { props } = this; - props.navigation.setOptions({ - headerRight: this.getAboutButton, + useLayoutEffect(() => { + const getAboutButton = () => ( + + navigation.navigate('amicale-contact')} + /> + + ); + navigation.setOptions({ + headerRight: getAboutButton, }); - } - - getAboutButton = () => ( - - - - ); - - onAboutPress = () => { - const { props } = this; - props.navigation.navigate('amicale-contact'); - }; + }, [navigation]); /** * Gets the list title image for the list. @@ -102,8 +90,7 @@ class ServicesScreen extends React.Component { * @param source The source image to display. Can be a string for icons or a number for local images * @returns {*} */ - getListTitleImage(source: string | number) { - const { props } = this; + const getListTitleImage = (source: string | number) => { if (typeof source === 'number') { return ; } @@ -111,11 +98,11 @@ class ServicesScreen extends React.Component { ); - } + }; /** * A list item showing a list of available services for the current category @@ -123,20 +110,17 @@ class ServicesScreen extends React.Component { * @param item * @returns {*} */ - getRenderItem = ({ item }: { item: ServiceCategoryType }) => { - const { props } = this; + const getRenderItem = ({ item }: { item: ServiceCategoryType }) => { return ( { - props.navigation.navigate('services-section', { data: item }); - }} + onPress={() => navigation.navigate('services-section', { data: item })} > this.getListTitleImage(item.image)} + left={() => getListTitleImage(item.image)} right={() => } /> @@ -145,33 +129,31 @@ class ServicesScreen extends React.Component { ); }; - keyExtractor = (item: ServiceCategoryType): string => item.title; + const keyExtractor = (item: ServiceCategoryType): string => item.title; - render() { - return ( - - } - hasTab - /> - - - ); - } + return ( + + } + hasTab + /> + + + ); } -export default withTheme(ServicesScreen); +export default ServicesScreen; diff --git a/src/utils/Services.ts b/src/utils/Services.ts index 8f91a59..ee4d5e1 100644 --- a/src/utils/Services.ts +++ b/src/utils/Services.ts @@ -86,8 +86,21 @@ export type ServiceCategoryType = { content: Array; }; +function getAmicaleOnPress( + route: string, + onPress: (route: string, params?: { [key: string]: any }) => void, + isLoggedIn: boolean +) { + if (isLoggedIn) { + return () => onPress(route); + } else { + return () => onPress(MainRoutes.Login, { nextScreen: route }); + } +} + export function getAmicaleServices( onPress: (route: string, params?: { [key: string]: any }) => void, + isLoggedIn: boolean, excludedItems?: Array ): Array { const amicaleDataset = [ @@ -96,21 +109,21 @@ export function getAmicaleServices( title: i18n.t('screens.clubs.title'), subtitle: i18n.t('screens.services.descriptions.clubs'), image: Urls.images.clubs, - onPress: () => onPress(MainRoutes.ClubList), + onPress: getAmicaleOnPress(MainRoutes.ClubList, onPress, isLoggedIn), }, { key: SERVICES_KEY.PROFILE, title: i18n.t('screens.profile.title'), subtitle: i18n.t('screens.services.descriptions.profile'), image: Urls.images.profile, - onPress: () => onPress(MainRoutes.Profile), + onPress: getAmicaleOnPress(MainRoutes.Profile, onPress, isLoggedIn), }, { key: SERVICES_KEY.EQUIPMENT, title: i18n.t('screens.equipment.title'), subtitle: i18n.t('screens.services.descriptions.equipment'), image: Urls.images.equipment, - onPress: () => onPress(MainRoutes.EquipmentList), + onPress: getAmicaleOnPress(MainRoutes.EquipmentList, onPress, isLoggedIn), }, { key: SERVICES_KEY.AMICALE_WEBSITE, @@ -289,6 +302,7 @@ export function getSpecialServices( export function getCategories( onPress: (route: string, params?: { [key: string]: any }) => void, + isLoggedIn: boolean, excludedItems?: Array ): Array { const categoriesDataset = [ @@ -297,7 +311,7 @@ export function getCategories( title: i18n.t('screens.services.categories.amicale'), subtitle: i18n.t('screens.services.more'), image: AMICALE_LOGO, - content: getAmicaleServices(onPress), + content: getAmicaleServices(onPress, isLoggedIn), }, { key: SERVICES_CATEGORIES_KEY.STUDENTS,