From 60fe59ee2674e49ea70a5bd788698879f1ea22c6 Mon Sep 17 00:00:00 2001 From: Antonin Date: Tue, 8 Oct 2024 17:52:21 +0200 Subject: [PATCH] Fix/8 10 24 (#880) --- app/(app)/_layout.tsx | 29 +------- app/(app)/profil/_layout.tsx | 35 ++++------ app/(app)/profil/communications.tsx | 2 +- app/(app)/profil/cotisation-et-dons.tsx | 2 +- app/(app)/profil/mot-de-passe.tsx | 2 +- app/_layout.tsx | 2 + src/components/Buttons/AuthButton.tsx | 2 +- src/components/Header/Header.tsx | 3 +- .../ProfileCard/MyProfileCard.tsx | 2 +- src/components/events/EventFeedList.tsx | 3 +- src/components/layouts/ProfilLayout.tsx | 5 -- src/components/menu/Item.tsx | 4 +- src/ctx/SessionProvider.tsx | 69 +++++++++++++------ src/hooks/useInit.ts | 2 +- src/screens/actions/BottomSheetList.tsx | 1 - src/screens/profil/menu/Menu.tsx | 6 +- 16 files changed, 82 insertions(+), 87 deletions(-) diff --git a/app/(app)/_layout.tsx b/app/(app)/_layout.tsx index ba59ae1d3..7af07c7cc 100644 --- a/app/(app)/_layout.tsx +++ b/app/(app)/_layout.tsx @@ -1,37 +1,10 @@ import React from 'react' import { PortalLayout } from '@/components/layouts/PortalLayout' -import WaitingScreen from '@/components/WaitingScreen' -import { useSession } from '@/ctx/SessionProvider' -import useInit from '@/hooks/useInit' -import { parse, useURL } from 'expo-linking' -import { Stack, useGlobalSearchParams } from 'expo-router' +import { Stack } from 'expo-router' import { isWeb, useMedia, View } from 'tamagui' export default function AppLayout() { - const { signIn, isAuth, isLoading } = useSession() - - const { code, _switch_user } = useGlobalSearchParams<{ code?: string; _switch_user?: string }>() - const url = useURL() const media = useMedia() - - useInit() - - if (!isLoading && (code || url)) { - if (isWeb && code) { - signIn({ code, isAdmin: _switch_user === 'true' }) - return - } - if (url && !isWeb) { - const { queryParams } = parse(url) - const code = queryParams?.code as string | undefined - - if (code) { - signIn({ code }) - return - } - } - } - return ( diff --git a/app/(app)/profil/_layout.tsx b/app/(app)/profil/_layout.tsx index 65d73fe27..6731458ca 100644 --- a/app/(app)/profil/_layout.tsx +++ b/app/(app)/profil/_layout.tsx @@ -1,29 +1,16 @@ import { NamedExoticComponent } from 'react' -import { View } from 'react-native' import EuCampaignIllustration from '@/assets/illustrations/EuCampaignIllustration' -import BoundarySuspenseWrapper from '@/components/BoundarySuspenseWrapper' import { NavBar, ProfileView, SmallHeader, VoxHeader } from '@/components/Header/Header' import PageLayout from '@/components/layouts/PageLayout/PageLayout' -import SkeCard from '@/components/Skeleton/CardSkeleton' import { useSession } from '@/ctx/SessionProvider' import ProfilMenu from '@/screens/profil/menu/Menu' import { TabRouter } from '@react-navigation/native' import { NativeStackHeaderProps } from '@react-navigation/native-stack' import type { IconProps } from '@tamagui/helpers-icon' -import { ArrowLeft, CircleUser, HelpingHand, MessageCircle, Settings2, TreeDeciduous } from '@tamagui/lucide-icons' -import { Link, Navigator, Redirect, router, Slot, Stack, useNavigation, usePathname } from 'expo-router' -import { isWeb, useMedia, XStack, YStack } from 'tamagui' +import { ArrowLeft, CircleUser, HelpingHand, KeyRound, MessageCircle, Settings2, TreeDeciduous } from '@tamagui/lucide-icons' +import { Link, Navigator, Redirect, router, Slot, Stack } from 'expo-router' +import { useMedia, XStack } from 'tamagui' -const Skeleton = () => ( - - - - - - - - -) function CustomRouter() { return ( @@ -69,6 +56,7 @@ const CotisHeader = createProfilHeader(HelpingHand) const InfoHeader = createProfilHeader(Settings2) const ComHeader = createProfilHeader(MessageCircle) const ElusHeader = createProfilHeader(TreeDeciduous) +const MDPHeader = createProfilHeader(KeyRound) export default function AppLayout() { const { isAuth } = useSession() @@ -79,19 +67,19 @@ export default function AppLayout() { } return media.md ? ( - , animation: media.gtSm ? 'none' : 'slide_from_right' }}> + , + header: IndexHeader, }} /> , }} /> @@ -115,10 +103,17 @@ export default function AppLayout() { , }} /> + , + }} + /> ) : ( diff --git a/app/(app)/profil/communications.tsx b/app/(app)/profil/communications.tsx index a47dac94d..3c8bfefda 100644 --- a/app/(app)/profil/communications.tsx +++ b/app/(app)/profil/communications.tsx @@ -9,7 +9,7 @@ function CommunicationScreen() { return ( <> - {metatags.createTitle('Cotisation et dons')} + {metatags.createTitle('Cotisations et dons')} diff --git a/app/(app)/profil/cotisation-et-dons.tsx b/app/(app)/profil/cotisation-et-dons.tsx index aec0bd9a8..0be5796db 100644 --- a/app/(app)/profil/cotisation-et-dons.tsx +++ b/app/(app)/profil/cotisation-et-dons.tsx @@ -9,7 +9,7 @@ function PersonalInfoScreen() { return ( <> - {metatags.createTitle('Cotisation et dons')} + {metatags.createTitle('Cotisations et dons')} diff --git a/app/(app)/profil/mot-de-passe.tsx b/app/(app)/profil/mot-de-passe.tsx index 8d5d5a30b..5d38b0b87 100644 --- a/app/(app)/profil/mot-de-passe.tsx +++ b/app/(app)/profil/mot-de-passe.tsx @@ -8,7 +8,7 @@ function ChangePasswordScreen() { return ( <> - {metatags.createTitle('Cotisation et dons')} + {metatags.createTitle('Mot de passe')} diff --git a/app/_layout.tsx b/app/_layout.tsx index 947307d0a..491f92113 100644 --- a/app/_layout.tsx +++ b/app/_layout.tsx @@ -5,6 +5,7 @@ import WaitingScreen from '@/components/WaitingScreen' import { SessionProvider, useSession } from '@/ctx/SessionProvider' import useAppUpdate from '@/hooks/useAppUpdate' import useImportFont from '@/hooks/useImportFont' +import useInitPushNotification from '@/hooks/useInit' import UpdateScreen from '@/screens/update/updateScreen' import TamaguiProvider from '@/tamagui/provider' import { ErrorMonitor } from '@/utils/ErrorMonitor' @@ -34,6 +35,7 @@ const useRegisterRoutingInstrumentation = () => { } const WaitingRoomHoc = (props: { children: ViewProps['children']; isLoading?: boolean }) => { + useInitPushNotification() const { isLoading } = useSession() if (isLoading) { return diff --git a/src/components/Buttons/AuthButton.tsx b/src/components/Buttons/AuthButton.tsx index ba47e3f9e..19d19950b 100644 --- a/src/components/Buttons/AuthButton.tsx +++ b/src/components/Buttons/AuthButton.tsx @@ -5,7 +5,7 @@ import { useSession } from '@/ctx/SessionProvider' export const SignInButton = (props: Omit, 'children'>) => { const { signIn } = useSession() return ( - + signIn()} variant="text" size="md" {...props}> Me connecter ) diff --git a/src/components/Header/Header.tsx b/src/components/Header/Header.tsx index 4fdfe19de..336e12098 100644 --- a/src/components/Header/Header.tsx +++ b/src/components/Header/Header.tsx @@ -79,7 +79,8 @@ export const NavBar = () => { } export const ProfileView = () => { - const user = useGetProfil() + const { session } = useSession() + const user = useGetProfil({ enabled: !!session }) const profile = user?.data return ( diff --git a/src/components/ProfileCards/ProfileCard/MyProfileCard.tsx b/src/components/ProfileCards/ProfileCard/MyProfileCard.tsx index b11d785d5..443da6afb 100644 --- a/src/components/ProfileCards/ProfileCard/MyProfileCard.tsx +++ b/src/components/ProfileCards/ProfileCard/MyProfileCard.tsx @@ -7,9 +7,9 @@ import { useUserStore } from '@/store/user-store' import { openURL } from 'expo-linking' export default function MyProfileCard() { - const user = useGetProfil() const { session } = useSession() const { user: credentials } = useUserStore() + const user = useGetProfil({ enabled: !!session }) const profile = user?.data const onNavigateToCadre = useCallback( () => openURL(`${credentials?.isAdmin ? clientEnv.ADMIN_URL : clientEnv.OAUTH_BASE_URL}${profile?.cadre_auth_path}`), diff --git a/src/components/events/EventFeedList.tsx b/src/components/events/EventFeedList.tsx index 4903bf9a5..318cfc494 100644 --- a/src/components/events/EventFeedList.tsx +++ b/src/components/events/EventFeedList.tsx @@ -121,7 +121,8 @@ const EventListCard = memo((args: { item: RestItemEvent; cb: Parameters { const media = useMedia() - const user = useGetProfil() + const { session } = useSession() + const user = useGetProfil({ enabled: Boolean(session) }) const listRef = useRef(null) useScrollToTop(listRef) diff --git a/src/components/layouts/ProfilLayout.tsx b/src/components/layouts/ProfilLayout.tsx index f3fb03316..d9adc55d2 100644 --- a/src/components/layouts/ProfilLayout.tsx +++ b/src/components/layouts/ProfilLayout.tsx @@ -21,11 +21,6 @@ export default function ProfilLayout({ children }: { children: React.ReactNode } return media.md ? ( - - - - - & { children: string | string[]; icon: React.ExoticComponent }>( - ({ children, icon: Icon, ...props }) => { + ({ children, icon: Icon, ...props }, ref) => { return ( - + diff --git a/src/ctx/SessionProvider.tsx b/src/ctx/SessionProvider.tsx index c1e50afd6..755d7d9c1 100644 --- a/src/ctx/SessionProvider.tsx +++ b/src/ctx/SessionProvider.tsx @@ -5,7 +5,9 @@ import { useGetProfil, useGetUserScopes } from '@/services/profile/hook' import { User, useUserStore } from '@/store/user-store' import { ErrorMonitor } from '@/utils/ErrorMonitor' import { useToastController } from '@tamagui/toast' -import { router, useLocalSearchParams } from 'expo-router' +import { parse, useURL } from 'expo-linking' +import { router, useGlobalSearchParams, useLocalSearchParams } from 'expo-router' +import { isWeb } from 'tamagui' type AuthContext = { signIn: (props?: { code: string; isAdmin?: boolean }) => Promise @@ -44,6 +46,10 @@ export function useSession() { export function SessionProvider(props: React.PropsWithChildren) { const { user: session, setCredentials: setSession, _hasHydrated } = useUserStore() const { redirect: pRedirect } = useLocalSearchParams<{ redirect?: string }>() + const params = useGlobalSearchParams<{ code?: string; _switch_user?: string }>() + const [onShotParams, setOneShotParams] = React.useState(params) + + const url = useURL() const [isLoginInProgress, setIsLoginInProgress] = React.useState(false) const toast = useToastController() @@ -63,24 +69,47 @@ export function SessionProvider(props: React.PropsWithChildren) { const isGlobalLoading = [isLoginInProgress, user.isLoading, scope.isLoading, !_hasHydrated].some(Boolean) const isAuth = Boolean(session && !isGlobalLoading) - const handleSignIn: AuthContext['signIn'] = async (props) => { - try { - setIsLoginInProgress(true) - const session = await login(props?.code) - if (!session) { - return + const handleSignIn: AuthContext['signIn'] = React.useCallback( + async (props) => { + try { + if (isLoginInProgress) { + return + } + setIsLoginInProgress(true) + const session = await login(props?.code) + if (!session) { + return + } + const { accessToken, refreshToken } = session + setSession({ accessToken, refreshToken, isAdmin: props?.isAdmin }) + } catch (e) { + ErrorMonitor.log(e.message, { e }) + toast.show('Erreur lors de la connexion', { type: 'error' }) + } finally { + setIsLoginInProgress(false) + } + }, + [isLoginInProgress, login], + ) + + React.useEffect(() => { + const { code, _switch_user } = onShotParams + if (code || url) { + if (isWeb && code) { + setOneShotParams({}) + handleSignIn({ code, isAdmin: _switch_user === 'true' }) + } + if (url && !isWeb) { + const { queryParams } = parse(url) + const code = queryParams?.code as string | undefined + if (code) { + handleSignIn({ code }) + } } - const { accessToken, refreshToken } = session - setSession({ accessToken, refreshToken, isAdmin: props?.isAdmin }) - } catch (e) { - ErrorMonitor.log(e.message, { e }) - toast.show('Erreur lors de la connexion', { type: 'error' }) - } finally { - setIsLoginInProgress(false) } - } + }, []) - const handleRegister = async () => { + const handleRegister = React.useCallback(async () => { try { setIsLoginInProgress(true) const session = await register() @@ -96,11 +125,11 @@ export function SessionProvider(props: React.PropsWithChildren) { } finally { setIsLoginInProgress(false) } - } + }, [isLoginInProgress]) - const handleSignOut = async () => { + const handleSignOut = React.useCallback(async () => { await logout() - } + }, []) const providerValue = useMemo( () => @@ -114,7 +143,7 @@ export function SessionProvider(props: React.PropsWithChildren) { user, scope, }) satisfies AuthContext, - [handleSignIn, handleSignOut, session, isLoginInProgress], + [handleSignIn, handleSignOut, session, isLoginInProgress, isGlobalLoading], ) return {props.children} diff --git a/src/hooks/useInit.ts b/src/hooks/useInit.ts index a5f37948a..3ec1f4652 100644 --- a/src/hooks/useInit.ts +++ b/src/hooks/useInit.ts @@ -1,5 +1,5 @@ import { useEffect } from 'react' -import { ROUTES } from '@/config/routes' + import { useSession } from '@/ctx/SessionProvider' import PushRepository from '@/data/PushRepository' import { useMatomo } from '@/services/matomo/hook' diff --git a/src/screens/actions/BottomSheetList.tsx b/src/screens/actions/BottomSheetList.tsx index c9fa1d509..f52f8ef4a 100644 --- a/src/screens/actions/BottomSheetList.tsx +++ b/src/screens/actions/BottomSheetList.tsx @@ -3,7 +3,6 @@ import Text from '@/components/base/Text' import { ActionCard } from '@/components/Cards' import EmptyState from '@/components/EmptyStates/EmptyEvent/EmptyEvent' import SkeCard from '@/components/Skeleton/CardSkeleton' -import { useSession } from '@/ctx/SessionProvider' import { RestAction } from '@/services/actions/schema' import { useGetProfil } from '@/services/profile/hook' import { ScrollView, Sheet, XStack, YStack } from 'tamagui' diff --git a/src/screens/profil/menu/Menu.tsx b/src/screens/profil/menu/Menu.tsx index e5e937490..a2374f04c 100644 --- a/src/screens/profil/menu/Menu.tsx +++ b/src/screens/profil/menu/Menu.tsx @@ -15,17 +15,17 @@ export const menuData: Array & { pathname?: Hre // }, { icon: HelpingHand, - children: 'Cotisation et dons', + children: 'Cotisations et dons', pathname: '/profil/cotisation-et-dons', }, { icon: Settings2, - children: 'Information personnelles', + children: 'Informations personnelles', pathname: '/profil/informations-personnelles', }, { icon: TreeDeciduous, - children: "Information d'élu", + children: "Informations d'élu", pathname: '/profil/informations-elu', }, // {