import React, { useState, useEffect } from 'react'; import { View } from 'react-native'; import { useOrder, useLanguage, useUtils } from 'ordering-components/native'; import MaterialCommunityIcon from 'react-native-vector-icons/MaterialCommunityIcons'; import { convertHoursToMinutes } from '../../utils'; import { BIContainer, BIHeader, BIContent, BIInfo, BIContentInfo, BITotal, BIActions } from './styles'; import { OAlert, OIcon, OText } from '../shared'; export const BusinessItemAccordion = (props: any) => { const { cart, moment, handleClearProducts } = props const [orderState] = useOrder(); const [, t] = useLanguage(); const [{ parsePrice }] = useUtils(); const [alert, setAlert] = useState({ show: false }) const isCartPending = cart?.status === 2 const isClosed = !cart?.valid_schedule const isProducts = cart?.products?.length const [isActive, setActiveState] = useState(false) useEffect(() => { const cartsArray = Object.values(orderState?.carts) const cartsLength = cartsArray.filter((cart: any) => cart.products.length > 0).length ?? 0 if ((cartsLength === 1) && !isClosed) { setActiveState(true) } }, [orderState?.carts]) return ( !isClosed ? setActiveState(!isActive) : isClosed} activeOpacity={1} > {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 && ( {parsePrice(cart?.total >= 0 ? cart?.total : 0)} {t('CART_TOTAL', 'Total')} )} {isClosed && ( {t('CLOSED', 'Closed')} {moment} )} {!isClosed && !isProducts && ( {t('NO_PRODUCTS', 'No products')} )} {props.onNavigationRedirect && !isClosed && ( props.onNavigationRedirect('Business', { store: cart?.business?.slug })} /> )} {!isClosed && !!isProducts && ( <> {!isCartPending && ( setAlert({ show: true, title: t('DELETE_CART', 'Delete Cart'), onAccept: () => { handleClearProducts && handleClearProducts() setAlert({ show: false }) }, content: [t('QUESTION_DELETE_CART', 'Are you sure to you wants delete the selected cart')] })} /> )} )} {props.children} setAlert({ show: false })} onCancel={() => setAlert({ show: false })} content={alert.content} /> ) }