import React, { useEffect, useState } from 'react' import { View, StyleSheet } from 'react-native' import { PlaceSpot as PlaceSpotController, useLanguage, useOrder, useToast, ToastType } from 'ordering-components/native' import { PlaceGroupContainer, PlaceSpotContainer } from './styles' import { NotFoundSource } from '../NotFoundSource' import { OText, ODropDown, OInput, OButton } from '../shared' import { Placeholder, PlaceholderLine } from 'rn-placeholder' import { PlaceSpotParams } from '../../types' import { useTheme } from 'styled-components/native' import SelectDropdown from 'react-native-select-dropdown' import IconAntDesign from 'react-native-vector-icons/AntDesign' const PlaceSpotUI = (props: PlaceSpotParams) => { const { cart, orderTypes, placesState, handleChangePlace, spotNumber, spotState, isCheckout, vehicle, isInputMode, setSpotNumber, setVehicle, handleChangeSpot, setPlaceSpotNumber } = props const theme = useTheme() const [, t] = useLanguage() const [orderState] = useOrder() const [, { showToast }] = useToast(); const [placeGroupSelected, setPlaceGroupSelected] = useState(null) const vehicleInputAllowed = [4, 5] const currentOrderType = isCheckout ? orderState?.options?.type : cart?.delivery_type const isEatin = currentOrderType === 3 const isDriveThru = currentOrderType === 5 const placeholderText = isEatin ? t('EATIN_SPOT_NUMBER', 'Table number') : isDriveThru ? t('DRIVE_THRU_SPOT_NUMBER', 'Drive thru lane') : t('CURBSIDE_SPOT_NUMBER', 'Spot number') const styles = StyleSheet.create({ selectOption: { backgroundColor: theme.colors.backgroundGray100, borderRadius: 7.6, paddingVertical: 10, paddingHorizontal: 14, flexDirection: 'row-reverse', alignItems: 'center', justifyContent: 'space-between', height: 50, width: '100%' }, optionWrapper: { marginBottom: 20 } }) const vehicleTypeList: any = [ { key: 'car', text: t('VEHICLE_TYPE_CAR', 'Car') }, { key: 'truck', text: t('VEHICLE_TYPE_TRUCK', 'Truck') }, { key: 'suv', text: t('VEHICLE_TYPE_SUV', 'SUV') }, { key: 'van', text: t('VEHICLE_TYPE_VAN', 'Van') }, { key: 'motorcycle', text: t('VEHICLE_TYPE_MOTORCYCLE', 'Motorcycle') } ] const vehicleInputList = [ { key: 'model', text: t('VEHICLE_MODEL', 'Model') }, { key: 'car_registration', text: t('VEHICLE_CAR_REGISTRATION', 'Car registration') }, { key: 'color', text: t('VEHICLE_COLOR', 'Color') } ] const getPlacesGroups = () => { const groups = placesState.placeGroups?.filter((group: any) => group?.enabled && placesState?.places?.find((place: any) => place?.enabled && place?.place_group_id === group?.id)) return groups.map((group: any) => ({ value: group, content: group?.name, showOnSelected: group?.name })) } const getPlaces = () => { const places = placeGroupSelected && placesState?.places?.filter((place: any) => place?.enabled && place?.place_group_id === placeGroupSelected?.id) return places.map((place: any) => ({ value: place, content: place.name, showOnSelected: place.name })) } const handlerChangePlace = (place: any) => { handleChangePlace(place) } const onChangeSpot = () => { if (orderState.loading) return if (!Number.isInteger(Number(spotNumber))) { showToast(ToastType.Error, t('VALIDATION_ERROR_INTEGER', 'The _attribute_ must be an integer.').replace('_attribute_', placeholderText)) return } if (Number(spotNumber) < 0) { showToast(ToastType.Error, t('VALIDATION_MUST_BIGGER_ZERO', '_attribute_ must be bigger than zero').replace('_attribute_', placeholderText)) return } const isVehicle = Object.values(vehicle).every(e => e) const bodyToSend: any = {} spotNumber && (bodyToSend.spot_number = spotNumber) isVehicle && (bodyToSend.vehicle = vehicle) if (Object.keys(bodyToSend).length) { handleChangeSpot({ bodyToSend, isCheckout: !!isCheckout }) } } const manageErrorsToShow = (array = []) => { let stringError = '' const list = Array.isArray(array) ? array : Object.values(array) list.map((item: any, i: number) => { stringError += (i + 1) === array.length ? `- ${item?.message || item}` : `- ${item?.message || item}\n` }) return stringError; } useEffect(() => { if (!placesState?.loading) { const placeGroupOnCart = placesState?.placeGroups.find((group: any) => group?.id === cart?.place?.place_group_id) setPlaceGroupSelected(placeGroupOnCart) } }, [placesState]) useEffect(() => { if (spotState?.error?.length > 0) { const errorText = manageErrorsToShow(spotState?.error) showToast(ToastType.Error, errorText) } }, [spotState?.error]) const onChangePlaceSpot = (value: string) => { setSpotNumber(value) setPlaceSpotNumber(value) } return ( {isInputMode ? ( {orderTypes[currentOrderType]} {vehicleInputAllowed.includes(currentOrderType) && ( <> {t('VEHICLE_TYPE', 'Vehicle type')} obj.key === vehicle.type) : null} onSelect={(selectedItem, index) => { setVehicle({ ...vehicle, type: selectedItem.key ?? '' }) }} buttonTextAfterSelection={(selectedItem, index) => { return selectedItem.text }} rowTextForSelection={(item, index) => { return item.text }} buttonStyle={styles.selectOption} buttonTextStyle={{ color: theme.colors.disabled, fontSize: 14, textAlign: 'left', marginHorizontal: 0 }} dropdownStyle={{ borderRadius: 8, borderColor: theme.colors.lightGray }} rowStyle={{ borderBottomColor: theme.colors.backgroundGray100, backgroundColor: theme.colors.backgroundGray100, height: 40, flexDirection: 'column', alignItems: 'flex-start', paddingTop: 8, paddingHorizontal: 14 }} rowTextStyle={{ color: theme.colors.disabled, fontSize: 14, marginHorizontal: 0 }} renderDropdownIcon={() => { return ( ) }} /> {vehicleInputList.map((input: any) => ( {input.text} setVehicle({ ...vehicle, [input.key]: value })} style={{ borderColor: theme.colors.border, borderRadius: 7.6, marginBottom: 20 }} inputStyle={{ fontSize: 12, color: theme.colors.textNormal }} /> ))} )} {placeholderText} onChangePlaceSpot(value)} style={{ borderColor: theme.colors.border, borderRadius: 7.6 }} inputStyle={{ fontSize: 12, color: theme.colors.textNormal }} /> onChangeSpot()} textStyle={{ fontSize: 12 }} imgRightSrc={null} text={t('UPDATE_SPOT_NUMBER', 'Update')} isDisabled={(!spotNumber && !Object.values(vehicle).every(e => e))} style={{ borderRadius: 7.6, height: 44, shadowOpacity: 0, marginTop: 20 }} /> ) : ( <> {(placesState.error || placesState?.placeGroups?.length === 0) && !placesState?.loading && ( )} {placesState?.loading && ( )} {!(placesState.error || placesState?.placeGroups?.length === 0) && !placesState?.loading && ( <> {t('PLACE_GROUP', 'Place group')} setPlaceGroupSelected(group)} defaultValue={placeGroupSelected ?? cart?.place} isModal /> {placeGroupSelected && ( {t('SELECT_YOUR_SPOT', 'Select your spot')} handlerChangePlace(place)} placeholder={t('SELECT_YOUR_SPOT', 'Select your spot')} options={getPlaces()} defaultValue={placesState?.places?.find((place: any) => place?.id === cart?.place_id)} isModal /> )} )} )} ) } export const PlaceSpot = (props: PlaceSpotParams) => { const [, t] = useLanguage() const placeSpotProps = { ...props, UIComponent: PlaceSpotUI, orderTypes: { 1: t('DELIVERY', 'Delivery'), 2: t('PICKUP', 'Pickup'), 3: t('EAT_IN', 'Eat in'), 4: t('CURBSIDE', 'Curbside'), 5: t('DRIVE_THRU', 'Drive thru') } } return }