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.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 &&
}
{!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
}