import React, { useEffect, useState } from 'react'
import {
useLanguage, useUtils, RedeemGiftCard as RedeemGiftCardController
} from 'ordering-components-external/native'
import { useForm, Controller } from 'react-hook-form'
import { StyleSheet, View, Alert, Platform } from 'react-native';
import { useTheme } from 'styled-components/native';
import { OText, OButton, OInput } from '../../shared';
import AntDesignIcon from 'react-native-vector-icons/AntDesign'
import {
Container,
FormController
} from './styles'
const RedeemGiftCardUI = (props: any) => {
const {
actionState,
redeemedGiftCard,
handleApply,
onClose,
setRedeemedGiftCard
} = props
const theme = useTheme()
const [, t] = useLanguage()
const { handleSubmit, control, errors } = useForm()
const [{ parsePrice }] = useUtils()
const [codeValue, setCodeValue] = useState('')
const style = StyleSheet.create({
btnStyle: {
borderRadius: 7.6,
height: 44,
marginTop: 20
},
inputStyle: {
borderWidth: 1,
borderColor: theme.colors.border,
borderRadius: 7.6,
},
})
const onSubmit = (values) => {
handleApply(values)
}
const handleChangeCode = (string: any) => {
string = string.replace(/-/g, '')
if (!string) return
const codeSlices = string.match(/.{1,4}/g)
string = codeSlices.join('-')
setCodeValue(string)
}
useEffect(() => {
if (Object.keys(errors).length > 0) {
const list = Object.values(errors)
let stringError = ''
list.map((item: any, i: number) => {
stringError += (i + 1) === list.length ? `- ${item.message}` : `- ${item.message}\n`
})
Alert.alert(
t('ERROR', 'Error'),
stringError,
[
{ text: t('OK', 'oK'), onPress: () => { } }
]
)
}
}, [errors])
useEffect(() => {
if (!actionState.error) return
let stringError = ''
if (typeof actionState.error === 'string') {
stringError = actionState.error
} else {
actionState.error.map(item => {
stringError += `- ${item}\n`
})
}
Alert.alert(
t('ERROR', 'Error'),
stringError,
[
{ text: t('OK', 'oK'), onPress: () => { } }
]
)
}, [actionState.error])
return (
{!redeemedGiftCard ? (
{t('REDEEM_GIFT_CARD', 'Redeem a gift card')}
{t('GIFT_CARD_CODE', 'Gift card code')}
(
{
onChange(val)
handleChangeCode(val)
}}
autoCapitalize='characters'
autoCorrect={false}
blurOnSubmit={false}
style={style.inputStyle}
/>
)}
name='code'
rules={{
required: t('VALIDATION_ERROR_REQUIRED', 'Code is required').replace('_attribute_', t('CODE', 'Code'))
}}
defaultValue=""
/>
{t('PASSWORD', 'Password')}
(
onChange(val)}
autoCapitalize='none'
autoCompleteType='password'
autoCorrect={false}
blurOnSubmit={false}
style={style.inputStyle}
/>
)}
name='password'
rules={{
required: t('VALIDATION_ERROR_REQUIRED', 'Password is required').replace('_attribute_', t('PASSWORD', 'Password'))
}}
defaultValue=""
/>
) : (
<>
{t('GIFT_CARD', 'Gift card')}
{t('TYPE', 'Type')}: {redeemedGiftCard?.type}
{t('AMOUNT', 'Amount')}: {parsePrice(redeemedGiftCard?.amount)}
{t('FROM', 'From')}: {redeemedGiftCard?.receiver?.name} {redeemedGiftCard?.receiver?.lastname}
{!!redeemedGiftCard?.title && {t('TITLE', 'Title')}: {redeemedGiftCard?.title}}
{!!redeemedGiftCard?.message && {t('MESSAGES', 'Messages')}: {redeemedGiftCard?.message}}
{
setRedeemedGiftCard(null)
onClose()
}}
text={t('OK', 'Ok')}
textStyle={{ fontSize: 13 }}
imgRightSrc={null}
style={style.btnStyle}
/>
>
)}
)
}
export const RedeemGiftCard = (props: any) => {
const redeemGiftCardProps = {
...props,
UIComponent: RedeemGiftCardUI
}
return
}