import React, { useState } from 'react'; import { Cart as CartController, useOrder, useLanguage, useConfig, useUtils, useValidationFields, } from 'ordering-components/native'; import { useTheme } from 'styled-components/native'; import { ScrollView, View, useWindowDimensions, ActivityIndicator, StyleSheet } from 'react-native'; import { CheckoutAction, OSBill, OSTable, OSCoupon, OSTotal, Title, LineDivider, BIHeader, BIInfo, BIContentInfo, BITotal, TopHeader } from './styles'; import { ProductItemAccordion } from '../ProductItemAccordion'; import { CouponControl } from '../CouponControl'; import { OButton, OInput, OModal, OText, OIcon } from '../shared'; import { ProductForm } from '../ProductForm'; import { UpsellingProducts } from '../UpsellingProducts'; import { convertHoursToMinutes, verifyDecimals } from '../../utils'; import { Container } from '../../layouts/Container'; import { NotFoundSource } from '../NotFoundSource' import { OSRow } from '../OrderSummary/styles'; import AntIcon from 'react-native-vector-icons/AntDesign' import MaterialCommunityIcon from 'react-native-vector-icons/MaterialCommunityIcons' import { TaxInformation } from '../TaxInformation'; import { TouchableOpacity } from 'react-native'; const CartUI = (props: any) => { const { cart, clearCart, isCartList, changeQuantity, getProductMax, offsetDisabled, removeProduct, handleCartOpen, setIsCartsLoading, handleChangeComment, commentState } = props const theme = useTheme(); const { height } = useWindowDimensions() const [, t] = useLanguage() const [orderState] = useOrder() const [{ configs }] = useConfig(); const [{ parsePrice, parseNumber, parseDate }] = useUtils() const [validationFields] = useValidationFields() const [openProduct, setModalIsOpen] = useState(false) const [curProduct, setCurProduct] = useState(null) const [openUpselling, setOpenUpselling] = useState(false) const [canOpenUpselling, setCanOpenUpselling] = useState(false) const [isUpsellingProducts, setIsUpsellingProducts] = useState(false) const [openTaxModal, setOpenTaxModal] = useState({ open: false, data: null }) const isCartPending = cart?.status === 2 const isCouponEnabled = validationFields?.fields?.checkout?.coupon?.enabled const isClosed = !cart?.valid_schedule const isProducts = cart?.products?.length const handleDeleteClick = (product: any) => { removeProduct(product, cart) } const handleEditProduct = (product: any) => { 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) // } catch (error) { // setIsCartsLoading && setIsCartsLoading(false) // } // } const handleUpsellingPage = () => { setOpenUpselling(false) setCanOpenUpselling(false) props.onNavigationRedirect('CheckoutNavigator', { screen: 'CheckoutPage', cartUuid: cart?.uuid, businessLogo: cart?.business?.logo, businessName: cart?.business?.name, cartTotal: cart?.total }) } const getIncludedTaxes = () => { if (cart?.taxes === null) { return cart.business.tax_type === 1 ? cart?.tax : 0 } else { return cart?.taxes.reduce((taxIncluded: number, tax: any) => { return taxIncluded + (tax.type === 1 ? tax.summary?.tax : 0) }, 0) } } const CartHeader = () => { return ( {cart?.business?.logo && ( )} {cart?.business?.name} {orderState?.options?.type === 1 ? ( {convertHoursToMinutes(cart?.business?.delivery_time)} ) : ( {convertHoursToMinutes(cart?.business?.pickup_time)} )} {!isClosed && !!isProducts && cart?.valid_products && cart?.total > 0 && ( {t('CART_TOTAL', 'Total')} {parsePrice(cart?.total)} )} {isClosed && ( {t('CLOSED', 'Closed')} )} {!isClosed && !isProducts && ( {t('NO_PRODUCTS', 'No products')} )} ) } const styles = StyleSheet.create({ headerItem: { overflow: 'hidden', backgroundColor: theme.colors.clear, width: 35, marginVertical: 18, }, }) return ( cart?.products?.length > 0 ? ( {!isCartList ? ( <OText size={20} > {t('YOUR_CART', 'Your cart')} </OText> ) : ( )} {cart?.products?.length > 0 && ( {cart?.products?.map((product: any) => ( ))} {cart?.valid_products && ( {t('SUBTOTAL', 'Subtotal')} {parsePrice(cart?.subtotal + getIncludedTaxes())} {cart?.discount > 0 && cart?.total >= 0 && ( {cart?.discount_type === 1 ? ( {t('DISCOUNT', 'Discount')} {`(${verifyDecimals(cart?.discount_rate, parsePrice)}%)`} ) : ( {t('DISCOUNT', 'Discount')} )} - {parsePrice(cart?.discount || 0)} )} { cart.taxes?.length > 0 && cart.taxes.filter((tax: any) => tax.type === 2 && tax?.rate !== 0).map((tax: any) => ( {tax.name || t('INHERIT_FROM_BUSINESS', 'Inherit from business')}{' '} {`(${verifyDecimals(tax?.rate, parseNumber)}%)`}{' '} setOpenTaxModal({ open: true, data: tax })} > {parsePrice(tax?.summary?.tax || 0)} )) } { cart?.fees?.length > 0 && cart?.fees?.filter((fee: any) => !(fee.fixed === 0 && fee.percentage === 0)).map((fee: any) => ( {fee.name || t('INHERIT_FROM_BUSINESS', 'Inherit from business')}{' '} ({parsePrice(fee?.fixed)} + {fee?.percentage}%){' '} setOpenTaxModal({ open: true, data: fee })} > {parsePrice(fee?.summary?.fixed + fee?.summary?.percentage || 0)} )) } {orderState?.options?.type === 1 && cart?.delivery_price > 0 && ( {t('DELIVERY_FEE', 'Delivery Fee')} {parsePrice(cart?.delivery_price)} )} {cart?.driver_tip > 0 && ( {t('DRIVER_TIP', 'Driver tip')} {cart?.driver_tip_rate > 0 && parseInt(configs?.driver_tip_type?.value, 10) === 2 && !parseInt(configs?.driver_tip_use_custom?.value, 10) && ( `(${verifyDecimals(cart?.driver_tip_rate, parseNumber)}%)` )} {parsePrice(cart?.driver_tip)} )} {isCouponEnabled && !isCartPending && ( )} {cart?.status !== 2 && ( {t('COMMENTS', 'Comments')} handleChangeComment(value)} style={{ borderColor: theme.colors.border, borderRadius: 10, marginBottom: 20, height: 104, maxHeight: 104, alignItems: 'flex-start', }} multiline /> {commentState?.loading && ( )} )} {t('TOTAL', 'Total')} {cart?.total >= 1 && parsePrice(cart?.total)} )} )} {cart?.products?.length > 0 && ( <> { }} handleUpsellingProducts={setIsUpsellingProducts} /> {cart?.valid_products && ( = cart?.minimum || !cart?.minimum) && cart?.valid_address ? ( !openUpselling !== canOpenUpselling ? t('CHECKOUT', 'Checkout') : 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={() => props.onNavigationRedirect('CheckoutNavigator', { screen: 'CheckoutPage', cartUuid: cart?.uuid, businessLogo: cart?.business?.logo, businessName: cart?.business?.name, cartTotal: cart?.total })} style={{ width: '100%', flexDirection: 'row', justifyContent: 'center', borderRadius: 7.6, shadowOpacity: 0 }} /> )} )} setModalIsOpen(false)} > setModalIsOpen(false)} /> setOpenTaxModal({ open: false, data: null })} entireModal customClose > <> setOpenTaxModal({ open: false, data: null })}> ) : ( <OText size={20} > {t('YOUR_CART', 'Your cart')} </OText> ) ) } export const Cart = (props: any) => { const cartProps = { ...props, UIComponent: CartUI } return ( ) }