import React, { useEffect, useRef } from 'react'; import { useState } from 'react' import { TopRoutes } from './truckRatesComponents/topRoutes' import { RouteType, SourceDestinationType, TruckRatesProps, TruckType } from './truckRatesComponents/type' import { truckTypePrimaryGroup } from '../json/truckTypePrimaryGroup'; import PriceRange from './truckRatesComponents/priceRange'; import RateByTruckGroup from './truckRatesComponents/rateByTruckGroup'; import SelectedRoute from './truckRatesComponents/selectedRoute'; import { DailyRates } from './dailyRates'; import util from '../lib/util'; import { GraphSvg } from '../assets/svg/GraphSvg'; import { ListSvg } from '../assets/svg/ListSvg'; import constants from '../lib/common/constants'; type Section = 'graph' | 'history' interface ButtonList { id: number; value: Section; icon: (props: { fillColor: string; }) => React.JSX.Element; } type TruckGroup = { id: number; sort_Name: string; param: string; type: string; truck_type_ids: number[]; } export const TruckRates = (props: TruckRatesProps) => { const { initialRoute, onTruckGroupSelect, onRouteChange, priceRange, cityPriceData, hasLoadMore, loadMore, priceLoading, tripsLoading, showBookLink, utmSource, onPeriodChange, rateData, showGraph, translatedWords } = props const { transportTruckRates = 'Transport Truck Rates', truckFreightCalculator = 'Truck Freight Calculator', rateHistory = 'Rate history', graph = 'Graph', history = 'History', open: openLabel = 'Open', container: containerLabel = 'Container', } = translatedWords || {} const [route, setRoute] = useState(initialRoute) const [activeTab, setActiveTab] = useState('graph'); const [width, setWidth] = useState(305); const containerRef = useRef(null); const [showRatePerTon, setShowRatePerTon] = useState(false) const handleSelectRoute = (city: SourceDestinationType) => { const selectedRoute = { sAliasId: city?.source?.alias?.id || null, sourceId: city?.source_id, sourceBranchId: city?.source?.branch_id, sourceName: city?.source?.alias?.name || city?.source?.name, dAliasId: city?.destination?.alias?.id || null, destinationId: city?.destination_id, destinationBranchId: city?.destination?.branch_id, destinationName: city?.destination?.alias?.name || city?.destination?.name } setRoute({ ...route, ...selectedRoute }) onRouteChange(selectedRoute) } const handleSelectCity = (city: SourceDestinationType, type: "source" | "destination") => { const selectedRoute = { ...(type === "source" ?{sAliasId: city?.source?.alias?.id || null, sourceId: city?.source_id, sourceBranchId: city?.source?.branch_id, sourceName: city?.source?.alias?.name || city?.source?.name} : {}), ...(type === "destination" ? {dAliasId: city?.destination?.alias?.id || null, destinationId: city?.destination_id, destinationBranchId: city?.destination?.branch_id, destinationName: city?.destination?.alias?.name || city?.destination?.name } : {}) } setRoute({ ...route, ...selectedRoute }) onRouteChange({ ...route, ...selectedRoute }) } const [selectedIndex, setSelectedIndex] = useState(0); const tabData = truckTypePrimaryGroup?.map((tab) => { const truck_type:any = tab?.truck_type && tab?.truck_type.length > 0 ? tab.truck_type[0] : {} const type = truck_type?.truck_type_group_id return ({ id: tab.id, sort_Name: tab.name, param: tab.param, type: (type === 1) ? openLabel : containerLabel, truck_type_ids: tab?.truck_type?.map((tt: TruckType) => tt.id) || [] }) }) const handleItemChange = (index: number, truck_group: TruckGroup) => { if(showGraph) { handleItemChangeshowGraph(index, truck_group) } else { handleItemChangeNonGraph(index, truck_group?.id) } } const handleItemChangeNonGraph = (index: number, primary_group_id: number) => { if (tabData[index]?.truck_type_ids?.some(id => constants.ratePerTonTruckTypes.includes(id))) setShowRatePerTon(true); else setShowRatePerTon(false); setSelectedIndex(index); onTruckGroupSelect({ primary_group_id, truck_type_ids: tabData[index].truck_type_ids }) }; const handleItemChangeshowGraph = ( index: number, truck_group: TruckGroup ) => { if (tabData[index]?.truck_type_ids?.some((id) => constants.ratePerTonTruckTypes.includes(id))) setShowRatePerTon(true); else setShowRatePerTon(false); setSelectedIndex(index); onTruckGroupSelect({ primary_group_id: truck_group.id, truck_type_ids: tabData[index].truck_type_ids, transport_service: truck_group.param, }); setRoute({...route, transport_service: truck_group.param}) if (containerRef.current) { containerRef.current.scrollTo({ top: 0, behavior: 'smooth', }); } }; const buttonList: ButtonList[] = [ { id: 0, value: 'graph', icon: GraphSvg }, { id: 1, value: 'history', icon: ListSvg }, ]; const handleButtonChange = (section: Section) => { setActiveTab(section); if (containerRef.current) { containerRef.current.scrollTo({ top: section === 'history' ? width : 0, behavior: 'smooth', }); } }; const handleScroll = () => { // Detect the scroll position to change tabs if (containerRef.current) { const scrollTop = containerRef.current.scrollTop; if (scrollTop >= width) { setActiveTab('history'); } else { setActiveTab('graph'); } } }; useEffect(() => { const container = containerRef.current; if (container) { container.addEventListener('scroll', util.debounce(handleScroll)); } const width = window.innerWidth; if (width < 640) { setWidth(305); } return () => { if (container) { container.removeEventListener( 'scroll', util.debounce(handleScroll) ); } }; }, []); const onHandlePeriodChange = () => { if(onPeriodChange) onPeriodChange() } return (

{transportTruckRates}

{tabData.map((item, index) => (
handleItemChange(index, item)} >

{item.sort_Name}

{`${item.type}`}

))}
{showGraph ? (<>
    {buttonList.map((item) => { const active = activeTab === item.value return (
  • handleButtonChange(item.value)} > {item.value === 'graph' ? graph : history}
  • ); })}

{rateHistory}

) : ( <> )}

{truckFreightCalculator}

) }