import React, { useState, useEffect } from 'react' import { OrderTypeControl, useLanguage, useOrder } from 'ordering-components/native' import { StyleSheet, TouchableOpacity, View, ActivityIndicator, Platform } from 'react-native' import Picker from 'react-native-country-picker-modal'; import { useTheme } from 'styled-components/native' import { OrderTypeWrapper, SelectItem, SelectItemBtn } from './styles' import { OrderTypeSelectParams } from '../../types' import { OText } from '../shared' const OrderTypeSelectorUI = (props: OrderTypeSelectParams) => { const { handleChangeOrderType, typeSelected, defaultValue, configTypes, orderTypes } = props const theme = useTheme(); const [orderState] = useOrder(); const [isOpen, setIsOpen] = useState(false); const [optionSelected, setOptionSelected] = useState(null) let currentDriver; const styles = StyleSheet.create({ itemSelected: { backgroundColor: theme.colors.disabled, }, closeBtn: { width: 40, height: 40, } }) const _orderTypes = orderTypes.filter((type: any) => configTypes?.includes(type.value)) const items: any[] = _orderTypes.map((type) => { return { key: type.value, value: type.value, label: type.content } }) const typeSelectedObj: any = items.find(item => item.value === (defaultValue || typeSelected)) || {} const handleChangeOrderTypeCallback = (orderType: number) => { setOptionSelected(orderType) if(!orderState.loading){ handleChangeOrderType(orderType) } } useEffect(() => { if (optionSelected === orderState?.options?.type && !orderState.loading) { setIsOpen(false) } }, [orderState]) return ( typeSelected !== undefined && ( setIsOpen(false)} withCountryNameButton // @ts-ignore closeButtonStyle={{ width: '100%', alignItems: 'flex-end', padding: 10 }} closeButtonImageStyle={Platform.OS === 'ios' && styles.closeBtn} renderFlagButton={() => ( <> setIsOpen(true)} disabled={items.length === 0 || orderState.loading} > {typeSelectedObj.label} )} flatListProps={{ keyExtractor: (item: any) => item.value, data: items || [], renderItem: ({ item }: any) => ( handleChangeOrderTypeCallback(item.value)} > {optionSelected === item.value && orderState.loading && ( )} {item.label} ), }} /> ) ) } export const OrderTypeSelector = (props: any) => { const [, t] = useLanguage() const orderTypeProps = { ...props, UIComponent: OrderTypeSelectorUI, orderTypes: props.orderType || [ { value: 1, content: t('DELIVERY', 'Delivery') }, { value: 2, content: t('PICKUP', 'Pickup') }, { value: 3, content: t('EAT_IN', 'Eat in') }, { value: 4, content: t('CURBSIDE', 'Curbside') }, { value: 5, content: t('DRIVE_THRU', 'Drive thru') } ] } return }