import React, { useEffect } from 'react';
import { useLanguage, ToastType, useToast } from 'ordering-components-external/native';
import { _setStoreData, _removeStoreData } from '../../../../../src/providers/StoreUtil';
import { OButton, OInput, OText } from '../shared';
import { useForm, Controller } from 'react-hook-form';
import { StyleSheet, View } from 'react-native';
import { Container } from '../../layouts/Container';
import NavBar from '../NavBar';
import { OSActions } from '../OrderDetails/styles';
import { LANDSCAPE, PORTRAIT, useDeviceOrientation } from '../../../../../src/hooks/DeviceOrientation';
import { useTheme } from 'styled-components/native';
const CustomerName = (props: Props): React.ReactElement => {
const {
navigation,
onProceedToPay
} = props;
const theme = useTheme()
const [, t] = useLanguage();
const { control, handleSubmit, errors } = useForm();
const [, { showToast }] = useToast();
const [orientationState] = useDeviceOrientation();
const onSubmit = (values: any) => {
_setStoreData('customer_name', { customerName: values.name });
onProceedToPay()
};
const onSkip = () => {
_removeStoreData('customer_name')
onProceedToPay()
}
const styles = StyleSheet.create({
inputStyle: {
borderRadius: 4,
marginBottom: 20,
borderWidth: 1,
borderColor: theme.colors.disabled,
height: 50
},
});
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 goToBack = () => navigation?.goBack();
const submitButton = (
);
const skipButton = (
);
return (
<>
{t('WHATS_YOUR_NAME', "What's your name?")}
(
onChange(val)}
onSubmitEditing={handleSubmit(onSubmit)}
/>
)}
name="name"
rules={{
required: t(
'VALIDATION_ERROR_REQUIRED',
'The field Customer Name is required',
).replace('_attribute_', t('REQUEST_COLLECTION_CUSTOMER_NAME', 'Customer Name')),
pattern: {
value: /^[a-zA-Z áéíóúüñçÁÉÍÓÚÜÑÇ]+$/i,
message: t(
'INVALID_ERROR',
'Invalid name',
).replace('_attribute_', t('NAME', 'Name')),
}
}}
defaultValue=""
/>
{orientationState?.orientation === LANDSCAPE && submitButton}
{orientationState?.orientation === LANDSCAPE && skipButton}
{(orientationState?.orientation === PORTRAIT) && (
{submitButton}
)}
{(orientationState?.orientation === PORTRAIT) && (
{skipButton}
)}
>
);
};
interface Props {
navigation: any;
onProceedToPay: any;
}
export default CustomerName;