import React, { useEffect, useState, useCallback } from 'react'; import { View, Animated, TouchableOpacity } from 'react-native'; import MaterialCommunityIcon from 'react-native-vector-icons/MaterialCommunityIcons'; import { useUtils, useLanguage } from 'ordering-components/native'; import { useTheme } from 'styled-components/native'; import { DeviceOrientationMethods } from '../../../../../src/hooks/DeviceOrientation' const { useDeviceOrientation } = DeviceOrientationMethods import { Accordion, AccordionSection, // ProductInfo, // ProductQuantity, ContentInfo, // ProductImage, AccordionContent, ProductOptionsList, ProductOption, ProductSubOption, ProductComment, } from './styles'; import { OText, OAlert } from '../shared'; import { ProductItemAccordionParams } from '../../types'; import { getCurrenySymbol } from '../../utils'; export const ProductItemAccordion = (props: ProductItemAccordionParams) => { const { isClickableEvent, isCartPending, isCartProduct, product, getProductMax, onDeleteProduct, onEditProduct, currency } = props; const [, t] = useLanguage(); const theme = useTheme(); const [{ parsePrice }] = useUtils(); const [orientationState] = useDeviceOrientation(); const WIDTH_SCREEN = orientationState?.dimensions?.width const [isActive, setActiveState] = useState(false); const [isReadMore, setIsReadMore] = useState(false); const [lengthMore, setLengthMore] = useState(false); 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 parseOptions = typeof productInfo().options === 'string' ? JSON.parse(productInfo().options) : productInfo().options const getProductPrice = (product: any) => { let subOptionPrice = 0 if (Array.isArray(product?.options)) { if (product.options?.length > 0) { for (const option of product.options) { for (const suboption of option.suboptions) { subOptionPrice += suboption.quantity * ((['left', 'right'].includes(suboption.position)) ? (suboption.half_price ?? suboption.price) : suboption.price) } } } } const price = product.quantity * (product.price + subOptionPrice) return parseFloat(price.toFixed(2)) } const getFormattedSubOptionName = ({ quantity, name, position, price }: any) => { const pos = position ? `(${position})` : '' return `${quantity} x ${name} ${pos} +${price}` }; /*useEffect(() => { toggleAccordion() }, [isActive])*/ const productOptions = getProductMax && [...Array(getProductMax(product) + 1)].map((_: any, opt: number) => { return { label: opt === 0 ? t('REMOVE', 'Remove') : opt.toString(), value: opt.toString(), }; }); useEffect(() => { if ( productInfo?.()?.ingredients?.length > 0 || productInfo?.()?.options?.length > 0 || product.comment !== '' ) { setActiveState(true); } }, []); const onTextLayout = useCallback((e: any) => { setLengthMore((e.nativeEvent.lines.length == 2 && e.nativeEvent.lines[1].width > WIDTH_SCREEN * .76) || e.nativeEvent.lines.length > 2); //to check the text is more than 2 lines or not }, []); return ( (!product?.valid_menu && isCartProduct) || isClickableEvent ? {} : setActiveState(!isActive) } activeOpacity={1}> {product?.quantity} {product?.name} {parsePrice(getProductPrice(product), { currency: getCurrenySymbol(currency) })} {( productInfo?.()?.ingredients?.length > 0 || productInfo?.()?.options?.length > 0 || !!product.comment ) && !isClickableEvent && ( )} {onEditProduct && isCartProduct && !isCartPending && product?.valid_menu && ( onEditProduct(product)} /> )} {onDeleteProduct && isCartProduct && !isCartPending && ( onDeleteProduct(product)}> )} {((isCartProduct && !isCartPending && product?.valid_menu && !product?.valid_quantity) || (!product?.valid_menu && isCartProduct && !isCartPending)) && ( {t('NOT_AVAILABLE', 'Not available')} )} {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} ), )} )} {parseOptions?.length > 0 && ( {parseOptions?.map((option: any, i: number) => ( {t(option?.name.toUpperCase(), 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: (['left', 'right'].includes(suboption.position)) ? parsePrice(suboption.half_price ?? suboption.price, { currency: getCurrenySymbol(currency) }) : parsePrice(suboption.price, { currency: getCurrenySymbol(currency) }), })} ))} ))} )} {!!product.comment && ( {t('COMMENT', 'Comment')} {product.comment} {lengthMore && ( setIsReadMore(!isReadMore)} style={{ marginLeft: 10 }}> {isReadMore ? t('SHOW_LESS', 'Show less') : t('READ_MORE', 'Read more')} )} )} ); };