import React, { useState, useEffect } from 'react' import { ProductOptionSuboption as ProductSubOptionController, useUtils, useLanguage } from 'ordering-components/native' import { StyleSheet, View } from 'react-native' import MaterialCommunityIcon from 'react-native-vector-icons/MaterialCommunityIcons' import { Container, IconControl, QuantityControl, Checkbox, PositionControl, Circle, LeftSide, RightSide } from './styles' import { OText } from '../shared' import { useTheme } from 'styled-components/native' export const ProductOptionSubOptionUI = (props: any) => { const { state, increment, decrement, balance, option, suboption, toggleSelect, changePosition, disabled } = props const theme = useTheme() const [, t] = useLanguage() const [{ parsePrice }] = useUtils() const [showMessage, setShowMessage] = useState(false) const handleIncrement = () => { increment() } const handleDecrement = () => { decrement() } const handlePosition = (position: string) => { changePosition(position) } const handleSuboptionClick = () => { toggleSelect() if (balance === option?.max && option?.suboptions?.length > balance && !(option?.min === 1 && option?.max === 1)) { setShowMessage(true) } } useEffect(() => { if (!(balance === option?.max && option?.suboptions?.length > balance && !(option?.min === 1 && option?.max === 1))) { setShowMessage(false) } }, [balance]) const disableIncrement = option?.limit_suboptions_by_max ? balance === option?.max : state.quantity === suboption?.max || (!state.selected && balance === option?.max) const price = option?.with_half_option && suboption?.half_price && state.position !== 'whole' ? suboption?.half_price : suboption?.price return ( handleSuboptionClick()} disabled={disabled}> {((option?.min === 0 && option?.max === 1) || option?.max > 1) ? ( state?.selected ? ( ) : ( ) ) : ( state?.selected ? ( ) : ( ) )} {suboption?.name} {showMessage && {`${t('OPTIONS_MAX_LIMIT', 'Maximum options to choose')}: ${option?.max}`}} {option?.allow_suboption_quantity && ( {state.quantity} )} { option?.with_half_option && ( handlePosition('left')}> handlePosition('whole')}> handlePosition('right')}> ) } {price > 0 && ( + {parsePrice(price)} )} ) } const styles = StyleSheet.create({ inverse: { transform: [{ rotateY: '180deg' }] } }) export const ProductOptionSubOption = (props: any) => { const productOptionSubOptionProps = { ...props, UIComponent: ProductOptionSubOptionUI } return ( ) }