import React,{ useState, useEffect } from 'react' import { StyleSheet } from 'react-native' import MaterialComIcon from 'react-native-vector-icons/MaterialCommunityIcons' import { UpsellingPage as UpsellingPageController, useUtils, useLanguage } from 'ordering-components/native' import { OText, OIcon, OModal, OBottomPopup, OButton } from '../shared' import { UpsellingProductsParams } from '../../types' import { Container, UpsellingContainer, Item, Details, AddButton, CloseUpselling } from './styles' import { useTheme } from 'styled-components/native' const UpsellingProductsUI = (props: UpsellingProductsParams) => { const { isCustomMode, upsellingProducts, business, handleUpsellingPage, openUpselling, canOpenUpselling, setCanOpenUpselling, onRedirect, setOpenUpselling } = props const theme = useTheme(); const styles = StyleSheet.create({ imageStyle: { width: 120, height: 90, resizeMode: 'contain', borderRadius: 10 }, closeUpsellingButton: { borderRadius: 25, borderColor: theme.colors.primary, backgroundColor: theme.colors.white, borderWidth: 1, height: 42, marginBottom: 10 }, upsellingModal: { height: '50%', top: 250 } }) const [{ parsePrice }] = useUtils() const [, t] = useLanguage() useEffect(() => { if (!isCustomMode) { if (upsellingProducts?.products?.length && !upsellingProducts.loading) { setCanOpenUpselling && setCanOpenUpselling(true) } if ((!upsellingProducts?.products?.length && !upsellingProducts.loading && !canOpenUpselling && openUpselling) || (!upsellingProducts?.products?.length && !upsellingProducts.loading && openUpselling)) { handleUpsellingPage && handleUpsellingPage() } } }, [upsellingProducts.loading, upsellingProducts?.products.length]) const handleFormProduct = (product: any) => { setOpenUpselling && setOpenUpselling(false) onRedirect && onRedirect('ProductDetails', { product: product, businessId: product?.api?.businessId, businessSlug: business.slug, onAction: () => { setOpenUpselling && setOpenUpselling(true) } }) } const UpsellingLayout = () => { return ( { !upsellingProducts.loading && ( <> { !upsellingProducts.error ? upsellingProducts.products.map((product: any) => (
{product.name} {parsePrice(product.price)}
handleFormProduct(product)}>
)) : ( {upsellingProducts.message} ) } ) }
) } return ( <> {isCustomMode ? ( ) : ( <> {!canOpenUpselling || upsellingProducts?.products?.length === 0 ? null : ( <> handleUpsellingPage()} > handleUpsellingPage()} /> )} )} ) } export const UpsellingProducts = (props : UpsellingProductsParams) => { const upsellingProductsProps = { ...props, UIComponent: UpsellingProductsUI } return ( ) }