import React, { useState, useEffect } from 'react'
import { View, TouchableOpacity, StyleSheet, Platform } from 'react-native'
import MaterialIcon from 'react-native-vector-icons/MaterialIcons'
import {
BusinessAndProductList,
useLanguage,
useOrder,
useSession,
useUtils,
useToast,
ToastType
} from 'ordering-components/native'
import { OButton, OText } from '../shared'
import { BusinessBasicInformation } from '../BusinessBasicInformation'
import { SearchBar } from '../SearchBar'
import { BusinessProductsCategories } from '../BusinessProductsCategories'
import { BusinessProductsList } from '../BusinessProductsList'
import { BusinessProductsListingParams } from '../../types'
import {
WrapHeader,
TopHeader,
AddressInput,
WrapSearchBar,
WrapContent,
BusinessProductsListingContainer
} from './styles'
import { FloatingButton } from '../FloatingButton'
import { ProductForm } from '../ProductForm'
import { UpsellingProducts } from '../UpsellingProducts'
import { useTheme } from 'styled-components/native'
const PIXELS_TO_SCROLL = 1000
const BusinessProductsListingUI = (props: BusinessProductsListingParams) => {
const {
navigation,
errors,
businessState,
categoryState,
handleChangeSearch,
categorySelected,
searchValue,
handleSearchRedirect,
featuredProducts,
errorQuantityProducts,
header,
logo,
businessId,
categoryId,
productId,
getNextProducts,
handleChangeCategory,
setProductLogin,
updateProductModal,
} = props
const theme = useTheme()
const [, t] = useLanguage()
const [{ auth }] = useSession()
const [orderState] = useOrder()
const [{ parsePrice }] = useUtils()
const [ ,{showToast}] = useToast()
const { business, loading, error } = businessState
const [openBusinessInformation, setOpenBusinessInformation] = useState(false)
const [isOpenSearchBar, setIsOpenSearchBar] = useState(false)
const [curProduct, setCurProduct] = useState(null)
const [openUpselling, setOpenUpselling] = useState(false)
const [canOpenUpselling, setCanOpenUpselling] = useState(false)
const [openModalProduct,setOpenModalProduct] = useState(false)
const currentCart: any = Object.values(orderState.carts).find((cart: any) => cart?.business?.slug === business?.slug) ?? {}
const onRedirect = (route: string, params?: any) => {
navigation.navigate(route, params)
}
const onProductClick = (product: any) => {
onRedirect('ProductDetails', {
product: product,
businessSlug: business.slug,
businessId: business.id,
categoryId: categoryId,
productId: productId,
})
}
const handleCancel = () => {
setIsOpenSearchBar(false)
handleChangeSearch('')
}
const handleCloseProductModal = () => {
setCurProduct(null)
setOpenModalProduct(false)
updateProductModal && updateProductModal(null)
}
const handleUpsellingPage = () => {
onRedirect('CheckoutNavigator', {
screen: 'CheckoutPage',
cartUuid: currentCart?.uuid,
businessLogo: logo,
businessName: business?.name,
cartTotal: currentCart?.total
})
setOpenUpselling(false)
}
useEffect(() => {
if(businessId && categoryId && productId){
setOpenModalProduct(true)
}
}, [])
const handleScroll = ({ nativeEvent }: any) => {
const y = nativeEvent.contentOffset.y
const height = nativeEvent.contentSize.height
const hasMore = !(categoryState.pagination.totalPages === categoryState.pagination.currentPage)
if (y + PIXELS_TO_SCROLL > height && !loading && hasMore && getNextProducts) {
getNextProducts()
showToast(ToastType.Info, t('LOADING_MORE_PRODUCTS', 'Loading more products'))
}
}
return (
<>
0 && categoryState.products.length !== 0}
onScroll={(e: any) => handleScroll(e)}
>
{!loading && business?.id && (
{!isOpenSearchBar && (
<>
(navigation?.canGoBack() && navigation.goBack()) || (auth && navigation.navigate('BottomTab'))}
imgLeftStyle={{ tintColor: '#fff' }}
/>
auth
? onRedirect('AddressList', { isGoBack: true, isFromProductsList: true })
: onRedirect('AddressForm', { address: orderState.options?.address })}
>
{orderState?.options?.address?.address}
{!errorQuantityProducts && (
setIsOpenSearchBar(true)}
style={styles.searchIcon}
>
)}
>
)}
{isOpenSearchBar && (
handleCancel()}
isCancelXButtonShow
noBorderShow
placeholder={t('SEARCH_PRODUCTS', 'Search Products')}
lazyLoad={businessState?.business?.lazy_load_products_recommended}
/>
)}
)}
{!loading && business?.id && (
<>
{!(business?.categories?.length === 0) && (
a.rank - b.rank)]}
categorySelected={categorySelected}
onClickCategory={handleChangeCategory}
featured={featuredProducts}
openBusinessInformation={openBusinessInformation}
/>
)}
a.rank - b.rank)
]}
category={categorySelected}
categoryState={categoryState}
businessId={business.id}
errors={errors}
onProductClick={onProductClick}
handleSearchRedirect={handleSearchRedirect}
featured={featuredProducts}
searchValue={searchValue}
handleClearSearch={handleChangeSearch}
errorQuantityProducts={errorQuantityProducts}
handleCancelSearch={handleCancel}
/>
>
)}
{loading && !error && (
<>
>
)}
{!loading && auth && currentCart?.products?.length > 0 && categoryState.products.length !== 0 && (
= currentCart?.minimum
? !openUpselling ? t('VIEW_ORDER', 'View Order') : t('LOADING', 'Loading')
: `${t('MINIMUN_SUBTOTAL_ORDER', 'Minimum subtotal order:')} ${parsePrice(currentCart?.minimum)}`
}
isSecondaryBtn={currentCart?.subtotal_to_calculate < currentCart?.minimum}
btnLeftValueShow={currentCart?.subtotal_to_calculate >= currentCart?.minimum && !openUpselling && currentCart?.products?.length > 0}
btnRightValueShow={currentCart?.subtotal_to_calculate >= currentCart?.minimum && !openUpselling && currentCart?.products?.length > 0}
btnLeftValue={currentCart?.products?.length}
btnRightValue={parsePrice(currentCart?.total)}
disabled={openUpselling || currentCart?.subtotal_to_calculate < currentCart?.minimum}
handleClick={() => setOpenUpselling(true)}
/>
)}
{openUpselling && (
)}
>
)
}
const styles = StyleSheet.create({
mainContainer: {
flex: 1,
},
BackIcon: {
paddingRight: 20,
},
headerItem: {
flexDirection: 'row',
alignItems: 'center',
marginVertical: 15,
marginHorizontal: 20,
},
btnBackArrow: {
borderWidth: 0,
color: '#FFF',
backgroundColor: 'rgba(0,0,0,0.3)',
borderRadius: 24,
marginRight: 15,
},
searchIcon: {
borderWidth: 0,
color: '#FFF',
backgroundColor: 'rgba(0,0,0,0.3)',
borderRadius: 24,
padding: 15,
justifyContent: 'center'
}
})
export const BusinessProductsListing = (props: BusinessProductsListingParams) => {
const businessProductslistingProps = {
...props,
isForceSearch: Platform.OS === 'ios',
UIComponent: BusinessProductsListingUI
}
return (
)
}