import React, { useEffect, useState } from 'react'; import { UserFormDetails as UserProfileController, useSession, useLanguage, ToastType, useToast, useConfig } from 'ordering-components/native'; import { useTheme } from 'styled-components/native'; import { useForm } from 'react-hook-form'; import Spinner from 'react-native-loading-spinner-overlay'; import { Platform, StyleSheet, TouchableOpacity, View } from 'react-native'; import { ProfileParams } from '../../types'; import { LogoutButton } from '../LogoutButton' import { LanguageSelector } from '../LanguageSelector' import MessageCircle from 'react-native-vector-icons/AntDesign' import Ionicons from 'react-native-vector-icons/Ionicons' import MaterialIcons from 'react-native-vector-icons/MaterialIcons' import FontAwesome from 'react-native-vector-icons/FontAwesome' import FastImage from 'react-native-fast-image' import { OAlert } from '../../../../../src/components/shared' import { WebsocketStatus } from '../WebsocketStatus' import { OIcon, OText, } from '../shared'; import { CenterView, Actions, ListWrap, ListItem, NotificationsWrapper, NotificationBadge } from './styles'; import { useWindowDimensions } from 'react-native'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; import ToggleSwitch from 'toggle-switch-react-native'; const ProfileListUI = (props: ProfileParams) => { const { navigation, notificationsGroup, handleRemoveAccount, removeAccountState, handleChangePromotions, appVersion } = props; const theme = useTheme(); const isChewLayout = theme?.header?.components?.layout?.type?.toLowerCase() === 'chew' const hideProfileImage = theme?.profile?.components?.picture?.hidden const hideOrders = theme?.profile?.components?.orders?.hidden const hideProfile = theme?.profile?.components?.profile?.hidden const hideWallet = theme?.profile?.components?.wallet?.hidden const hideMessages = theme?.bar_menu?.components?.messages?.hidden const hideHelp = theme?.bar_menu?.components?.help?.hidden const hideFavorites = theme?.bar_menu?.components?.favortes?.hidden const hideSession = theme?.bar_menu?.components?.sessions?.hidden const hidePromotions = theme?.bar_menu?.components?.promotions?.hidden const hideAddressList = theme?.profile?.components?.address_list?.hidden const showLanguages = theme?.profile?.components?.languages?.hidden const showNotifications = !theme?.profile?.components?.notification_settings?.hidden const langPickerStyle = StyleSheet.create({ inputAndroid: { color: theme.colors.textNormal, fontSize: 14, fontWeight: '500', paddingEnd: 24, paddingStart: 0, height: 40, borderWidth: 1, borderColor: theme.colors.clear, backgroundColor: theme.colors.clear }, inputIOS: { color: theme.colors.textNormal, fontSize: 14, fontWeight: '500', paddingEnd: 24, height: 40, borderWidth: 1, borderColor: theme.colors.clear, backgroundColor: theme.colors.clear }, icon: { width: 12, marginTop: 7, marginEnd: 7 } }) const styles = StyleSheet.create({ photo: { borderRadius: 7.6, shadowColor: '#000000', shadowOffset: { width: 0, height: 1 }, shadowRadius: 2, shadowOpacity: 0.2, backgroundColor: theme.colors.white, marginEnd: 14 }, pagePadding: { paddingHorizontal: 20 }, messageIconStyle: { fontSize: 18, fontWeight: 'bold', marginEnd: 14 } }); const [{ user }, { logout }] = useSession(); const [, t] = useLanguage(); const [{ configs }] = useConfig(); const [, { showToast }] = useToast(); const { errors } = useForm(); const { height } = useWindowDimensions(); const { top, bottom } = useSafeAreaInsets(); const [confirm, setConfirm] = useState({ open: false, content: null, handleOnAccept: null, id: null, title: null }) const isWalletEnabled = configs?.cash_wallet?.value && configs?.wallet_enabled?.value === '1' && (configs?.wallet_cash_enabled?.value === '1' || configs?.wallet_credit_point_enabled?.value === '1') const IsPromotionsEnabled = configs?.advanced_offers_module?.value === '1' || configs?.advanced_offers_module?.value === true const onRedirect = (route: string, params?: any) => { navigation.navigate(route, params) } useEffect(() => { if (notificationsGroup.result.result && !notificationsGroup.loading) { if (!notificationsGroup.result?.error) { showToast(ToastType.Success, t('UPDATE_SUCCESSFULLY', 'Update successfully')); } } }, [notificationsGroup.result]) useEffect(() => { if (Object.keys(errors).length > 0) { // Convert all errors in one string to show in toast provider const list = Object.values(errors); let stringError = ''; list.map((item: any, i: number) => { stringError += i + 1 === list.length ? `- ${item.message}` : `- ${item.message}\n`; }); showToast(ToastType.Error, stringError); } }, [errors]); const detailProps = { goToBack: () => props.navigation?.canGoBack() && props.navigation.goBack(), onNavigationRedirect: (route: string, params: any) => props.navigation.navigate(route, params), handleRemoveAccount, } useEffect(() => { if (removeAccountState?.result === 'OK') { logout() } }, [removeAccountState]) return ( {user?.photo && !hideProfileImage && ( )} {user?.name} {user?.lastname} {!hideProfile && ( navigation.navigate('ProfileForm', { ...detailProps })}> {t('VIEW_ACCOUNT', 'View account')} )} {!hideAddressList && ( onRedirect('AddressList', { isFromProfile: true, isGoBack: true })} activeOpacity={0.7}> {t('SAVED_PLACES', 'My saved places')} )} {!hideMessages && ( onRedirect('Messages', { isFromProfile: true, isGoBack: true })} activeOpacity={0.7}> {t('MESSAGES', 'Messages')} )} {!hideOrders && ( onRedirect('MyOrders', { isFromProfile: true, isGoBack: true })} activeOpacity={0.7}> {t('MY_ORDERS', 'My Orders')} )} {isWalletEnabled && !hideWallet && ( onRedirect('Wallets', { isFromProfile: true, isGoBack: true })} activeOpacity={0.7}> {t('WALLETS', 'Wallets')} )} {IsPromotionsEnabled && !hidePromotions && ( onRedirect('Promotions', { isFromProfile: true, isGoBack: true })} activeOpacity={0.7}> {t('PROMOTIONS', 'Promotions')} )} {!hideHelp && ( navigation.navigate('Help', {})} activeOpacity={0.7}> {t('HELP', 'Help')} )} {showNotifications && ( navigation.navigate('Notifications', { isFromProfile: true, isGoBack: true })} activeOpacity={0.7}> {t('MARKETING_NOTIFICATIONS', 'Marketing notifications')} handleChangePromotions(!(user && (!!user?.settings?.notification?.newsletter || !!user?.settings?.sms?.newsletter || !!user?.settings?.email?.newsletter)))} /> )} {!hideSession && ( navigation.navigate('Sessions')} activeOpacity={0.7}> {t('SESSIONS', 'Sessions')} )} {!hideFavorites && ( navigation.navigate('Favorite')} activeOpacity={0.7}> {t('FAVORITES', 'Favorites')} )} {!showLanguages && ( )} {appVersion && ( v{appVersion} )} setConfirm({ ...confirm, open: false, title: null })} onClose={() => setConfirm({ ...confirm, open: false, title: null })} /> ); }; export const UserProfile = (props: any) => { const profileProps = { ...props, UIComponent: ProfileListUI, }; return ; };