import React, { useState, useRef } from 'react'
import {
ProductForm as ProductOptions,
useSession,
useLanguage,
useOrder,
useUtils
} from 'ordering-components/native'
import { ProductIngredient } from '../ProductIngredient'
import { ProductOption } from '../ProductOption'
import MaterialCommunityIcon from 'react-native-vector-icons/MaterialCommunityIcons'
import { View, TouchableOpacity, StyleSheet, Dimensions, ScrollView, I18nManager, Platform, Animated } from 'react-native'
import Icon from 'react-native-vector-icons/Feather';
import {
ProductHeader,
WrapHeader,
TopHeader,
WrapContent,
ProductTitle,
ProductDescription,
ProductEditions,
SectionTitle,
WrapperIngredients,
WrapperSubOption,
ProductComment,
ProductActions,
WrapperArrowIcon
} from './styles'
import { OButton, OInput, OText, OIcon } from '../shared'
import { ProductOptionSubOption } from '../ProductOptionSubOption'
import { NotFoundSource } from '../NotFoundSource'
import { Placeholder, PlaceholderLine, Fade } from 'rn-placeholder'
import { useTheme } from 'styled-components/native'
import MaterialIcon from 'react-native-vector-icons/MaterialIcons'
const windowHeight = Dimensions.get('window').height;
const windowWidth = Dimensions.get('window').width
import styled from 'styled-components/native'
const KeyboardView = styled.KeyboardAvoidingView`
flex: 1;
`;
export const ProductOptionsUI = (props: any) => {
const {
navigation,
editMode,
isSoldOut,
productCart,
increment,
decrement,
showOption,
maxProductQuantity,
errors,
handleSave,
handleChangeIngredientState,
handleChangeSuboptionState,
handleChangeCommentState,
productObject,
onClose,
businessSlug
} = props
const theme = useTheme();
const styles = StyleSheet.create({
mainContainer: {
flex: 1,
height: windowHeight
},
headerItem: {
overflow: 'hidden',
borderRadius: 50,
backgroundColor: '#CCCCCC80',
width: 35,
margin: 15
},
optionContainer: {
marginBottom: 20
},
comment: {
borderBottomWidth: 1,
borderRadius: 0,
borderColor: theme.colors.lightGray,
height: 100,
alignItems: 'flex-start',
marginHorizontal: 20
},
quantityControl: {
flexDirection: 'row',
width: '35%',
justifyContent: 'space-between',
alignItems: 'center',
flex: 1,
marginRight: 10
},
btnBackArrow: {
borderWidth: 0,
color: '#FFF',
backgroundColor: 'rgba(0,0,0,0.3)',
borderRadius: 24,
marginRight: 15,
},
productHeaderSkeleton: {
flexDirection: 'row',
width: '100%',
position: 'relative',
maxHeight: 260,
height: 260,
resizeMode: 'cover',
minHeight: 200,
zIndex: 0
},
wrapArrowIcon: {
backgroundColor: theme.colors.lightGray,
padding: 2,
borderRadius: 20
},
productTagWrapper: {
flexDirection: 'row',
alignItems: 'center'
},
productTagImageStyle: {
width: 32,
height: 32,
borderRadius: 8,
resizeMode: 'cover'
},
productTagNameStyle: {
paddingHorizontal: 6,
marginRight: 5
}
})
const [{ optimizeImage, parsePrice }] = useUtils()
const [, t] = useLanguage()
const [orderState] = useOrder()
const [{ auth }] = useSession()
const { product, loading, error } = productObject
const [openIngredient, setOpenIngredient] = useState('active')
const isError = (id: number) => {
let bgColor = theme.colors.white
if (errors[`id:${id}`]) {
bgColor = 'rgba(255, 0, 0, 0.05)'
}
if (isSoldOut || maxProductQuantity <= 0) {
bgColor = 'hsl(0, 0%, 72%)'
}
return bgColor
}
const handleSaveProduct = () => {
const isErrors = Object.values(errors).length > 0
if (!isErrors) {
handleSave && handleSave()
return
}
}
const handleRedirectLogin = (product: any) => {
onClose()
navigation.navigate('Login', { product: { businessId: product?.businessId, id: product?.id, categoryId: product?.categoryId, slug: businessSlug } })
}
const saveErrors = orderState.loading || maxProductQuantity === 0 || Object.keys(errors).length > 0
const rotateAnimation = useRef(new Animated.Value(0)).current
const interpolateRotating = rotateAnimation.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '180deg']
})
const toggleIngredient = () => {
Animated.timing(rotateAnimation, {
toValue: openIngredient === '' ? 0 : 1,
duration: 300,
useNativeDriver: true
}).start()
setOpenIngredient(openIngredient === '' ? 'active' : '')
}
return (
<>
{!error && (
{loading && !product ? (
) : (
<>
>
)}
{loading && !product ? (
) : (
{product?.name || productCart.name}
{((product?.sku && product?.sku !== '-1' && product?.sku !== '1') || (product?.estimated_person)) && (
{
((product?.sku && product?.sku !== '-1' && product?.sku !== '1') || (productCart?.sku && productCart?.sku !== '-1' && productCart?.sku !== '1'))
&& <>{t('SKU', 'Sku')}{' '}{product?.sku || productCart?.sku}>
}
{product?.sku && product?.sku !== '-1' && product?.sku !== '1' && product?.estimated_person && (
<> · >
)}
{product?.estimated_person
&& <>{product?.estimated_person}{' '}{t('ESTIMATED_PERSONS', 'persons')}>
}
)}
{productCart.price ? parsePrice(productCart.price) : ''}
)}
{(product?.description || productCart?.description) && (
{product?.description || productCart?.description}
)}
{product?.tags?.map((tag: any) => (
{tag?.image ? (
) : (
)}
{tag.name}
))}
{loading && !product ? (
<>
{[...Array(2)].map((item, i) => (
{[...Array(3)].map((item, i) => (
))}
))}
>
) : (
{product?.ingredients?.length > 0 && (
toggleIngredient()}
>
{t('INGREDIENTS', 'Ingredients')}
{product?.ingredients.map((ingredient: any) => (
))}
)}
{product?.extras?.map((extra: any) => extra.options.map((option: any) => {
const currentState = productCart.options[`id:${option.id}`] || {}
return (
{
showOption(option) && (
{
option.suboptions.map((suboption: any) => {
const currentState = productCart.options[`id:${option.id}`]?.suboptions[`id:${suboption.id}`] || {}
const balance = productCart.options[`id:${option.id}`]?.balance || 0
return suboption?.enabled ? (
) : null
})
}
)
}
)
}))}
{!product?.hide_special_instructions && (
{t('SPECIAL_COMMENT', 'Special comment')}
handleChangeCommentState({ target: { value: val } })}
isDisabled={!(productCart && !isSoldOut && maxProductQuantity)}
style={styles.comment}
/>
)}
)}
)}
{error && error.length > 0 && (
)}
{!loading && !error && product && (
{productCart && !isSoldOut && maxProductQuantity > 0 && (
{productCart.quantity}
= maxProductQuantity || isSoldOut}
>
= maxProductQuantity || isSoldOut ? theme.colors.mediumGray : theme.colors.gray}
/>
)}
{productCart && !isSoldOut && maxProductQuantity > 0 && auth && orderState.options?.address_id && (
handleSaveProduct()}
imgRightSrc=''
text={`${orderState.loading ? t('LOADING', 'Loading') : editMode ? t('UPDATE', 'Update') : t('ADD_TO_CART', 'Add to Cart')} ${productCart.total ? parsePrice(productCart?.total) : ''}`}
textStyle={{ color: theme.colors.white }}
style={{
backgroundColor: theme.colors.primary,
opacity: saveErrors ? 0.3 : 1,
borderRadius: 0
}}
isDisabled={saveErrors}
/>
)}
{auth && !orderState.options?.address_id && (
orderState.loading ? (
) : (
)
)}
{(!auth || isSoldOut || maxProductQuantity <= 0) && (
handleRedirectLogin(productCart)}
text={isSoldOut || maxProductQuantity <= 0 ? t('SOLD_OUT', 'Sold out') : t('LOGIN_SIGNUP', 'Login / Sign Up')}
imgRightSrc=''
textStyle={{ color: theme.colors.primary }}
style={{ borderColor: theme.colors.primary, backgroundColor: theme.colors.white, borderRadius: 0 }}
/>
)}
)}
>
)
}
export const ProductForm = (props: any) => {
const productOptionsProps = {
...props,
UIComponent: ProductOptionsUI
}
return
}