import React, { useState, useEffect } from 'react'; import { TouchableOpacity, StyleSheet, ActivityIndicator } from 'react-native'; import { DriverTips as DriverTipsController, useUtils, useLanguage, useConfig, useOrder } from 'ordering-components/native'; import { useTheme } from 'styled-components/native'; import { OButton, OInput, OText } from '../shared'; import { DTContainer, DTCard, DTWrapperTips, DTForm, DTLabel, DTWrapperInput } from './styles' const DriverTipsUI = (props: any) => { const { driverTip, driverTipsOptions, optionSelected, isFixedPrice, isDriverTipUseCustom, handlerChangeOption } = props; const [{ parsePrice }] = useUtils(); const [, t] = useLanguage(); const [{ configs }] = useConfig(); const [{ options, carts, loading }, orderState] = useOrder(); const theme = useTheme(); const style = StyleSheet.create({ circle: { borderRadius: 30 }, inputStyle: { borderColor: theme.colors.border, borderRadius: 8, marginRight: 20, height: 44, flex: 1, } }) const [value, setvalue] = useState(0); const [isChanging, setChange] = useState(false); const [curOpt, setCurOpt] = useState(optionSelected); const placeholderCurrency = (configs?.currency_position?.value || 'left') === 'left' ? `${configs?.format_number_currency?.value}0` : `0${configs?.format_number_currency?.value}` const handleChangeDriverTip = (val: any) => { let tip = parseFloat(val) tip = isNaN(tip) ? 0 : tip setvalue(tip) } const handleChangeOpt = (option: any) => { setCurOpt(option); setChange(true); handlerChangeOption(option) } useEffect(() => { setChange(false); }, [optionSelected]) return ( {!isDriverTipUseCustom ? ( <> {t('CUSTOM_DRIVER_TIP_MESSAGE', '100% of these tips go directly to your driver')} {driverTipsOptions.sort((a: any, b: any) => parseInt(a) > parseInt(b)).map((option: any, i: number) => ( handleChangeOpt(option)} disabled={loading} > {`${isFixedPrice ? parsePrice(option) : `${option}%`}`} {loading && curOpt === option && isChanging && } ))} {!driverTipsOptions.includes(driverTip) && driverTip > 0 && ( {t('CUSTOM_DRIVER_TIP_AMOUNT', 'The driver\'s current tip comes from a custom option')} )} ) : ( {t('CUSTOM_DRIVER_TIP_MESSAGE', '100% of these tips go directly to your driver')} 0 && value !== driverTip) || !value} onClick={() => { handlerChangeOption(value) setvalue(0) }} style={{ borderRadius: 8, height: 44, shadowOpacity: 0 }} /> {parseFloat(driverTip || 0) > 0 && ( {t('CURRENT_DRIVER_TIP_AMOUNT', 'Current driver tip amount')}: {parsePrice(driverTip)} )} )} ) } export const DriverTips = (props: any) => { const driverTipsProps = { ...props, UIComponent: DriverTipsUI } return }