import React, { useState, useEffect } from 'react' import { Pressable, StyleSheet, View, ScrollView, TouchableOpacity, Platform } from 'react-native'; import { useTheme } from 'styled-components/native' import { Fade, Placeholder, PlaceholderLine } from 'rn-placeholder'; import FastImage from 'react-native-fast-image' import NavBar from '../NavBar' import { WalletList, useLanguage, useUtils, useConfig } from 'ordering-components/native' import AntDesignIcon from 'react-native-vector-icons/AntDesign' import { Container, Header, BalanceElement, OTabs, OTab, SectionContent, LoyaltyContent, LoyaltyWrapp, LoyaltyImg, WalletTransactionsWrapper } from './styles' import { OButton, OIcon, OText, OModal } from '../shared'; import { NotFoundSource } from '../NotFoundSource'; import { WalletTransactions } from '../WalletTransactions' import { GiftCardUI } from '../GiftCard/GiftCardUI' const WalletsUI = (props: any) => { const { navigation, walletList, userLoyaltyLevel, transactionsList, setWalletSelected, isWalletCashEnabled, isWalletPointsEnabled, getWallets, refreshWallets, setRefreshWallets } = props const [, t] = useLanguage() const theme = useTheme() const [{ parsePrice }] = useUtils() const [{ configs }] = useConfig() const styles = StyleSheet.create({ logoStyle: { width: 120, height: 120, borderRadius: 8, borderWidth: 1, borderColor: theme.colors.border, flexDirection: 'column', justifyContent: 'center', alignItems: 'center', }, dividerStyle: { height: 8, backgroundColor: theme.colors.backgroundGray100, marginVertical: 25, marginHorizontal: -40, width: '100%' } }); const [tabSelected, setTabSelected] = useState(isWalletCashEnabled ? 'cash' : 'credit_point') const [openHistory, setOpenHistory] = useState(false) const isChewLayout = theme?.header?.components?.layout?.type?.toLowerCase() === 'chew' const hideWalletsTheme = theme?.bar_menu?.components?.wallet?.hidden === true const isWalletEnabled = configs?.cash_wallet?.value && configs?.wallet_enabled?.value === '1' && (isWalletCashEnabled || isWalletPointsEnabled) const currentWalletSelected = (walletList.wallets?.length > 0 && walletList.wallets?.find((w: any) => w.type === tabSelected)) ?? null const loyaltyLevel = Object.keys(userLoyaltyLevel.loyaltyLevel ?? {}).length > 0 && userLoyaltyLevel.loyaltyLevel const walletName: any = { cash: { name: t('CASH_WALLET', 'Cash Wallet'), value: 0, isActive: isWalletCashEnabled }, credit_point: { name: t('POINTS_WALLET', 'Points Wallet'), value: 1, isActive: isWalletPointsEnabled } } const handleChangeTab = (wallet: any) => { setTabSelected(wallet.type) setWalletSelected(wallet.id) } const goToBack = () => { navigation?.canGoBack() && navigation.goBack() } useEffect(() => { if (!isWalletEnabled) { navigation.navigate('BottomTab', { screen: 'Profile' }) } }, [configs]) useEffect(() => { if (refreshWallets) { getWallets() setRefreshWallets && setRefreshWallets(false) } }, [refreshWallets]) return ( <>
{isChewLayout && !openHistory && ( setOpenHistory(true)} style={{ borderRadius: 8, height: 40, width: !hideWalletsTheme ? '100%' : 150, marginTop: !hideWalletsTheme ? 10 : 0 }} /> )}
{!walletList.loading && !userLoyaltyLevel.loading && !walletList.error && walletList.wallets?.length > 0 && ( <> {walletList.wallets?.map((wallet: any) => walletName[wallet.type]?.isActive && ( handleChangeTab(wallet)} > {walletName[wallet.type]?.name} ))} {!!loyaltyLevel && tabSelected === 'credit_point' && ( {`${t('LOYALTY_LEVEL_TITLE', 'Your level is')}`} {loyaltyLevel.image ? ( ) : ( )} {loyaltyLevel.name} )} {currentWalletSelected?.type === 'cash' ? parsePrice(currentWalletSelected?.balance) : currentWalletSelected?.balance } {currentWalletSelected?.type === 'cash' ? configs?.stripe_currency?.value : t('POINTS', 'Points')} {currentWalletSelected?.type === 'cash' && ( <> )} {!isChewLayout && ( )} )} {(walletList?.loading || userLoyaltyLevel.loading) && ( <> {[...Array(4).keys()].map(i => ( ))} )} {!walletList?.loading && !userLoyaltyLevel.loading && (walletList?.error || !walletList?.wallets?.length) && ( )}
setOpenHistory(false)} entireModal customClose > setOpenHistory(false)} icon={AntDesignIcon} iconProps={{ name: 'arrowleft', size: 26 }} /> ) } export const Wallets = (props: any) => { const [{ configs }] = useConfig() const isWalletCashEnabled = configs?.wallet_cash_enabled?.value === '1' const isWalletPointsEnabled = configs?.wallet_credit_point_enabled?.value === '1' const walletsProps = { ...props, UIComponent: WalletsUI, isWalletCashEnabled, isWalletPointsEnabled } return ( ) }