import React, { useState, useEffect } from 'react'
import {
useUtils,
useLanguage,
ProductOptionSuboption as ProductSubOptionController
} from 'ordering-components/native'
import { useTheme } from 'styled-components/native';
import { StyleSheet, View } from 'react-native'
import IconAntDesign from 'react-native-vector-icons/AntDesign';
import FastImage from 'react-native-fast-image';
import {
Container,
IconControl,
QuantityControl,
Checkbox,
PositionControl,
Circle,
Logo
} from './styles'
import { OIcon, OText } from '../shared'
import { InView } from 'react-native-intersection-observer'
import { Placeholder, PlaceholderLine } from 'rn-placeholder';
export const ProductOptionSubOptionUI = (props: any) => {
const {
state,
increment,
decrement,
balance,
option,
suboption,
toggleSelect,
changePosition,
disabled,
setIsScrollAvailable,
image,
enableIntersection,
usePizzaValidation,
pizzaState
} = props
const disableIncrement =
option?.with_half_option
? pizzaState?.[`option:${option?.id}`]?.value === option?.max
: option?.limit_suboptions_by_max
? (balance === option?.max || state.quantity === suboption.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
const theme = useTheme();
const [, t] = useLanguage()
const [{ parsePrice, optimizeImage }] = useUtils()
const [showMessage, setShowMessage] = useState(false)
const [isDirty, setIsDirty] = useState(false)
const [isIntersectionObserver, setIsIntersectionObserver] = useState(!enableIntersection)
const iconsSize = 20
const styles = StyleSheet.create({
icon: {
borderRadius: 7.6,
width: 60,
height: 60
},
logo: {
borderRadius: 10,
marginLeft: 3,
},
});
const handleSuboptionClick = () => {
toggleSelect()
setIsDirty(true)
const minMaxValidation = option?.with_half_option ? usePizzaValidation : (balance === option?.max && option?.suboptions?.length > balance && !(option?.min === 1 && option?.max === 1))
if (!state.selected && minMaxValidation) {
setShowMessage(true)
}
}
const handleChangeInterSection = (inView: boolean) => {
setIsIntersectionObserver(inView)
}
useEffect(() => {
if (balance === option?.max && state?.selected && isDirty) {
setIsDirty(false)
setIsScrollAvailable(option?.id)
}
}, [state?.selected])
useEffect(() => {
if (!(balance === option?.max && option?.suboptions?.length > balance && !(option?.min === 1 && option?.max === 1))) {
setShowMessage(false)
}
}, [balance, pizzaState?.[`option:${option?.id}`]?.value])
return (
handleChangeInterSection(inView)} triggerOnce={true}>
{isIntersectionObserver ? (
handleSuboptionClick() : null}>
handleSuboptionClick()}>
{((option?.min === 0 && option?.max === 1) || option?.max > 1) ? (
state?.selected ? (
) : (
)
) : (
state?.selected ? (
) : (
)
)}
{image && (
)}
{suboption?.name}
{!(option?.max === 1 && option?.min === 1) && option?.allow_suboption_quantity && state?.selected && (
<>
{state.quantity}
>
)}
{option?.with_half_option && state?.selected && (
<>
changePosition('left')}>
changePosition('whole')}>
changePosition('right')}>
>
)}
{price > 0 && (
+ {parsePrice(price)}
)}
) : (
)}
{showMessage && (
{`${t('OPTIONS_MAX_LIMIT', 'Maximum options to choose')}: ${option?.max}`}
)}
)
}
const styles = StyleSheet.create({
inverse: {
transform: [{ rotateY: '180deg' }]
}
})
export const ProductOptionSubOption = (props: any) => {
const productOptionSubOptionProps = {
...props,
UIComponent: ProductOptionSubOptionUI,
isOrigin: true
}
return (
)
}