import React, { useState } from 'react';
import { View, Animated, TouchableOpacity, ImageBackground } from 'react-native';
import { useLanguage, useUtils } from 'ordering-components-external/native';
import FastImage from 'react-native-fast-image'
import {
StyledCartItem,
AccordionContent,
ProductOptionsList,
ProductOption,
ProductSubOption,
ProductComment
} from './styles';
import { OButton, OImage, OText, OIconButton } from '../shared';
import EvilIcons from 'react-native-vector-icons/EvilIcons';
import { Product } from '../../types';
import QuantityControl from '../QuantityControl';
import { LANDSCAPE, useDeviceOrientation } from '../../../../../src/hooks/DeviceOrientation';
import { useTheme } from 'styled-components/native';
import FontAwesomeIcon from 'react-native-vector-icons/FontAwesome'
const CartItem = (props: CartItemProps) => {
const theme = useTheme()
const [, t] = useLanguage();
const [orientationState] = useDeviceOrientation();
const [{ parsePrice }] = useUtils();
const [isActive, setActiveState] = useState(false);
const {
isCartPending,
isCartProduct,
product,
changeQuantity,
getProductMax,
onDeleteProduct,
onEditProduct,
isFromCheckout,
} = props
const productInfo = () => {
if (isCartProduct) {
const ingredients = JSON.parse(JSON.stringify(Object.values(product?.ingredients ?? {})))
let options = JSON.parse(JSON.stringify(Object.values(product?.options ?? {})))
options = options.map((option: any) => {
option.suboptions = Object.values(option.suboptions ?? {})
return option
})
return {
...productInfo,
ingredients,
options
}
}
return product
}
const isProductIngredients = productInfo()?.ingredients.length > 0 || productInfo()?.options.length > 0 || product?.comment
const getFormattedSubOptionName = ({ quantity, name, position, price }: { quantity: number, name: string, position: string, price: any }) => {
const pos = position ? `(${position})` : ''
const str = `${quantity} x ${name} ${pos}`
return price ? `${str} ${price}` : str
}
return (
<>
(!product?.valid_menu && isCartProduct)
? {}
: setActiveState(!isActive)}
activeOpacity={0.5}
>
{product?.images ? (
) : (
)}
{product?.name || ''}
{ onEditProduct ? onEditProduct(product) : null }}
iconProps={{ name: 'edit' }}
IconCustom={() => }
/>
)}
style={{ justifyContent: 'flex-start', right: 40 }}
onClick={() => (!product?.valid_menu && isCartProduct)
? {}
: setActiveState(!isActive)}
/>
{parsePrice(product?.total || product?.price)}
1)
? (() => { changeQuantity && changeQuantity(product, (product?.quantity || 0) - 1) })
: undefined
}
onIncrement={changeQuantity && (() => { changeQuantity(product, (product?.quantity || 0) + 1) })}
onDelete={onDeleteProduct && (() => { onDeleteProduct(product) })}
/>
{productInfo()?.ingredients.length > 0 && productInfo()?.ingredients.some((ingredient: any) => !ingredient.selected) && (
{t('INGREDIENTS', 'Ingredients')}
{productInfo()?.ingredients.map((ingredient: any) => !ingredient.selected && (
{t('NO', 'No')} {ingredient.name}
))}
)}
{productInfo()?.options.length > 0 && (
{productInfo()?.options.map((option: any, i: number) => (
{option.name}
{option.suboptions.map((suboption: any) => (
{getFormattedSubOptionName({
quantity: suboption.quantity,
name: suboption.name,
position: (suboption.position !== 'whole') ? t(suboption.position.toUpperCase(), suboption.position) : '',
price: suboption.price > 0 && `+${parsePrice(suboption.price)}`
})}
))}
))}
)}
{product && product?.comment && (
{t('SPECIAL_COMMENT', 'Special Comment')}
{product.comment}
)}
>
);
}
interface CartItemProps {
isCartPending?: boolean,
isCartProduct?: boolean,
product?: Product,
getProductMax?: any,
changeQuantity: (product: any, quantity: number) => {},
onDeleteProduct: (product: any) => void,
onEditProduct: (product: any) => void,
offsetDisabled?: any,
isFromCheckout?: any
}
export default CartItem;