import React, { useState, useEffect } from 'react' import { I18nManager, Platform, StyleSheet, TextStyle, View } from 'react-native' import { UpsellingPage as UpsellingPageController, useUtils, useLanguage } from 'ordering-components/native' import { OText, OIcon, OModal, OBottomPopup, OButton } from '../shared' import { useTheme } from 'styled-components/native' import { UpsellingProductsParams } from '../../types' import { Container, UpsellingContainer, Item, Details, AddButton, CloseUpselling } from './styles' import { ProductForm } from '../ProductForm'; const UpsellingProductsUI = (props: UpsellingProductsParams) => { const { isCustomMode, upsellingProducts, business, handleUpsellingPage, openUpselling, canOpenUpselling, setCanOpenUpselling, scrollContainerStyle } = props const theme = useTheme(); const styles = StyleSheet.create({ imageStyle: { width: 58, height: 58, resizeMode: 'cover', borderTopRightRadius: I18nManager.isRTL ? 0 : 7.6, borderBottomRightRadius: I18nManager.isRTL ? 0 : 7.6, borderTopLeftRadius: !I18nManager.isRTL ? 0 : 7.6, borderBottomLeftRadius: !I18nManager.isRTL ? 0 : 7.6, }, closeUpsellingButton: { borderRadius: 25, borderColor: theme.colors.primary, backgroundColor: theme.colors.white, borderWidth: 1, height: 42, marginBottom: 10 }, upsellingModal: { height: '50%', top: 250 } }) const [actualProduct, setActualProduct] = useState(null) const [modalIsOpen, setModalIsOpen] = useState(false) 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) => { setActualProduct(product) setModalIsOpen(true) } const handleSaveProduct = () => { setActualProduct(null) setModalIsOpen(false) } const UpsellingLayout = () => { return ( { !upsellingProducts.loading && ( <> { !upsellingProducts.error ? upsellingProducts.products.map((product: any) => (
{product.name} {parsePrice(product.price)} handleFormProduct(product)}> {t('ADD', 'Add')}
)) : ( {upsellingProducts.message} ) } ) }
) } return ( <> {isCustomMode ? ( {upsellingProducts?.products?.length > 0 && {t('WANT_SOMETHING_ELSE', 'Do you want something else?')} } ) : ( <> {!canOpenUpselling || upsellingProducts?.products?.length === 0 ? null : ( <> {!modalIsOpen && ( handleUpsellingPage()} > handleUpsellingPage()} /> )} )} )} setModalIsOpen(false)} entireModal customClose overScreen > {actualProduct && ( handleSaveProduct()} onClose={() => setModalIsOpen(false)} /> )} ) } export const UpsellingProducts = (props: UpsellingProductsParams) => { const upsellingProductsProps = { ...props, UIComponent: UpsellingProductsUI } return ( ) }