import React, { useState } from 'react'; import { ScrollView, Platform, View } from 'react-native'; import { useTheme } from 'styled-components/native'; import { Cart as CartController, useOrder, useLanguage, useConfig, useUtils, useValidationFields, } from 'ordering-components/native'; import { CheckoutAction, OrderTypeWrapper, FloatingLayout, OSRow } from './styles'; import { OSBill, OSCoupon, OSTable } from '../OrderSummary/styles'; import { OButton, OIcon, OModal, OText } from '../shared'; import { ProductForm } from '../ProductForm'; import { verifyDecimals } from '../../../../../src/utils'; import { Cart as TypeCart } from '../../types'; import CartItem from '../CartItem'; import NavBar from '../NavBar'; import { CouponControl } from '../CouponControl'; import { LANDSCAPE, PORTRAIT, useDeviceOrientation } from "../../../../../src/hooks/DeviceOrientation"; import { useCartBottomSheet } from '../../providers/CartBottomSheetProvider'; import { Container } from '../../../../../src/layouts/Container'; import AntDesignIcon from 'react-native-vector-icons/AntDesign' const CartUI = (props: any) => { const { cart, clearCart, changeQuantity, getProductMax, offsetDisabled, removeProduct, setIsCartsLoading, navigation, }: CartUIProps = props const theme = useTheme() 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 [orientationState] = useDeviceOrientation(); const [, { hideCartBottomSheet }] = useCartBottomSheet(); const selectedOrderType = orderState?.options?.type; const isCartPending = cart?.status === 2 const isCouponEnabled = validationFields?.fields?.checkout?.coupon?.enabled 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) navigation?.pop(2) hideCartBottomSheet() } catch (error) { setIsCartsLoading && setIsCartsLoading(false) } } const handleChangeOrderType = () => { navigation.push('DeliveryType', { callback: () => { navigation.pop(1) }, goBack: () => { navigation.pop(1) }, }); } const goToBack = () => navigation.goBack(); 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 getIncludedTaxesDiscounts = () => { return cart?.taxes?.filter((tax: any) => tax?.type === 1)?.reduce((carry: number, tax: any) => carry + (tax?.summary?.tax_after_discount ?? tax?.summary?.tax), 0) } return ( <> )} /> {t('THIS_ORDER_IS_TO', 'This order is to')} {' '} {selectedOrderType === 2 && t('TAKE_OUT', 'Take out')} {selectedOrderType === 3 && t('EAT_IN', 'Eat in')} {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?.offers?.length === 0 && orientationState?.orientation == PORTRAIT && ( {cart?.discount_type === 1 ? ( {`${t('VALID_CODE', 'Valid code')}! ${t('YOU_GOT', 'you got')} `} {`${verifyDecimals(cart?.discount_rate, parseNumber)}% `} {`${t('OFF', 'off')}`} ) : ( {`${t('VALID_CODE', 'Valid code')}! `} )} - {parsePrice(cart?.discount || 0)} )} { cart?.offers?.length > 0 && cart?.offers?.filter((offer: any) => offer?.target === 1)?.map((offer: any, i: number) => ( {offer.name} {offer.rate_type === 1 && ( {`(${verifyDecimals(offer?.rate, parsePrice)}%)`} )} - {parsePrice(offer?.summary?.discount)} )) } {cart?.subtotal_with_discount > 0 && cart?.discount > 0 && cart?.total >= 0 && ( {t('SUBTOTAL_WITH_DISCOUNT', 'Subtotal with discount')} {cart?.business?.tax_type === 1 ? ( {parsePrice(cart?.subtotal_with_discount + getIncludedTaxesDiscounts() ?? 0)} ) : ( {parsePrice(cart?.subtotal_with_discount ?? 0)} )} )} { cart.taxes?.length > 0 && cart.taxes.filter((tax: any) => tax.type === 2 && tax?.rate !== 0).map((tax: any, i: number) => ( {tax.name || t('INHERIT_FROM_BUSINESS', 'Inherit from business')}{' '} {`(${verifyDecimals(tax?.rate, parseNumber)}%)`}{' '} {parsePrice(tax?.summary?.tax_after_discount ?? tax?.summary?.tax ?? 0)} )) } { cart?.fees?.length > 0 && cart?.fees?.filter((fee: any) => !(fee.fixed === 0 && fee.percentage === 0)).map((fee: any, i: number) => ( {fee.name || t('INHERIT_FROM_BUSINESS', 'Inherit from business')}{' '} ({fee?.fixed > 0 && `${parsePrice(fee?.fixed)}${fee.percentage > 0 ? ' + ' : ''}`}{fee.percentage > 0 && `${fee.percentage}%`}){' '} {parsePrice(fee?.summary?.fixed + (fee?.summary?.percentage_after_discount ?? fee?.summary?.percentage) ?? 0)} )) } { cart?.offers?.length > 0 && cart?.offers?.filter((offer: any) => offer?.target === 3)?.map((offer: any, i: number) => ( {offer.name} {offer.rate_type === 1 && ( {`(${verifyDecimals(offer?.rate, parsePrice)}%)`} )} - {parsePrice(offer?.summary?.discount)} )) } {selectedOrderType === 1 && cart?.delivery_price > 0 && ( {t('DELIVERY_FEE', 'Delivery Fee')} {parsePrice(cart?.delivery_price)} )} { cart?.offers?.length > 0 && cart?.offers?.filter((offer: any) => offer?.target === 2)?.map((offer: any, i: number) => ( {offer.name} {offer.rate_type === 1 && ( {`(${verifyDecimals(offer?.rate, parsePrice)}%)`} )} - {parsePrice(offer?.summary?.discount)} )) } {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)} )} {!cart?.discount_type && isCouponEnabled && !isCartPending && orientationState?.orientation == PORTRAIT && ( )} {t('TOTAL', 'Total')} {parsePrice(cart?.total >= 0 ? cart?.total : 0)} )} <> {cart?.discount > 0 && cart?.total >= 0 && orientationState?.orientation == LANDSCAPE && ( {cart?.discount_type === 1 ? ( {`${t('VALID_CODE', 'Valid code')}! ${t('YOU_GOT', 'you got')} `} {`${verifyDecimals(cart?.discount_rate, parseNumber)}% `} {`${t('OFF', 'off')}`} ) : ( {`${t('VALID_CODE', 'Valid code')}! `} )} - {parsePrice(cart?.discount || 0)} )} {!cart?.discount_type && isCouponEnabled && !isCartPending && orientationState?.orientation === LANDSCAPE && ( )} = cart?.minimum || !cart?.minimum) && cart?.valid_address ? ( `${t('CONFIRM_THIS_ORDER', 'Confirm this order')}` ) : !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={cart?.subtotal < cart?.minimum || !cart?.valid_address} borderColor={theme.colors.primary} imgRightSrc={null} textStyle={{ color: 'white', textAlign: 'center', flex: 1 }} onClick={() => { navigation?.navigate('CustomerName', { cartUuid: cart?.uuid }) }} style={{ width: '100%', marginTop: 4 }} /> setModalIsOpen(false)} > setModalIsOpen(false)} /> ) } interface CartUIProps { cart: TypeCart, clearCart: any, changeQuantity: any, getProductMax: any, offsetDisabled: any, removeProduct: (product: any, cart: any) => void, handleCartOpen: any, setIsCartsLoading: any, isFromCart: any, navigation: any, } export const Cart = (props: any) => { const cartProps = { ...props, UIComponent: CartUI } return ( ) }