-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCartScreen.jsx
More file actions
57 lines (48 loc) · 1.42 KB
/
CartScreen.jsx
File metadata and controls
57 lines (48 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// [START complete-tutorial.present-checkout]
// [START integrate.present-checkout]
import React, { useEffect } from 'react';
import { Button, View, StyleSheet } from 'react-native';
import { useShopifyCheckoutSheet } from '@shopify/checkout-sheet-kit';
function CartScreen({ checkoutUrl, onOrderComplete }) {
const shopifyCheckout = useShopifyCheckoutSheet();
useEffect(() => {
const completedSub = shopifyCheckout.addEventListener(
'completed',
(event) => {
console.log('Order completed:', event.orderDetails.id);
onOrderComplete(event);
}
);
const canceledSub = shopifyCheckout.addEventListener(
'canceled',
() => {
console.log('Checkout canceled');
}
);
const failedSub = shopifyCheckout.addEventListener(
'error',
(error) => {
console.error('Checkout failed:', error);
}
);
return () => {
completedSub.remove();
canceledSub.remove();
failedSub.remove();
};
}, [shopifyCheckout]);
const handleCheckout = () => {
shopifyCheckout.present(checkoutUrl);
};
return (
<View style={styles.container}>
<Button title="Checkout" onPress={handleCheckout} />
</View>
);
}
const styles = StyleSheet.create({
container: { flex: 1, justifyContent: 'center', padding: 16 },
});
export default CartScreen;
// [END integrate.present-checkout]
// [END complete-tutorial.present-checkout]