import React, { useState } from 'react';
import { Fade, Placeholder, PlaceholderLine } from 'rn-placeholder';
import {
BusinessController as BusinessSingleCard,
useUtils,
useOrder,
useLanguage,
useConfig,
useToast,
useSession,
ToastType
} from 'ordering-components/native';
import { OIcon, OText } from '../shared';
import { Dimensions, StyleSheet, View } from 'react-native';
import { InView } from 'react-native-intersection-observer'
import { BusinessControllerParams } from '../../types';
import { convertHoursToMinutes, lightenDarkenColor, shape } from '../../utils';
import ReactNativeHapticFeedback from "react-native-haptic-feedback";
import {
BusinessHero,
BusinessContent,
BusinessInfo,
Metadata,
BusinessState,
BusinessLogo,
Reviews,
RibbonBox,
ReviewAndFavorite,
OfferBox
} from './styles';
import { useTheme } from 'styled-components/native';
import FontAwesomeIcon from 'react-native-vector-icons/FontAwesome5';
import FastImage from 'react-native-fast-image'
import { LottieAnimation } from '../LottieAnimation';
import { CardAnimation } from '../shared/CardAnimation';
function BusinessControllerPropsAreEqual(prevProps: any, nextProps: any) {
return JSON.stringify(prevProps.business) === JSON.stringify(nextProps.business) &&
prevProps.isBusinessOpen === nextProps.isBusinessOpen
}
const deliveryTypes = [1, 7]
export const BusinessControllerUI = React.memo((props: BusinessControllerParams) => {
const {
business,
handleClick,
navigation,
isBusinessOpen,
style,
businessHeader,
businessFeatured,
businessLogo,
businessReviews,
businessDeliveryPrice,
businessDeliveryTime,
businessPickupTime,
businessDistance,
handleFavoriteBusiness,
enableIntersection,
getBusinessOffer
} = props;
const [{ parsePrice, parseDistance, parseNumber, optimizeImage }] = useUtils();
const [, { showToast }] = useToast()
const [orderState] = useOrder();
const [{ auth }] = useSession()
const [configState] = useConfig();
const [, t] = useLanguage();
const theme = useTheme()
const windowHeight = Dimensions.get('window').height
const [isIntersectionObserver, setIsIntersectionObserver] = useState(!enableIntersection)
const hideBusinessLogo = theme?.business_listing_view?.components?.business?.components?.logo?.hidden
const hideBusinessFee = theme?.business_listing_view?.components?.business?.components?.fee?.hidden
const hideBusinessTime = theme?.business_listing_view?.components?.business?.components?.time?.hidden
const hideBusinessDistance = theme?.business_listing_view?.components?.business?.components?.distance?.hidden
const hideBusinessReviews = theme?.business_listing_view?.components?.business?.components?.reviews?.hidden
const hideBusinessFavorite = theme?.business_listing_view?.components?.business?.components?.favorite?.hidden
const hideBusinessOffer = theme?.business_listing_view?.components?.business?.components?.offer?.hidden
const hideBusinessHeader = theme?.business_listing_view?.components?.business?.components?.header?.hidden
const hideBusinessFavoriteBadge = theme?.business_listing_view?.components?.business?.components?.featured_badge?.hidden
const textSize = 12
const cardHeight = windowHeight * 0.34
const styles = StyleSheet.create({
container: {
marginVertical: 20,
borderRadius: 7.6,
width: '100%',
position: 'relative',
height: cardHeight
},
headerStyle: {
borderTopLeftRadius: 7.6,
borderTopRightRadius: 7.6,
},
businessLogo: {
backgroundColor: 'white',
width: 62,
height: 62,
borderRadius: 7.6,
justifyContent: 'center',
alignItems: 'center',
marginTop: -32,
shadowColor: '#000000',
shadowOffset: { width: 0, height: 1 },
shadowOpacity: 0.1,
shadowRadius: 1,
elevation: 2
},
businessStateView: {
backgroundColor: '#DEE2E6',
borderRadius: 50,
height: 20,
alignItems: 'center',
justifyContent: 'center',
paddingHorizontal: 8,
},
businessStateText: {
textAlign: 'center',
},
starIcon: {
marginHorizontal: 2,
marginTop: -2,
},
featured: {
position: 'absolute',
padding: 8,
backgroundColor: theme.colors.backgroundDark,
opacity: 0.8,
borderRadius: 10,
left: 20,
top: 10,
},
closed: {
},
bullet: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'flex-start',
}
});
const types = ['food', 'laundry', 'alcohol', 'groceries'];
const getBusinessType = () => {
if (Object.keys(business).length <= 0) return t('GENERAL', 'General');
const _types: any = [];
types.forEach((type) => {
if (business[type]) {
_types.push(t(type.toUpperCase(), type));
}
});
return _types.join(', ');
};
const vibrateApp = (impact?: string) => {
const options = {
enableVibrateFallback: true,
ignoreAndroidSystemSettings: false
};
ReactNativeHapticFeedback.trigger(impact || "impactLight", options);
}
const handleBusinessClick = (selectedBusiness: any) => {
vibrateApp()
if (business?.open) handleClick && handleClick(selectedBusiness)
else {
if (configState?.configs?.preorder_status_enabled?.value === '1') {
navigation.navigate('BusinessPreorder', { business: selectedBusiness, handleBusinessClick: handleClick })
return
}
showToast(ToastType.Info, t('ERROR_ADD_PRODUCT_BUSINESS_CLOSED', 'The business is closed at the moment'));
}
}
const handleChangeFavorite = () => {
if (auth) {
handleFavoriteBusiness && handleFavoriteBusiness(!business?.favorite)
} else {
navigation && navigation.navigate('Login');
}
}
const handleChangeInterSection = (inView: boolean) => {
setIsIntersectionObserver(inView)
}
return (
{isIntersectionObserver ? (
handleBusinessClick(business)}
>
{business?.ribbon?.enabled && (
{business?.ribbon?.text}
)}
{!hideBusinessHeader && (
)}
{(businessFeatured ?? business?.featured) && !hideBusinessFavoriteBadge && (
)}
{!hideBusinessOffer && (
!!getBusinessOffer((business?.offers)) &&
{t('DISCOUNT', 'Discount')}{' '}{getBusinessOffer((business?.offers))}
)}
{!isBusinessOpen && (configState?.configs?.preorder_status_enabled?.value === '1') && (
{t('PREORDER', 'PREORDER')}
)}
{!hideBusinessLogo && (
)}
{business?.name}
{(!hideBusinessFavorite || !hideBusinessReviews) && (
{(businessReviews?.reviews?.total > 0 ?? business?.reviews?.total > 0) && !hideBusinessReviews && (
{parseNumber(businessReviews?.reviews?.total ?? business?.reviews?.total, { separator: '.' })}
)}
{!hideBusinessFavorite && (
)}
)}
{business?.address}
{!isBusinessOpen ? (
{t('CLOSED', 'Closed')}
) : (
{deliveryTypes.includes(orderState?.options?.type) && !hideBusinessFee && (
{`${t('DELIVERY_FEE', 'Delivery fee')} ${parsePrice(businessDeliveryPrice ?? business?.delivery_price) + ' \u2022 '}`}
)}
{!hideBusinessTime && (
{`${convertHoursToMinutes(
deliveryTypes.includes(orderState?.options?.type)
? (businessDeliveryTime ?? business?.delivery_time)
: (businessPickupTime ?? business?.pickup_time),
)} \u2022 `}
)}
{!hideBusinessDistance && (
{parseDistance(businessDistance ?? business?.distance)}
)}
)}
) : (
)}
);
}, BusinessControllerPropsAreEqual);
export const BusinessController = (props: BusinessControllerParams) => {
const BusinessControllerProps = {
...props,
UIComponent: BusinessControllerUI,
};
return ;
};