diff options
3 files changed, 253 insertions, 34 deletions
| diff --git a/app/javascript/vehicle_journeys/components/tools/CreateModal.js b/app/javascript/vehicle_journeys/components/tools/CreateModal.js index 8536f66e6..24d9a23c2 100644 --- a/app/javascript/vehicle_journeys/components/tools/CreateModal.js +++ b/app/javascript/vehicle_journeys/components/tools/CreateModal.js @@ -117,6 +117,11 @@ export default class CreateModal extends Component {                                      className='form-control'                                      onKeyDown={(e) => actions.resetValidation(e.currentTarget)}                                      /> +                                  <input +                                    type='hidden' +                                    ref='tz_offset' +                                    value={new Date().getTimezoneOffset()} +                                    />                                  </div>                                </div>                              </div> diff --git a/app/javascript/vehicle_journeys/reducers/vehicleJourneys.js b/app/javascript/vehicle_journeys/reducers/vehicleJourneys.js index 383dea4a0..8705b3cf2 100644 --- a/app/javascript/vehicle_journeys/reducers/vehicleJourneys.js +++ b/app/javascript/vehicle_journeys/reducers/vehicleJourneys.js @@ -14,47 +14,73 @@ const vehicleJourney= (state = {}, action, keep) => {          hour: 0,          minute: 0        } -      if(action.data["start_time.hour"] && action.data["start_time.minute"] && action.selectedJourneyPattern.full_schedule){ -        current_time.hour = parseInt(action.data["start_time.hour"].value) -        current_time.minute = parseInt(action.data["start_time.minute"].value) || 0 +      let computeSchedule = false +      let userTZOffet = 0 +      if(action.data["start_time.hour"] && action.data["start_time.hour"].value && action.data["start_time.hour"].value.length > 0 && action.data["start_time.minute"] && action.selectedJourneyPattern.full_schedule && action.selectedJourneyPattern.costs){ +        computeSchedule = true +        userTZOffet = action.data["tz_offset"] && parseInt(action.data["tz_offset"].value) || 0 +        current_time.hour = parseInt(action.data["start_time.hour"].value) + parseInt(userTZOffet / 60) +        current_time.minute = 0 +        if(action.data["start_time.minute"].value){ +          current_time.minute = parseInt(action.data["start_time.minute"].value) + (userTZOffet - 60 * parseInt(userTZOffet / 60)) +        }        }        _.each(action.stopPointsList, (sp) =>{          let inJourney = false -        if(action.selectedJourneyPattern.full_schedule && action.selectedJourneyPattern.costs && action.selectedJourneyPattern.costs[prevSp.stop_area_id + "-" + sp.stop_area_id]){ -          let delta = parseInt(action.selectedJourneyPattern.costs[prevSp.stop_area_id + "-" + sp.stop_area_id].time) -          current_time = actions.addMinutesToTime(current_time, delta) -          prevSp = sp -          inJourney = true -        } -        let offsetHours = sp.time_zone_offset / 3600 -        let offsetminutes = sp.time_zone_offset/60 - 60*offsetHours -        let newVjas = { -          delta: 0, -          arrival_time:{ -            hour: (24 + current_time.hour + offsetHours) % 24, -            minute: current_time.minute + offsetminutes -          }, -          stop_point_objectid: sp.object_id, -          stop_area_cityname: sp.city_name, -          dummy: true -        } +        let newVjas +        if(computeSchedule){ +          if(action.selectedJourneyPattern.costs[prevSp.stop_area_id + "-" + sp.stop_area_id]){ +            let delta = parseInt(action.selectedJourneyPattern.costs[prevSp.stop_area_id + "-" + sp.stop_area_id].time) +            current_time = actions.addMinutesToTime(current_time, delta) +            prevSp = sp +            inJourney = true +          } +          let offsetHours = sp.time_zone_offset / 3600 +          let offsetminutes = sp.time_zone_offset/60 - 60*offsetHours +          newVjas = { +            delta: 0, +            arrival_time:{ +              hour: (24 + current_time.hour + offsetHours) % 24, +              minute: current_time.minute + offsetminutes +            }, +            stop_point_objectid: sp.object_id, +            stop_area_cityname: sp.city_name, +            dummy: true +          } -        if(sp.waiting_time && inJourney){ -          current_time = actions.addMinutesToTime(current_time, parseInt(sp.waiting_time)) -        } +          if(sp.waiting_time && inJourney){ +            current_time = actions.addMinutesToTime(current_time, parseInt(sp.waiting_time)) +          } -        newVjas.departure_time = { -          hour: (24 + current_time.hour + offsetHours) % 24, -          minute: current_time.minute + offsetminutes -        } +          newVjas.departure_time = { +            hour: (24 + current_time.hour + offsetHours) % 24, +            minute: current_time.minute + offsetminutes +          } -        if(current_time.hour + offsetHours > 24){ -          newVjas.departure_day_offset = 1 -          newVjas.arrival_day_offset = 1 +          if(current_time.hour + offsetHours > 24){ +            newVjas.departure_day_offset = 1 +            newVjas.arrival_day_offset = 1 +          } +          if(current_time.hour + offsetHours < 0){ +            newVjas.departure_day_offset = -1 +            newVjas.arrival_day_offset = -1 +          }          } -        if(current_time.hour + offsetHours < 0){ -          newVjas.departure_day_offset = -1 -          newVjas.arrival_day_offset = -1 +        else{ +          newVjas = { +            delta: 0, +            arrival_time: { +              hour: 0, +              minute: 0 +            }, +            departure_time: { +              hour: 0, +              minute: 0 +            }, +            stop_point_objectid: sp.object_id, +            stop_area_cityname: sp.city_name, +            dummy: true +          }          }          _.each(action.selectedJourneyPattern.stop_areas, (jp) =>{ diff --git a/spec/javascript/vehicle_journeys/reducers/vehicleJourneys_spec.js b/spec/javascript/vehicle_journeys/reducers/vehicleJourneys_spec.js index 389c60add..608115727 100644 --- a/spec/javascript/vehicle_journeys/reducers/vehicleJourneys_spec.js +++ b/spec/javascript/vehicle_journeys/reducers/vehicleJourneys_spec.js @@ -241,6 +241,194 @@ describe('vehicleJourneys reducer', () => {      }, ...state])    }) +  it('should handle ADD_VEHICLEJOURNEY with a start time and a fully timed JP, and use user\'s TZ', () => { +    let pristineVjasList = [{ +      delta : 0, +      arrival_time : { +        hour: 21, +        minute: 54 +      }, +      departure_time : { +        hour: 21, +        minute: 54 +      }, +      stop_point_objectid: 'test-1', +      stop_area_cityname: 'city', +      dummy: false +    }, +    { +      delta : 0, +      arrival_time : { +        hour: 21, +        minute: 57 +      }, +      departure_time : { +        hour: 22, +        minute: 7 +      }, +      stop_point_objectid: 'test-2', +      stop_area_cityname: 'city', +      dummy: false +    }, +    { +      delta : 0, +      arrival_time : { +        hour: "00", +        minute: "00" +      }, +      departure_time : { +        hour: "00", +        minute: "00" +      }, +      stop_point_objectid: 'test-3', +      stop_area_cityname: 'city', +      dummy: true +    }, +    { +      delta : 0, +      arrival_time : { +        hour: 23, +        minute: 37 +      }, +      departure_time : { +        hour: 23, +        minute: 37 +      }, +      stop_point_objectid: 'test-4', +      stop_area_cityname: 'city', +      dummy: false +    }] +    let fakeData = { +      published_journey_name: {value: 'test'}, +      published_journey_identifier: {value : ''}, +      "start_time.hour": {value : '22'}, +      "start_time.minute": {value : '59'}, +      "tz_offset": {value : '-65'} +    } +    let fakeSelectedJourneyPattern = { +      id: "1", +      full_schedule: true, +      stop_areas: [ +        {stop_area_short_description: {id: 1}}, +        {stop_area_short_description: {id: 2}}, +        {stop_area_short_description: {id: 4}}, +      ], +      costs: { +        "1-2": { +          distance: 10, +          time: 63 +        }, +        "2-4": { +          distance: 10, +          time: 30 +        } +      } +    } +    let fakeSelectedCompany = {name: "ALBATRANS"} +    expect( +      vjReducer(state, { +        type: 'ADD_VEHICLEJOURNEY', +        data: fakeData, +        selectedJourneyPattern: fakeSelectedJourneyPattern, +        stopPointsList: [{object_id: 'test-1', city_name: 'city', stop_area_id: 1, id: 1, time_zone_offset: 0, waiting_time: null}, {object_id: 'test-2', city_name: 'city', stop_area_id: 2, id: 2, time_zone_offset: -3600, waiting_time: 10}, {object_id: 'test-3', city_name: 'city', stop_area_id: 3, id: 3, time_zone_offset: 0, waiting_time: 20}, {object_id: 'test-4', city_name: 'city', stop_area_id: 4, id: 4, time_zone_offset: 0}], +        selectedCompany: fakeSelectedCompany +      }) +    ).toEqual([{ +      journey_pattern: fakeSelectedJourneyPattern, +      company: fakeSelectedCompany, +      published_journey_name: 'test', +      published_journey_identifier: '', +      short_id: '', +      objectid: '', +      footnotes: [], +      time_tables: [], +      purchase_windows: [], +      vehicle_journey_at_stops: pristineVjasList, +      selected: false, +      custom_fields: undefined, +      deletable: false, +      transport_mode: 'undefined', +      transport_submode: 'undefined' +    }, ...state]) +  }) + +  it('should handle ADD_VEHICLEJOURNEY with a start time and a fully timed JP but no time is set', () => { +    let pristineVjasList = [{ +      delta : 0, +      arrival_time : { +        hour: 0, +        minute: 0 +      }, +      departure_time : { +        hour: 0, +        minute: 0 +      }, +      stop_point_objectid: 'test-1', +      stop_area_cityname: 'city', +      dummy: false +    }, +    { +      delta : 0, +      arrival_time : { +        hour: 0, +        minute: 0 +      }, +      departure_time : { +        hour: 0, +        minute: 0 +      }, +      stop_point_objectid: 'test-2', +      stop_area_cityname: 'city', +      dummy: false +    }] +    let fakeData = { +      published_journey_name: {value: 'test'}, +      published_journey_identifier: {value : ''}, +      "start_time.hour": {value : ''}, +      "start_time.minute": {value : ''} +    } +    let fakeSelectedJourneyPattern = { +      id: "1", +      full_schedule: true, +      stop_areas: [ +        {stop_area_short_description: {id: 1}}, +        {stop_area_short_description: {id: 2}}, +      ], +      costs: { +        "1-2": { +          distance: 10, +          time: 63 +        }, +      } +    } +    let fakeSelectedCompany = {name: "ALBATRANS"} +    expect( +      vjReducer(state, { +        type: 'ADD_VEHICLEJOURNEY', +        data: fakeData, +        selectedJourneyPattern: fakeSelectedJourneyPattern, +        stopPointsList: [{object_id: 'test-1', city_name: 'city', stop_area_id: 1, id: 1, time_zone_offset: 0}, {object_id: 'test-2', city_name: 'city', stop_area_id: 2, id: 2, time_zone_offset: -3600}], +        selectedCompany: fakeSelectedCompany +      }) +    ).toEqual([{ +      journey_pattern: fakeSelectedJourneyPattern, +      company: fakeSelectedCompany, +      published_journey_name: 'test', +      published_journey_identifier: '', +      short_id: '', +      objectid: '', +      footnotes: [], +      time_tables: [], +      purchase_windows: [], +      vehicle_journey_at_stops: pristineVjasList, +      selected: false, +      custom_fields: undefined, +      deletable: false, +      transport_mode: 'undefined', +      transport_submode: 'undefined' +    }, ...state]) +  }) +    it('should handle ADD_VEHICLEJOURNEY with a start time and a fully timed JP but the minutes are not set', () => {      let pristineVjasList = [{        delta : 0, | 
