import React, { useState, useEffect, useRef } from 'react'; import FeatherIcon from 'react-native-vector-icons/Feather'; import { Linking, Keyboard, Platform, View, KeyboardAvoidingView, TextInput, StyleSheet, TouchableOpacity } from 'react-native'; import { useTheme } from 'styled-components/native'; import SelectDropdown from 'react-native-select-dropdown' import { useLanguage } from 'ordering-components/native'; import { Content, Timer, TimeField, Header, Comments, CommentsButtonGroup, TopActions } from './styles'; import { OText, OButton, OTextarea, OIcon } from '../shared'; import { AcceptOrRejectOrderParams } from '../../types'; import { orderCommentList } from '../../../../../src/utils' export const AcceptOrRejectOrder = (props: AcceptOrRejectOrderParams) => { const { isPage, customerCellphone, action, handleUpdateOrder, actions, closeModal, orderId, notShowCustomerPhone, orderTitle, appTitle, isLoadingOrder } = props; const [, t] = useLanguage(); const theme = useTheme(); const scrollViewRef = useRef(null); const viewRef = useRef(null); const timerRef = useRef() as React.MutableRefObject; const textTareaRef = useRef(); const [hour, setHour] = useState('00'); const [min, setMin] = useState('00'); const [time, setTime] = useState(''); const [comments, setComments] = useState(''); const [rejectReason, setRejectReason] = useState(null); const [keyboardState, setKeyboardState] = useState({ show: false, height: 0 }); const isDriverApp = appTitle?.key === 'DELIVERY_APP' const orderCommentsList = orderCommentList(action) let codeNumberPhone, numberPhone, numberToShow; const phoneNumber = customerCellphone; const titleOrder = t(orderTitle[action]?.key, orderTitle[action]?.text) const buttonText = t(orderTitle[action]?.btnKey, orderTitle[action]?.btnText) const showTextArea = ['reject', 'deliveryFailed', 'pickupFailed', 'notReady', 'forcePickUp', 'forceDelivery'].includes(action) const styles = StyleSheet.create({ selectOption: { alignItems: 'center', justifyContent: 'space-between', minHeight: 40, width: '100%', paddingHorizontal: 15, backgroundColor: theme.colors.inputChat, borderRadius: 7.6, }, buttonTextStyle: { textAlign: 'left', marginHorizontal: 0, fontSize: 16, lineHeight: 24, color: '#748194', textTransform: 'capitalize' }, dropdownStyle: { borderWidth: 1, borderRadius: 8, paddingTop: 5, backgroundColor: '#fff', borderColor: theme.colors.lightGray, overflow: 'hidden', minHeight: 120 }, rowStyle: { display: 'flex', borderBottomWidth: 0, height: 36, alignItems: 'center', paddingHorizontal: 10 }, parent: { flex: 1, backgroundColor: theme.colors.backgroundPage, }, upper: { flex: 1, zIndex: 1001, paddingTop: isPage ? 30 : 40, marginBottom: 10, backgroundColor: theme.colors.backgroundPage }, bottomParent: { justifyContent: "center", paddingHorizontal: 30, alignItems: "center", width: '100%', height: 45, }, bottom: { textAlignVertical: "center", textAlign: "center", }, btnBackArrow: { borderWidth: 0, width: 32, height: 32, tintColor: theme.colors.textGray, backgroundColor: theme.colors.clear, borderColor: theme.colors.clear, shadowColor: theme.colors.clear, paddingLeft: 0, paddingRight: 0 }, }) const handleFocus = () => { viewRef?.current?.measure((x: any, y: any) => { scrollViewRef?.current?.scrollTo({ x: 0, y }); }); }; if (!notShowCustomerPhone) { if (phoneNumber) { codeNumberPhone = phoneNumber.slice(0, 3); numberPhone = phoneNumber.slice(3, phoneNumber?.length); numberToShow = `(${codeNumberPhone}) ${numberPhone}`; } } const handleArrowBack: any = () => { closeModal(false); }; const handleTime = (e: any) => { if ( e.includes(',') || e.includes('.') || e.includes('-') || e.includes(' ') ) return; setTime(e.slice(-4)); const mins = e.slice(-2); const hours = e.slice(-4, -2); setMin(mins); setHour(hours); setTime(`${hours}${mins}`); }; useEffect(() => { if (actions && action === 'accept') { openTimerIOnput(); } if (actions && showTextArea) { openTextTareaOInput(); } }, []); const handleFixTime = () => { if (min >= '60') { setMin('59'); } if (min.length < 2) setMin(`0${min}`); if (hour.length < 2) setHour(`0${hour}`); if (!hour) setHour('00'); }; const openTimerIOnput = () => { const isFocus = timerRef.current?.isFocused(); if (isFocus) { timerRef.current?.blur(); } }; const openTextTareaOInput = () => { const isFocus = textTareaRef.current?.isFocused(); if (isFocus && textTareaRef?.current) { textTareaRef.current.blur(); } if (!isFocus && textTareaRef?.current) { textTareaRef?.current?.focus?.(); } }; const handleAcceptOrReject = () => { handleFixTime(); let minsToSend = min; if (min > '60') minsToSend = '59'; const time = parseInt(hour || '0') * 60 + (parseInt(minsToSend) || 0); let bodyToSend: any = {}; const orderStatus: any = { acceptByBusiness: { prepared_in: time, status: 7, }, rejectByBusiness: { reasons: comments, status: 5, }, acceptByDriver: { delivered_in: time, status: 8, }, rejectByDriver: { reasons: comments, status: 6, reject_reason: rejectReason }, pickupFailedByDriver: { reasons: comments, status: 10, reject_reason: rejectReason }, deliveryFailedByDriver: { reasons: comments, status: 12, reject_reason: rejectReason }, orderNotReady: { reasons: comments, status: 14, reject_reason: rejectReason }, forcePickUp: { reasons: comments, status: 9, reject_reason: rejectReason }, forceDelivery: { reasons: comments, status: 11, reject_reason: rejectReason } }; if (actions && action === 'accept') { bodyToSend = orderStatus[actions.accept]; } if (actions && action === 'reject') { bodyToSend = orderStatus[actions.reject]; } if (actions && action === 'pickupFailed') { bodyToSend = orderStatus[actions.pickupFailed] } if (actions && action === 'deliveryFailed') { bodyToSend = orderStatus[actions.deliveryFailed] } if (actions && action === 'notReady') { bodyToSend = orderStatus[actions.notReady] } if (actions && action === 'forcePickUp') { bodyToSend = orderStatus[actions.forcePickUp] } if (actions && action === 'forceDelivery') { bodyToSend = orderStatus[actions.forceDelivery] } bodyToSend.id = orderId; handleUpdateOrder?.(bodyToSend.status, bodyToSend); }; useEffect(() => { if (actions && action === 'accept') { const interval = setTimeout(() => { timerRef?.current?.focus?.() }, 200) return () => { clearTimeout(interval) } } }, [timerRef?.current]) useEffect(() => { const keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', (e) => setKeyboardState({ show: true, height: e.endCoordinates.height > 100 ? Platform.OS == 'ios' ? e.endCoordinates.height : 0 : 0 })) const keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', () => setKeyboardState({ show: false, height: 0 })) return () => { keyboardDidShowListener.remove() keyboardDidHideListener.remove() } }, []) return ( handleArrowBack()} style={styles.btnBackArrow}> {titleOrder}
{action === 'reject' && ( <> {!notShowCustomerPhone && ( <> {t( 'CALL_YOUR_CUSTOMER_TO_RESOLVE_THE_ISSUE_AS_POLITELY_AS_POSSIBLE', 'Call your customer to resolve the issue as politely as possible', )} {!!numberToShow ? ( Linking.openURL(`tel:${customerCellphone}`) } /> ) : ( Linking.openURL(`tel:${customerCellphone}`) } /> )} )} {t( 'MARK_THE_ORDER_AS_REJECTED', 'Mark the order as rejected', )} {t('NOTE', 'Note')} {': '} {t( 'YOUR_CUSTOMER_WILL_RECEIVE_A_NOTIFICATION_ABOUT_THIS_ACTIONS', 'Your customer will receive a notification about this actions', )} )}
{action === 'accept' && ( openTimerIOnput()}> {hour} {hour.length > 0 && :} {min} {time?.length > 2 && {t('HOURS', 'Hours')}} 2 ? 55 : 95 }}>{t('MINUTES', 'Minutes')} )} handleFixTime()} editable={true} selectionColor={theme.colors.primary} placeholderTextColor={theme.colors.textGray} color={theme.colors.textGray} onEndEditing={handleFixTime} onSubmitEditing={() => handleAcceptOrReject()} onBlur={() => actions && action === 'accept' && timerRef?.current?.focus?.()} /> {orderCommentsList && isDriverApp && ( { setRejectReason(selectedItem?.value) }} buttonTextAfterSelection={(selectedItem) => selectedItem.content} rowTextForSelection={(item) => item.key} buttonStyle={styles.selectOption} buttonTextStyle={styles.buttonTextStyle} renderDropdownIcon={isOpened => { return ; }} dropdownStyle={styles.dropdownStyle} dropdownOverlayColor='transparent' rowStyle={styles.rowStyle} renderCustomizedRowChild={(item) => { return ( {item?.content} ); }} /> )} {showTextArea && ( )}
handleAcceptOrReject()} />
); };