import React, { useEffect } from 'react';
import { View } from 'react-native';
import { Fade, Placeholder, PlaceholderLine } from 'rn-placeholder';
import { FavoriteParams } from '../../types';
import { SingleOrderCard } from '../SingleOrderCard';
import {
FavoriteList as FavoriteListController,
useOrder,
useLanguage
} from 'ordering-components-external/native';
import { useTheme } from 'styled-components/native';
import { _setStoreData } from '../../providers/StoreUtil';
import { Container, WrappButton } from './styles'
import { OButton } from '../shared';
import { BusinessController } from '../BusinessController';
import { SingleProductCard } from '../SingleProductCard';
import { NotFoundSource } from '../NotFoundSource';
import moment from 'moment';
import { getOrderStatus } from '../../utils'
const FavoriteListUI = (props: FavoriteParams) => {
const {
favoriteList,
handleUpdateFavoriteList,
pagination,
getFavoriteList,
navigation,
onNavigationRedirect,
reorderState,
handleReorder,
isBusiness,
isOrder,
isProduct
} = props
const theme = useTheme();
const [, t] = useLanguage()
const [orderState] = useOrder();
const [{ carts }] = useOrder()
const pastOrders = [1, 2, 5, 6, 10, 11, 12, 15, 16, 17]
const onProductClick = (product: any) => {
const categoryId = product?.category?.id
const businessId = product?.category?.business?.id
if (!categoryId || !businessId) return
onNavigationRedirect && onNavigationRedirect('ProductDetails', {
isRedirect: 'business',
productId: product?.id,
businessId: businessId,
categoryId: categoryId,
business: {
store: product?.category?.business.slug,
header: product?.category?.header,
}
})
}
useEffect(() => {
const _businessId = 'businessId:' + reorderState?.result?.business_id
if (reorderState?.error) {
if (reorderState?.result?.business_id) {
_setStoreData('adjust-cart-products', JSON.stringify(_businessId))
onNavigationRedirect && onNavigationRedirect('Business', { store: reorderState?.result?.business?.slug })
}
}
if (!reorderState?.error && reorderState.loading === false && reorderState?.result?.business_id) {
const cartProducts = carts?.[_businessId]?.products
const available = cartProducts.every((product: any) => product.valid === true)
const orderProducts = favoriteList?.favorites.find((order: any) => order?.id === reorderState?.result?.orderId)?.products
if (available && reorderState?.result?.uuid && (cartProducts?.length === orderProducts?.length)) {
onNavigationRedirect && onNavigationRedirect('CheckoutNavigator', { cartUuid: reorderState?.result.uuid })
} else {
_setStoreData('adjust-cart-products', JSON.stringify(_businessId))
cartProducts?.length !== orderProducts?.length && _setStoreData('already-removed', JSON.stringify('removed'))
onNavigationRedirect && onNavigationRedirect('Business', { store: reorderState?.result?.business?.slug })
}
}
}, [reorderState])
const handleBusinessClick = (business: any) => {
onNavigationRedirect && onNavigationRedirect('Business', {
store: business.slug,
header: business.header,
logo: business.logo,
});
}
const BusinessSkeleton = () => {
return (
)
}
const ProductSkeleton = () => {
return (
)
}
const OrderSkeleton = () => {
return (
)
}
return (
{isBusiness && (
<>
{favoriteList?.favorites?.length > 0 && (
favoriteList.favorites?.sort((a: any, b: any) => a?.name?.toLowerCase() > b?.name?.toLowerCase()).map((business: any, i: number) => (
))
)}
{favoriteList?.loading && (
[...Array(5).keys()].map(i => (
))
)}
{!favoriteList?.loading && !favoriteList?.favorites?.length && (
)}
>
)}
{isOrder && (
<>
{favoriteList?.favorites?.length > 0 && (
favoriteList.favorites?.sort((a: any, b: any) => moment(a?.delivery_datetime_utc).valueOf() - moment(b?.delivery_datetime_utc).valueOf())
.map((order: any, i: number) => (
))
)}
{favoriteList?.loading && (
[...Array(5).keys()].map(i => (
))
)}
{!favoriteList?.loading && !favoriteList?.favorites?.length && (
)}
>
)}
{isProduct && (
<>
{favoriteList?.favorites?.length > 0 && (
favoriteList.favorites?.sort((a: any, b: any) => a?.name?.toLowerCase() > b?.name?.toLowerCase()).map((product: any, i: number) => (
))
)}
{favoriteList?.loading && (
[...Array(5).keys()].map(i => (
))
)}
{!favoriteList?.loading && !favoriteList?.favorites?.length && (
)}
>
)}
{!favoriteList?.loading && pagination.totalPages && pagination.currentPage < pagination.totalPages && (
getFavoriteList(pagination?.currentPage + 1)}
text={t('LOAD_MORE_ITEMS', 'Load more items')}
imgRightSrc={null}
style={{ borderRadius: 7.6, shadowOpacity: 0, marginTop: 20 }}
/>
)}
)
}
export const FavoriteList = (props: any) => {
const favoriteBusinessesProps = {
...props,
UIComponent: FavoriteListUI
}
return
}