import React, { Component } from 'react' import PropTypes from 'prop-types' import actions from '../actions' export default class JourneyPattern extends Component{ constructor(props){ super(props) this.previousSpId = undefined this.updateCosts = this.updateCosts.bind(this) } updateCosts(e) { let costs = { [e.target.dataset.costsKey]: { [e.target.name]: parseFloat(e.target.value) } } this.props.onUpdateJourneyPatternCosts(costs) } vehicleJourneyURL(jpOid) { let routeURL = window.location.pathname.split('/', 7).join('/') let vjURL = routeURL + '/vehicle_journeys?jp=' + jpOid return ( {I18n.t('journey_patterns.journey_pattern.vehicle_journey_at_stops')} ) } hasFeature(key) { return this.props.status.features[key] } cityNameChecker(sp, i) { return this.props.journeyPatterns.showHeader((sp.stop_area_object_id || sp.object_id) + "-" + i) } spNode(sp, headlined){ return (
this.props.onCheckboxChange(e)} type='checkbox' id={sp.position} checked={sp.checked} disabled={(this.props.value.deletable || this.props.status.policy['journey_patterns.update'] == false || this.props.editMode == false) ? 'disabled' : ''} >
) } getErrors(errors) { let err = Object.keys(errors).map((key, index) => { return (
  • {key} { errors[key] }
  • ) }) return ( ) } isDisabled(action) { return !this.props.status.policy[`journey_patterns.${action}`] } totals(onlyCommercial=false){ let totalTime = 0 let totalDistance = 0 let from = null this.props.value.stop_points.map((stopPoint, i) =>{ let usePoint = stopPoint.checked if(onlyCommercial && (i == 0 || i == this.props.value.stop_points.length - 1) && stopPoint.kind == "non_commercial"){ usePoint = false } if(from && usePoint){ let [costsKey, costs, time, distance] = this.getTimeAndDistanceBetweenStops(from, stopPoint.id) totalTime += time totalDistance += distance } if(usePoint){ from = stopPoint.id } }) return [this.formatTime(totalTime), this.formatDistance(totalDistance)] } getTimeAndDistanceBetweenStops(from, to){ let costsKey = from + "-" + to let costs = this.getCosts(costsKey) let time = costs['time'] || 0 let distance = costs['distance'] || 0 return [costsKey, costs, time, distance] } getCosts(costsKey) { let cost = this.props.value.costs[costsKey] if (cost) { return cost } this.props.fetchRouteCosts(costsKey) return { distance: 0, time: 0 } } formatDistance(distance){ return parseFloat(Math.round(distance * 100) / 100).toFixed(2) + " km" } formatTime(time){ if(time < 60){ return time + " min" } else{ let hours = parseInt(time/60) let minutes = (time - 60*hours) return hours + " h " + (minutes > 0 ? minutes : '') } } render() { this.previousSpId = undefined let [totalTime, totalDistance] = this.totals(false) let [commercialTotalTime, commercialTotalDistance] = this.totals(true) return (
    {this.props.value.object_id ? this.props.value.short_id : '-'}
    {this.props.value.registration_number}
    {I18n.t('journey_patterns.show.stop_points_count', {count: actions.getChecked(this.props.value.stop_points).length})}
    {this.hasFeature('costs_in_journey_patterns') &&
    {totalDistance} {totalTime}
    } {this.hasFeature('costs_in_journey_patterns') &&
    {commercialTotalDistance} {commercialTotalTime}
    }
    • {this.vehicleJourneyURL(this.props.value.object_id)}
    {this.props.value.stop_points.map((stopPoint, i) =>{ let costs = null let costsKey = null let time = null let distance = null let time_in_words = null if(this.previousSpId && stopPoint.checked){ [costsKey, costs, time, distance] = this.getTimeAndDistanceBetweenStops(this.previousSpId, stopPoint.id) time_in_words = this.formatTime(time) } if(stopPoint.checked){ this.previousSpId = stopPoint.id } let headlined = this.cityNameChecker(stopPoint, i) return (
    {this.spNode(stopPoint, headlined)}
    {this.hasFeature('costs_in_journey_patterns') && costs &&
    {this.props.editMode &&

    km

    min

    } {!this.props.editMode &&

    {this.formatDistance(costs['distance'] || 0)}

    {time_in_words}

    }
    }
    ) })}
    ) } } JourneyPattern.propTypes = { value: PropTypes.object, index: PropTypes.number, onCheckboxChange: PropTypes.func.isRequired, onOpenEditModal: PropTypes.func.isRequired, onDeleteJourneyPattern: PropTypes.func.isRequired, journeyPatterns: PropTypes.object.isRequired, fetchRouteCosts: PropTypes.func.isRequired }