import React, { useState, useRef, useEffect } from 'react'; import { TouchableOpacity, View, Vibration } from 'react-native'; import { useOrder, useLanguage, useUtils, useConfig, useEvent } from 'ordering-components/native'; import { useTheme } from 'styled-components/native'; import { BIContainer, BIHeader, BIContent, BIInfo, BIContentInfo, BITotal, BIActions, PriceContainer } from './styles'; import { OAlert, OButton, OIcon, OText } from '../shared'; export const BusinessItemAccordion = (props: any) => { const { cart, moment, singleBusiness, handleClearProducts, handleClickCheckout, checkoutButtonDisabled, isMultiCheckout, isFromUpselling, changeActiveState, isActive, isGiftCart } = props const [orderState] = useOrder(); const [, t] = useLanguage(); const [{ parsePrice }] = useUtils(); const [{ configs }] = useConfig() const theme = useTheme(); const [events] = useEvent() const isCartPending = cart?.status === 2 const isClosed = !cart?.valid_schedule const isProducts = cart?.products?.length const isBusinessChangeEnabled = configs?.cart_change_business_validation?.value === '1' const [viewedCart, setViewedCart] = useState(null) useEffect(() => { const cartsArray = Object.values(orderState?.carts) const cartsLength = cartsArray.filter((cart: any) => cart.products.length > 0).length ?? 0 if (cartsLength === 1) { changeActiveState && changeActiveState(!isClosed, cart?.uuid) } }, [orderState?.carts?.length, isClosed]) const subtotalWithTaxes = cart?.taxes?.reduce((acc: any, item: any) => { if (item?.type === 1) return acc = acc + item?.summary?.tax return acc = acc }, cart?.subtotal) const bgStyle = (subtotalWithTaxes < cart?.minimum || !cart?.valid_address) ? theme.colors.secundary : theme.colors.primary const textStyles = (subtotalWithTaxes < cart?.minimum || !cart?.valid_address) ? theme.colors.black : theme.colors.white useEffect(() => { if (isActive && !isFromUpselling) { if (cart?.uuid !== viewedCart?.uuid) { setViewedCart(cart) events.emit('cart_viewed', cart) } } }, [isActive, viewedCart]) const handleGoToStore = () => { Vibration.vibrate(100) props.onNavigationRedirect('Business', { store: cart?.business?.slug }) } return ( !isClosed ? changeActiveState && changeActiveState(!isClosed, cart?.uuid) : isClosed} activeOpacity={1} > {cart?.business?.name} {props.onNavigationRedirect && !isClosed && !isGiftCart && ( <> handleGoToStore()}> {t('GO_TO_STORE', 'Go to store')} )} {!isCartPending && ( <> {!isGiftCart && ( {' \u2022 '} )} handleClearProducts()} > {t('CLEAR_CART', 'Clear cart')} )} {isBusinessChangeEnabled && props.handleChangeStore && !isGiftCart && ( <> {' \u2022 '} {t('CHANGE_STORE', 'Change store')} )} {/* {!isClosed && !!isProducts && cart?.valid_products && cart?.total > 0 && ( {parsePrice(cart?.total)} {t('CART_TOTAL', 'Total')} )} */} {isClosed && ( {t('CLOSED', 'Closed')} {moment} )} {!isClosed && !isProducts && ( {t('NO_PRODUCTS', 'No products')} )} {!isClosed && !!isProducts && ( <> )} {!isActive && !isClosed && !!isProducts && !isMultiCheckout && ( {parsePrice(cart?.total)} {cart?.valid_products && ( )} )} {props.children} ) }