import React, { useState, useEffect } from 'react' import { Platform, SafeAreaView, StyleSheet, TouchableOpacity, View } from 'react-native' import { UpsellingPage as UpsellingPageController, useUtils, useLanguage, } from 'ordering-components/native' import { useTheme } from 'styled-components/native'; import { OText, OIcon, OModal, OBottomPopup, OButton } from '../shared' import { UpsellingProductsParams } from '../../types' import { Container, UpsellingContainer, Item, Details, AddButton, CloseUpselling, TopBar, TopActions, WrapPrice, WrapperAdd } from './styles' import { ProductForm } from '../ProductForm'; import { OrderSummary } from '../OrderSummary'; import { ScrollView } from 'react-native-gesture-handler'; import NavBar from '../NavBar'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; const UpsellingProductsUI = (props: UpsellingProductsParams) => { const { isCustomMode, isShowTitle, upsellingProducts, business, cart, handleUpsellingPage, handleCloseUpsellingPage, openUpselling, canOpenUpselling, setCanOpenUpselling } = props const theme = useTheme(); const styles = StyleSheet.create({ imageStyle: { width: 73, height: 73, resizeMode: 'cover', borderRadius: 7.6, }, closeUpsellingButton: { borderRadius: 7.6, borderColor: theme.colors.primary, backgroundColor: theme.colors.primary, borderWidth: 1, height: 44, marginBottom: 10, shadowOpacity: 0 }, cancelBtn: { paddingHorizontal: 18, borderWidth: 1, borderRadius: 7.6, borderColor: theme.colors.textSecondary, height: 38 } }) const [actualProduct, setActualProduct] = useState(null) const [modalIsOpen, setModalIsOpen] = useState(false) const [{ parsePrice, optimizeImage }] = useUtils() const [, t] = useLanguage() const { bottom } = useSafeAreaInsets() 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) => { setActualProduct(product) setModalIsOpen(true) } const handleSaveProduct = () => { setActualProduct(null) setModalIsOpen(false) } const getProductPriceWithOffers = (product: any) => { return product?.in_offer ? product?.offer_rate_type === 1 ? product?.price - ((product?.price * product?.offer_rate) / 100) : product?.offer_rate : product?.price } const UpsellingLayout = () => { return ( {isShowTitle && ( {t('WANT_SOMETHING_ELSE', 'Do you want something else?')} )} {upsellingProducts.products.map((product: any) =>(
{product.name} {parsePrice(getProductPriceWithOffers(product))} {product?.in_offer && ( {parsePrice(product?.price)} )} handleFormProduct(product)}> {t('ADD', 'Add')}
))}
) } return ( <> {upsellingProducts?.products?.length === 0 ? null : ( isCustomMode ? ( ) : ( <> {!canOpenUpselling || upsellingProducts?.products?.length === 0 ? null : ( <> {!modalIsOpen && ( handleUpsellingPage()} > handleCloseUpsellingPage()}> {/* handleCloseUpsellingPage()}> {t('CANCEL', 'Cancel')} */} {t('YOUR_CART', 'Your cart')} {t('WANT_SOMETHING_ELSE', 'Do you want something else?')} handleUpsellingPage()} /> )} )} ) )} setModalIsOpen(false)} entireModal customClose > {actualProduct && ( handleSaveProduct()} onClose={() => setModalIsOpen(false)} /> )} ) } export const UpsellingProducts = (props: UpsellingProductsParams) => { const upsellingProductsProps = { ...props, UIComponent: UpsellingProductsUI, } return ( ) }