import React, { useEffect, useRef, useState } from 'react';
import { useLanguage, useOrder, useApi, useSession, useUtils } from 'ordering-components/native';
import { useTheme } from 'styled-components/native';
import { StyleSheet, View, Dimensions, TextStyle, Animated, PanResponder, KeyboardAvoidingView, Platform, BackHandler } from 'react-native';
import { OBottomPopup, OButton, OIcon, OText } from '../shared';
import { BottomPopup, LogoWrapper, Slogan } from './styles';
import { LanguageSelector } from '../LanguageSelector'
import { TouchableOpacity } from 'react-native-gesture-handler';
import { _setStoreData, _removeStoreData } from '../../providers/StoreUtil';
import { ScrollView } from 'react-native-gesture-handler';
import { LoginForm } from '../LoginForm';
import { SignupForm } from '../SignupForm';
import Modal from 'react-native-modal';
import { useFocusEffect } from '@react-navigation/core';
const windowHeight = Dimensions.get('window').height
export const Home = (props: any) => {
const { navigation, route, onNavigationRedirect } = props;
const theme = useTheme();
const [, t] = useLanguage();
const [orderState] = useOrder();
const [ordering] = useApi()
const [, { login }] = useSession()
const [{ optimizeImage }] = useUtils()
const [allBusinesses, setAllBusinesses] = useState([]);
const [openLogin, setOpenLogin] = useState(route?.params?.showAuth || false);
const [openSignUp, setOpenSignUp] = useState(false);
const handleGuessFlow = (page: string, params: any) => {
onNavigationRedirect(page, params);
_setStoreData('isGuestUser', true);
};
const getBusinesses = async () => {
let parameters = {
location: `40.7744146,-73.9678064`,
type: 1
}
const toFetch = ['id', 'logo', 'header', 'name'];
const response = await ordering.businesses().select(toFetch).parameters(parameters).get();
if (response.content.result) {
setAllBusinesses(response.content.result);
}
}
const handleLoginPage = () => {
setOpenLogin(false);
}
const handleSignUpPage = () => {
setOpenSignUp(false);
}
const loginProps = {
navigation,
useLoginByCellphone: true,
useLoginByEmail: true,
loginButtonText: t('LOGIN', 'Login'),
loginButtonBackground: theme.colors.primary,
forgotButtonText: t('FORGOT_YOUR_PASSWORD', 'Forgot your password?'),
registerButtonText: t('SIGNUP', 'Signup'),
onNavigationRedirect: (page: string) => {
if (!page) return
handleLoginPage();
navigation.navigate(page);
},
notificationState: route?.params?.notification_state,
onHandleOpenSignup: () => { handleLoginPage(), setOpenSignUp(true) }
}
const signupProps = {
...props,
useChekoutFileds: true,
loginButtonText: t('LOGIN', 'Login'),
signupButtonText: t('SIGNUP', 'Signup'),
useSignupByEmail: true,
notificationState: route?.params?.notification_state,
onNavigationRedirect: (page: string) => {
if (!page) return
handleSignUpPage();
navigation.navigate(page);
},
handleSuccessSignup: (user: any) => {
_removeStoreData('isGuestUser')
if (user?.id) {
login({
user,
token: user.session.access_token
})
}
},
onHandleOpenLogin: () => { handleSignUpPage(), setOpenLogin(true) }
}
useEffect(() => {
getBusinesses();
}, []);
useEffect(() => {
setTimeout(() => {
setOpenLogin(route?.params?.showAuth || false);
}, 500)
}, [route?.params?.showAuth])
const panY = useRef(new Animated.Value(windowHeight)).current;
let resetPositionAnim = Animated.timing(panY, {
toValue: 0,
duration: 300,
useNativeDriver: false
});
let closeAnim = Animated.timing(panY, {
toValue: 100,
duration: 500,
useNativeDriver: false
});
const panResponders = useRef(
PanResponder.create({
onStartShouldSetPanResponder: () => true,
onMoveShouldSetPanResponder: () => false,
onPanResponderMove: Animated.event(
[
null,
{ dy: panY }
],
{
useNativeDriver: false
}
),
onPanResponderRelease: (e, gs) => {
console.log(gs);
if (gs.dy > 0 && gs.vy > 2) {
return closeAnim.start(() => handleDismiss())
}
return resetPositionAnim.start();
},
})
).current;
const top = panY.interpolate({
inputRange: [-1, 0, 1],
outputRange: [0, 0, 1],
});
const handleDismiss = () => {
closeAnim.start(() => console.log('Closing'));
}
useEffect(() => {
resetPositionAnim.start();
}, [])
useFocusEffect(
React.useCallback(() => {
const onBackPress = () => {
if (openLogin) {
setOpenLogin(false)
return true;
} else if (openSignUp) {
setOpenSignUp(false)
return true;
} else {
return false;
}
};
BackHandler.addEventListener('hardwareBackPress', onBackPress);
return () =>
BackHandler.removeEventListener('hardwareBackPress', onBackPress);
}, [openLogin, openSignUp])
);
return (
<>
{t('WELCOME', 'Welcome!')}
{t('LETS_START_TO_ORDER_NOW', 'Let\'s start to order now')}
{t('SHOP_THESE_GREAT_STORES_IN_USA', 'Shop these great stores in USA')}
{allBusinesses.map(({ logo, id }): any =>
)}
setOpenLogin(true)}
/>
setOpenSignUp(true)}
imgRightSrc={null}
/>
orderState?.options?.address?.address
? handleGuessFlow('BusinessList', { isGuestUser: true })
: handleGuessFlow('AddressForm', { isGuestUser: true })
}
>
{t('CONTINUE_AS_GUEST', 'Continue as guest')}
{t('LOGIN', 'Login')}
{t('SIGN_UP', 'Sign up')}
>
);
};
const styles = StyleSheet.create({
languageSelector: {
marginRight: 0
},
textLink: {
flexDirection: 'row',
textAlign: 'center',
justifyContent: 'center',
alignItems: 'center'
},
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'space-between',
},
logo: {
height: 80,
width: 250,
marginTop: 10
},
slogan: {
height: windowHeight / 2,
width: 400
},
buttons: {
marginVertical: 9,
marginHorizontal: 40,
borderRadius: 3,
borderWidth: 1,
shadowOpacity: 0,
height: 42
},
sloganText: {
textAlign: 'center'
},
wrapperContent: {
marginTop: 20,
},
wrapperBtn: {
width: '100%',
position: 'absolute',
bottom: 0,
marginBottom: 20
},
logoWrap: { height: 86, width: 86, marginEnd: 10, marginTop: 34, borderRadius: 50, borderWidth: 1 },
box: {
height: 150,
width: 150,
backgroundColor: "blue",
borderRadius: 5
},
containerM: {
backgroundColor: 'red',
paddingTop: 12,
borderTopRightRadius: 12,
borderTopLeftRadius: 12,
paddingHorizontal: 40
},
bottomModalStyle: {
justifyContent: 'flex-end',
margin: 0
}
});