Skip to content

Commit

Permalink
test connection token on init (#177)
Browse files Browse the repository at this point in the history
* chore: test connection token on init

* chore: render app conditionally
  • Loading branch information
arekkubaczkowski authored Mar 22, 2022
1 parent 22b458b commit 163e22e
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions example/src/Root.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import { ActivityIndicator, Alert, StyleSheet } from 'react-native';

import { StripeTerminalProvider } from 'stripe-terminal-react-native';
import App from './App';
import { API_URL } from './Config';

export default function Root() {
const [initialized, setInitialized] = useState(false);

const fetchTokenProvider = async () => {
const response = await fetch(`${API_URL}/connection_token`, {
method: 'POST',
Expand All @@ -17,12 +20,30 @@ export default function Root() {
return secret;
};

useEffect(() => {
// test connection_token endpoint since native SDK's doesn't fetch it on init
async function init() {
try {
await fetchTokenProvider();
setInitialized(true);
} catch (error) {
console.error(error);
Alert.alert("Couldn't fetch connection token!");
}
}
init();
}, []);

return (
<StripeTerminalProvider
logLevel="verbose"
tokenProvider={fetchTokenProvider}
>
<App />
{initialized ? (
<App />
) : (
<ActivityIndicator style={StyleSheet.absoluteFillObject} />
)}
</StripeTerminalProvider>
);
}

0 comments on commit 163e22e

Please sign in to comment.