diff --git a/src/scenes/Profile/AccountManagementModalConnect.js b/src/scenes/Profile/AccountManagementModalConnect.js index acea2a6..93f1dcd 100644 --- a/src/scenes/Profile/AccountManagementModalConnect.js +++ b/src/scenes/Profile/AccountManagementModalConnect.js @@ -1,6 +1,6 @@ import React, { useCallback, useState, useEffect } from "react"; -import { View } from "react-native"; +import { View, Alert } from "react-native"; import LittleLoader from "~/components/LittleLoader"; import { Button } from "react-native-paper"; import { MaterialCommunityIcons } from "@expo/vector-icons"; @@ -70,12 +70,20 @@ export default function AccountManagementModalConnect({ }; }, [isLoading, loginRequest]); - const connectUsingPhoneNumber = () => { + const connectUsingPhoneNumber = async () => { setIsLoading(true); - sendAuthSMS({ - smsType: "C", - body: "Se connecter sur Alerte-Secours:\nCode: [CODE]\n💙", // must don't exceed 160 chars including replaced [CODE] - }); + try { + await sendAuthSMS({ + smsType: "C", + body: "Se connecter sur Alerte-Secours:\nCode: [CODE]\n💙", // must don't exceed 160 chars including replaced [CODE] + }); + } catch (e) { + setIsLoading(false); + Alert.alert( + "Échec de l’ouverture des SMS", + "Impossible d’ouvrir l’application SMS. Réessayez.", + ); + } }; const smsDisclaimerModalStatePair = useState({ visible: false }); diff --git a/src/scenes/Profile/PhoneNumbers.js b/src/scenes/Profile/PhoneNumbers.js index a75eb03..3b9e032 100644 --- a/src/scenes/Profile/PhoneNumbers.js +++ b/src/scenes/Profile/PhoneNumbers.js @@ -1,6 +1,6 @@ import React, { useState, useEffect, useCallback } from "react"; -import { View } from "react-native"; +import { View, Alert } from "react-native"; import { MaterialCommunityIcons } from "@expo/vector-icons"; @@ -41,10 +41,18 @@ export default function PhoneNumbersView({ data, waitingSmsType }) { const registerPhoneNumber = useCallback(async () => { setIsLoading(true); - sendAuthSMS({ - smsType: "R", - body: "S'enregistrer sur Alerte-Secours:\nCode: [CODE]\n💙", // must don't exceed 160 chars including replaced [CODE] - }); + try { + await sendAuthSMS({ + smsType: "R", + body: "S'enregistrer sur Alerte-Secours:\nCode: [CODE]\n💙", // must don't exceed 160 chars including replaced [CODE] + }); + } catch (e) { + setIsLoading(false); + Alert.alert( + "Échec de l’ouverture des SMS", + "Impossible d’ouvrir l’application SMS. Réessayez.", + ); + } }, [sendAuthSMS, setIsLoading]); // Clear loading state after 3 minutes diff --git a/src/scenes/Profile/index.js b/src/scenes/Profile/index.js index 0f229ad..706dc49 100644 --- a/src/scenes/Profile/index.js +++ b/src/scenes/Profile/index.js @@ -1,6 +1,6 @@ import React, { useEffect } from "react"; -import { ScrollView, View } from "react-native"; +import { ScrollView, View, AppState } from "react-native"; import Loader from "~/components/Loader"; import { useSubscription } from "@apollo/client"; @@ -12,6 +12,7 @@ import { createLogger } from "~/lib/logger"; import { FEATURE_SCOPES } from "~/lib/logger/scopes"; import withConnectivity from "~/hoc/withConnectivity"; +import { useFocusEffect } from "@react-navigation/native"; import Form from "./Form"; @@ -37,6 +38,39 @@ export default withConnectivity(function Profile({ navigation, route }) { // eslint-disable-next-line react-hooks/exhaustive-deps }, [userId]); + useFocusEffect( + React.useCallback(() => { + restart(); + }, [restart]), + ); + + useEffect(() => { + const sub = AppState.addEventListener("change", (state) => { + if (state === "active") { + restart(); + } + }); + return () => { + sub?.remove?.(); + }; + }, [restart]); + + useEffect(() => { + if ( + route.params?.waitingSmsType && + data?.selectOneUser?.oneUserLoginRequest + ) { + navigation.setParams({ + waitingSmsType: undefined, + openAccountModal: true, + }); + } + }, [ + route.params?.waitingSmsType, + data?.selectOneUser?.oneUserLoginRequest, + navigation, + ]); + if (loading || !data?.selectOneUser) { return ; }