import React, { useState, useEffect } from 'react'; import { StyleSheet, View, ActivityIndicator, TouchableOpacity } from 'react-native'; import FastImage from 'react-native-fast-image' import ToggleSwitch from 'toggle-switch-react-native'; import { useTheme } from 'styled-components/native'; import { BusinessController as BusinessSingleCard, useUtils, useLanguage, ToastType, useToast, } from 'ordering-components-external/native'; import { Card, Information, Logo } from './styles'; import { OIcon, OText } from '../shared'; import { BusinessControllerParams } from '../../types'; export const BusinessControllerUI = (props: BusinessControllerParams) => { const { businessState, updateBusiness, isUpdateStore, setIsUpdateStore, navigation } = props; const { loading, business, error } = businessState; const theme = useTheme(); const [{ optimizeImage }] = useUtils(); const [, t] = useLanguage(); const [, { showToast }] = useToast(); const [updatingBusiness, setUpdatingBusiness] = useState(false); const handleSwitch = () => { setUpdatingBusiness(true); setIsUpdateStore(true); updateBusiness && updateBusiness(business?.id, { enabled: !business?.enabled }); }; useEffect(() => { if (updatingBusiness && !error) { showToast( ToastType.Info, business?.enabled ? t('ENABLED_BUSINESS', 'Enabled business') : t('DISABLED_BUSINESS', 'Disabled business'), ); } if (error) { showToast( ToastType.Error, t('ERROR_UPDATING_BUSINESS', 'Error updating business'), ); } setIsUpdateStore(false); setUpdatingBusiness(false); }, [business]); const styles = StyleSheet.create({ icon: { borderRadius: 7.6, width: 70, height: 70, }, logo: { padding: 2, borderRadius: 18, shadowColor: '#000', shadowOffset: { width: 0, height: business?.logo ? 1.5 : 0, }, shadowOpacity: 0.21, shadowRadius: 3, elevation: business?.logo ? 7 : 0, }, header: { flexDirection: 'row', justifyContent: 'space-between', width: '100%', }, title: { fontWeight: '600', fontSize: 18, color: theme.colors.textGray, }, address: { fontSize: 14, color: theme.colors.unselectText, }, }); return ( <> {business && ( navigation && business?.slug && navigation.navigate('BusinessProductListing', { slug: business?.slug })} > {business?.name} {business?.address} {business?.zipcode} {loading && isUpdateStore ? ( ) : ( )} )} ); }; export const BusinessController = (props: BusinessControllerParams) => { const BusinessControllerProps = { ...props, isDisabledInterval: true, UIComponent: BusinessControllerUI, }; return ; };