From ceb2bb541c3bfa60d5be36aefad497361dbc13bb Mon Sep 17 00:00:00 2001 From: Ian Lin Date: Wed, 12 Jun 2024 16:11:21 +0800 Subject: [PATCH 1/2] connect reader immediately after registering successfully. --- .../screens/RegisterInternetReaderScreen.tsx | 92 ++++++++++++++++++- 1 file changed, 90 insertions(+), 2 deletions(-) diff --git a/dev-app/src/screens/RegisterInternetReaderScreen.tsx b/dev-app/src/screens/RegisterInternetReaderScreen.tsx index fba2e102..72bdbea8 100644 --- a/dev-app/src/screens/RegisterInternetReaderScreen.tsx +++ b/dev-app/src/screens/RegisterInternetReaderScreen.tsx @@ -1,5 +1,9 @@ -import React, { useState, useContext } from 'react'; -import type { Location } from '@stripe/stripe-terminal-react-native'; +import React, { useState, useContext, useCallback } from 'react'; +import { + Reader, + useStripeTerminal, + type Location, +} from '@stripe/stripe-terminal-react-native'; import { useNavigation } from '@react-navigation/core'; import { @@ -9,11 +13,13 @@ import { Platform, StyleSheet, View, + Alert, } from 'react-native'; import { colors } from '../colors'; import { AppContext } from '../AppContext'; import List from '../components/List'; import ListItem from '../components/ListItem'; +import type { NavigationAction } from '@react-navigation/native'; type InputValuesType = { registration_code: string; @@ -25,11 +31,87 @@ export default function RegisterInternetReaderScreen() { const navigation = useNavigation(); const [selectedLocation, setSelectedLocation] = useState(); const [status, setStatus] = useState(''); + const [readerId, setReaderId] = useState(''); const [inputValues, setInputValues] = useState({ registration_code: '', label: '', }); + const { cancelDiscovering, discoverReaders, connectInternetReader } = + useStripeTerminal({ + onFinishDiscoveringReaders: (finishError) => { + if (finishError) { + console.error( + 'Discover readers error', + `${finishError.code}, ${finishError.message}` + ); + if (navigation.canGoBack()) { + navigation.goBack(); + } + } else { + console.log('onFinishDiscoveringReaders success'); + } + }, + onUpdateDiscoveredReaders(readers) { + readers.map((reader) => { + if (reader.id === readerId) { + handleConnectInternetReader(reader); + return; + } + }); + }, + }); + + const handleGoBack = useCallback( + async (action: NavigationAction) => { + await cancelDiscovering(); + if (navigation.canGoBack()) { + navigation.dispatch(action); + } + }, + [cancelDiscovering, navigation] + ); + + const handleDiscoverReaders = useCallback(async () => { + navigation.addListener('beforeRemove', (e) => { + e.preventDefault(); + handleGoBack(e.data.action); + }); + + // List of discovered readers will be available within useStripeTerminal hook + const { error: discoverReadersError } = await discoverReaders({ + discoveryMethod: 'internet', + simulated: false, + timeout: 0, + }); + + if (discoverReadersError) { + const { code, message } = discoverReadersError; + Alert.alert('Discover readers error: ', `${code}, ${message}`); + if (navigation.canGoBack()) { + navigation.goBack(); + } + } + }, [navigation, discoverReaders]); + + const handleConnectInternetReader = async (reader: Reader.Type) => { + const { reader: connectedReader, error } = await connectInternetReader({ + reader, + }); + + if (error) { + console.log('connectInternetReader error:', error); + Alert.alert('Connect reader error: ', `${error.code}, ${error.message}`); + if (navigation.canGoBack()) { + navigation.goBack(); + } + } else { + console.log('Reader connected successfully', connectedReader); + navigation.goBack(); + } + return { error }; + }; + const registerReader = async () => { setStatus('Registering...'); try { @@ -47,6 +129,12 @@ export default function RegisterInternetReaderScreen() { console.log(resp); setStatus('Registered'); + setReaderId(resp.id); + + setTimeout(() => { + setStatus('Connecting'); + handleDiscoverReaders(); + }, 500); } catch (error) { console.error(error); setStatus('Could not register reader.'); From 5fbd83fe6904cec3dc744f87ca87be975a826d52 Mon Sep 17 00:00:00 2001 From: Ian Lin Date: Wed, 12 Jun 2024 16:32:46 +0800 Subject: [PATCH 2/2] fix lint issue. --- dev-app/src/screens/RegisterInternetReaderScreen.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev-app/src/screens/RegisterInternetReaderScreen.tsx b/dev-app/src/screens/RegisterInternetReaderScreen.tsx index 72bdbea8..e219bd8d 100644 --- a/dev-app/src/screens/RegisterInternetReaderScreen.tsx +++ b/dev-app/src/screens/RegisterInternetReaderScreen.tsx @@ -92,7 +92,7 @@ export default function RegisterInternetReaderScreen() { navigation.goBack(); } } - }, [navigation, discoverReaders]); + }, [navigation, discoverReaders, handleGoBack]); const handleConnectInternetReader = async (reader: Reader.Type) => { const { reader: connectedReader, error } = await connectInternetReader({