|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +/** |
| 4 | + * Copied with modification from src/frontend/components/Cart/CartDetail.tsx |
| 5 | + */ |
| 6 | +import { router } from "expo-router"; |
| 7 | +import { ThemedView } from "@/components/ThemedView"; |
| 8 | +import { ThemedText } from "@/components/ThemedText"; |
| 9 | +import { Pressable, StyleSheet } from "react-native"; |
| 10 | +import { useCart } from "@/providers/Cart.provider"; |
| 11 | +import CheckoutForm from "@/components/CheckoutForm"; |
| 12 | +import EmptyCart from "@/components/EmptyCart"; |
| 13 | +import { ThemedScrollView } from "@/components/ThemedScrollView"; |
| 14 | +import { useCallback, useMemo } from "react"; |
| 15 | +import { IFormData } from "@/components/CheckoutForm/CheckoutForm"; |
| 16 | +import Toast from "react-native-toast-message"; |
| 17 | +import SessionGateway from "@/gateways/Session.gateway"; |
| 18 | +import { useThemeColor } from "@/hooks/useThemeColor"; |
| 19 | + |
| 20 | +export default function Cart() { |
| 21 | + const tint = useThemeColor({}, "tint"); |
| 22 | + const styles = useMemo(() => getStyles(tint), [tint]); |
| 23 | + const { |
| 24 | + cart: { items }, |
| 25 | + emptyCart, |
| 26 | + placeOrder, |
| 27 | + } = useCart(); |
| 28 | + |
| 29 | + const onEmptyCart = useCallback(() => { |
| 30 | + emptyCart(); |
| 31 | + Toast.show({ |
| 32 | + type: "success", |
| 33 | + position: "bottom", |
| 34 | + text1: "Your cart was emptied", |
| 35 | + }); |
| 36 | + }, [emptyCart]); |
| 37 | + |
| 38 | + const onPlaceOrder = useCallback( |
| 39 | + async ({ |
| 40 | + email, |
| 41 | + state, |
| 42 | + streetAddress, |
| 43 | + country, |
| 44 | + city, |
| 45 | + zipCode, |
| 46 | + creditCardCvv, |
| 47 | + creditCardExpirationMonth, |
| 48 | + creditCardExpirationYear, |
| 49 | + creditCardNumber, |
| 50 | + }: IFormData) => { |
| 51 | + const { userId } = await SessionGateway.getSession(); |
| 52 | + await placeOrder({ |
| 53 | + userId, |
| 54 | + email, |
| 55 | + address: { |
| 56 | + streetAddress, |
| 57 | + state, |
| 58 | + country, |
| 59 | + city, |
| 60 | + zipCode, |
| 61 | + }, |
| 62 | + // TODO simplify react native demo for now by hard-coding the selected currency |
| 63 | + userCurrency: "USD", |
| 64 | + creditCard: { |
| 65 | + creditCardCvv, |
| 66 | + creditCardExpirationMonth, |
| 67 | + creditCardExpirationYear, |
| 68 | + creditCardNumber, |
| 69 | + }, |
| 70 | + }); |
| 71 | + |
| 72 | + Toast.show({ |
| 73 | + type: "success", |
| 74 | + position: "bottom", |
| 75 | + text1: "Your order is Complete!", |
| 76 | + text2: "We've sent you a confirmation email.", |
| 77 | + }); |
| 78 | + |
| 79 | + router.replace("/"); |
| 80 | + }, |
| 81 | + [placeOrder], |
| 82 | + ); |
| 83 | + |
| 84 | + if (!items.length) { |
| 85 | + return <EmptyCart />; |
| 86 | + } |
| 87 | + |
| 88 | + return ( |
| 89 | + <ThemedView style={styles.container}> |
| 90 | + <ThemedView> |
| 91 | + <ThemedScrollView> |
| 92 | + {items.map((item) => ( |
| 93 | + <ThemedView key={item.productId} style={styles.cartItem}> |
| 94 | + <ThemedText>{item.product.name}</ThemedText> |
| 95 | + <ThemedText style={styles.bold}>{item.quantity}</ThemedText> |
| 96 | + </ThemedView> |
| 97 | + ))} |
| 98 | + </ThemedScrollView> |
| 99 | + </ThemedView> |
| 100 | + <ThemedView style={styles.emptyCartContainer}> |
| 101 | + <Pressable style={styles.emptyCart} onPress={onEmptyCart}> |
| 102 | + <ThemedText style={styles.emptyCartText}>Empty Cart</ThemedText> |
| 103 | + </Pressable> |
| 104 | + </ThemedView> |
| 105 | + <CheckoutForm onSubmit={onPlaceOrder} /> |
| 106 | + </ThemedView> |
| 107 | + ); |
| 108 | +} |
| 109 | + |
| 110 | +const getStyles = (tint: string) => |
| 111 | + StyleSheet.create({ |
| 112 | + container: { |
| 113 | + flex: 1, |
| 114 | + gap: 20, |
| 115 | + justifyContent: "flex-start", |
| 116 | + }, |
| 117 | + emptyCartContainer: { |
| 118 | + display: "flex", |
| 119 | + alignItems: "flex-end", |
| 120 | + }, |
| 121 | + emptyCart: { |
| 122 | + borderRadius: 4, |
| 123 | + backgroundColor: "green", |
| 124 | + alignItems: "center", |
| 125 | + width: 100, |
| 126 | + right: 20, |
| 127 | + position: "relative", |
| 128 | + }, |
| 129 | + emptyCartText: { |
| 130 | + color: "white", |
| 131 | + }, |
| 132 | + cartItem: { |
| 133 | + marginLeft: 20, |
| 134 | + marginRight: 20, |
| 135 | + display: "flex", |
| 136 | + flexDirection: "row", |
| 137 | + justifyContent: "space-between", |
| 138 | + borderStyle: "solid", |
| 139 | + borderBottomWidth: 1, |
| 140 | + borderColor: tint, |
| 141 | + }, |
| 142 | + bold: { |
| 143 | + fontWeight: "bold", |
| 144 | + }, |
| 145 | + }); |
0 commit comments