import React, { useEffect, useState} from 'react'; import { View, TouchableOpacity } from 'react-native'; import { Cart as CartController, useLanguage, useOrder, useUtils, } from 'ordering-components/native'; import { StyledBottomContent, StyledContainer, StyledContent, StyledTopBar, } from './styles'; import { OButton, OModal, OText, OIconButton } from '../shared'; import EvilIcons from 'react-native-vector-icons/EvilIcons'; import CartItem from '../CartItem'; import { Cart as TypeCart } from '../../types'; import { ProductForm } from '../ProductForm'; import { UpsellingProducts } from '../UpsellingProducts'; import { PORTRAIT, useDeviceOrientation } from '../../../../../src/hooks/DeviceOrientation'; import { useCartBottomSheet } from '../../providers/CartBottomSheetProvider'; import { useTheme } from 'styled-components/native'; const CartBottomSheetUI = (props: CartBottomSheetUIProps): React.ReactElement | null => { const { cart, clearCart, changeQuantity, getProductMax, offsetDisabled, removeProduct, setIsCartsLoading, isFromCart, navigation, clearInactivityTimeout, resetInactivityTimeout, } = props const theme = useTheme() const [, t] = useLanguage() const [orderState] = useOrder() const [{ parsePrice }] = useUtils() const [orientationState] = useDeviceOrientation(); const [openProduct, setModalIsOpen] = useState(false) const [curProduct, setCurProduct] = useState(null) const [openUpselling, setOpenUpselling] = useState(false) const [canOpenUpselling, setCanOpenUpselling] = useState(false) const [, {hideCartBottomSheet }] = useCartBottomSheet(); const selectedOrderType = orderState?.options?.type; const isCartPending = cart?.status === 2 const handleDeleteClick = (product: any) => { removeProduct(product, cart) if(cart?.products?.length === 1){ hideCartBottomSheet() } } const handleEditProduct = (product: any) => { if (props.onEditProduct) { props.onEditProduct(product) return } setCurProduct(product) setModalIsOpen(true) } const handlerProductAction = (product: any) => { if (Object.keys(product).length) { setModalIsOpen(false) } } const handleClearProducts = async () => { try { setIsCartsLoading && setIsCartsLoading(true) const result = await clearCart(cart?.uuid) setIsCartsLoading && setIsCartsLoading(false) hideCartBottomSheet() } catch (error) { setIsCartsLoading && setIsCartsLoading(false) } } const onCloseUpselling = () => { setOpenUpselling(false) setCanOpenUpselling(false) } const handleUpsellingPage = () => { clearInactivityTimeout() onCloseUpselling() navigation?.navigate('Cart', { businessId: cart?.business_id }) } if (!props?.visible) return null; return ( {cart?.products?.length > 0 && cart?.products.map((product: any) => ( ))} {t('CANCEL_ORDER', 'Cancel order')} = cart?.minimum || !cart?.minimum) && cart?.valid_address ? ( !openUpselling !== canOpenUpselling ? `${t('CHECKOUT', 'Checkout')} ${parsePrice(cart?.total)}`: t('LOADING', 'Loading') ) : !cart?.valid_address ? ( `${t('OUT_OF_COVERAGE', 'Out of Coverage')}` ) : ( `${t('MINIMUN_SUBTOTAL_ORDER', 'Minimum subtotal order:')} ${parsePrice(cart?.minimum)}` )} bgColor={(cart?.subtotal < cart?.minimum || !cart?.valid_address) ? theme.colors.secundary : theme.colors.primary} isDisabled={(openUpselling && !canOpenUpselling) || cart?.subtotal < cart?.minimum || !cart?.valid_address} borderColor={theme.colors.primary} imgRightSrc={null} textStyle={{ color: 'white', textAlign: 'center', flex: 1 }} onClick={() => { resetInactivityTimeout() setOpenUpselling(true) }} style={{width: '100%', flexDirection: 'row', justifyContent: 'center'}} /> setModalIsOpen(false)} > setModalIsOpen(false)} /> {openUpselling && ( )} ); } const TopBar = (props:any) => { const theme = useTheme() const [, t] = useLanguage() return ( {t('YOUR_ORDER', 'your order')} {props?.selectedOrderType === 2 && t('TAKE_OUT', 'Take out')} {props?.selectedOrderType === 3 && t('EAT_IN', 'Eat in')} } style={{ flex:1, justifyContent: 'flex-end', left: 30 }} onClick={props.hideCartBottomSheet} /> ); } interface CartBottomSheetUIProps { visible: boolean; height: number; cart: TypeCart, clearCart: any, changeQuantity: any, getProductMax: any, offsetDisabled: any, removeProduct: (product: any, cart: any) => void, setIsCartsLoading: any, isFromCart: any, navigation: any, onNavigationRedirect: any, clearInactivityTimeout: any, resetInactivityTimeout: any, onEditProduct: any, onAddProduct: any, } export const CartBottomSheet = (props: any) => { const cartProps = { ...props, UIComponent: CartBottomSheetUI } return ( ) }