import React, { ReactNode, useEffect, useState } from 'react'; import { Badge } from '../../lib/common/badge'; import * as Dialog from '@radix-ui/react-dialog'; import { city } from '../../json/cities'; import { ICity, SourceDestinationState, TruckRatesTranslatedWords } from './type'; import { CloseIconSvg } from '../../assets/svg/closeIconSvg'; import Nodata from '../../lib/common/nodata'; import util from '../../lib/util'; const SelectedRoute = (props: any) => { const { route, handleSelectCity, translatedWords } = props; const { from = 'From', to = 'To', sourceCity = 'Source City', destinationCity = 'Destination City', searchCityPlaceholder = 'Search City (min 3 letter)', noData, }: TruckRatesTranslatedWords = translatedWords || {}; const initial: SourceDestinationState = { source_id: null, source: null, destination: null, destination_id: null, alias: null, }; const [selected, setSelected] = useState(initial); useEffect(() => { setSelected(initial); // eslint-disable-next-line react-hooks/exhaustive-deps }, [selected.source_id, selected.destination_id]); const handleSourceSelect = (city: ICity) => { const _source = { source_id: city.id, source: { id: city.id, branch_id: city.branch_id, name: city.city, alias: (city.alias as any) || null, }, }; setSelected({ ...selected, ..._source }); handleSelectCity({ ...selected, ..._source }, 'source'); }; const handleDestinationSelect = (city: ICity) => { const _destination = { destination_id: city.id, destination: { id: city.id, branch_id: city.branch_id, name: city.city, alias: (city.alias as any) || null, }, }; setSelected({ ...selected, ..._destination }); handleSelectCity({ ...selected, ..._destination }, 'destination'); }; return (
{route.sourceName}
} />
{route.destinationName}
} /> ); }; export default SelectedRoute; interface Props { selected: any; onSelect: any; type: 'source' | 'destination'; trigger: ReactNode; sourceCityLabel?: string; destinationCityLabel?: string; searchCityPlaceholder?: string; noData?: string; } export const DialogBox = (props: Props) => { const { selected, onSelect, type, trigger, sourceCityLabel = 'Source City', destinationCityLabel = 'Destination City', searchCityPlaceholder = 'Search City (min 3 letter)', noData } = props; const [open, setOpen] = React.useState(false); const topCity = city.slice(0, 15); const [cities, setCities] = useState(topCity); const onSearch = (e: any) => { const value = e.target.value.toUpperCase(); const result = value.length > 2 ? city.filter((c: any) => c.city.toUpperCase().includes(value)) : topCity; setCities(result); }; const handleSelect = (city: ICity) => { onSelect(city); setOpen(false); setCities(topCity); }; return ( {trigger}
{type === 'source' ? (
{' '} {selected.source?.name || ( {sourceCityLabel} )}
) : null} {type === 'destination' ? (
{' '} {selected.destination?.name || ( {destinationCityLabel} )}
) : null} onSearch(e)} placeholder={searchCityPlaceholder} autoFocus className="fr-w-full fr-mt-2 fr-py-2 fr-border-0 fr-border-b fr-border-slate-200" />
{util.isEmptyArray(city) ? ( ) : ( cities.map((_city: ICity, index: number) => { return ( ); }) )}
); };