diff options
| author | jpl | 2017-01-09 18:15:20 +0100 |
|---|---|---|
| committer | jpl | 2017-01-09 18:15:20 +0100 |
| commit | 02a6b47c69df2b8364f85c205cc4381f250bd177 (patch) | |
| tree | 29c15077a4599b962e5a3dd020195a5ed4936d4f | |
| parent | 75a4a30cdb1f01beb860b739c4a68c59900f5f7b (diff) | |
| parent | f96eb2324d401556931ac3e9c37b18b50101818e (diff) | |
| download | chouette-core-02a6b47c69df2b8364f85c205cc4381f250bd177.tar.bz2 | |
Merge branch 'master' into staging
8 files changed, 74 insertions, 34 deletions
diff --git a/app/assets/javascripts/es6_browserified/journey_patterns/actions/index.js b/app/assets/javascripts/es6_browserified/journey_patterns/actions/index.js index f3235f963..386955540 100644 --- a/app/assets/javascripts/es6_browserified/journey_patterns/actions/index.js +++ b/app/assets/javascripts/es6_browserified/journey_patterns/actions/index.js @@ -57,7 +57,7 @@ const actions = { type: 'SAVE_PAGE', dispatch }), - submitJourneyPattern : (dispatch, state) => { + submitJourneyPattern : (dispatch, state, next) => { let urlJSON = window.location.pathname + ".json" let req = new Request(urlJSON, { credentials: 'same-origin', @@ -72,8 +72,11 @@ const actions = { fetch(req) .then(response => response.json()) .then((json) => { - console.log('request for submit') - // dispatch(actions.receiveJourneyPatterns(journeyPatterns)) + if(next){ + dispatch(next) + }else{ + dispatch(actions.receiveJourneyPatterns(json)) + } }) }, fetchJourneyPatterns : (dispatch, currentPage, nextPage) => { diff --git a/app/assets/javascripts/es6_browserified/journey_patterns/components/ConfirmModal.js b/app/assets/javascripts/es6_browserified/journey_patterns/components/ConfirmModal.js index 42653f823..86bbe3acb 100644 --- a/app/assets/javascripts/es6_browserified/journey_patterns/components/ConfirmModal.js +++ b/app/assets/javascripts/es6_browserified/journey_patterns/components/ConfirmModal.js @@ -2,29 +2,29 @@ var React = require('react') var Component = require('react').Component var PropTypes = require('react').PropTypes -const ConfirmModal = ({modal, onModal}) => ( +const ConfirmModal = ({dispatch, modal, onModalAccept, onModalCancel, journeyPatterns}) => ( <div className={ 'modal fade ' + ((modal.type == 'confirm') ? 'in' : '') } id='ConfirmModal'> <div className='modal-dialog'> <div className='modal-content'> <div className='modal-body'> - <p> Voulez-vous sauver vos modifications avant de blabblabla? </p> + <p> Voulez-vous enregistrer vos modifications avant de changer de page? </p> </div> <div className='modal-footer'> <button className='btn btn-default' data-dismiss='modal' type='button' - onClick= {() => {onModal(modal.confirmModal.cancel)}} + onClick= {() => {onModalCancel(modal.confirmModal.cancel)}} > - Annuler + Ne pas enregistrer </button> <button className='btn btn-danger' data-dismiss='modal' type='button' - onClick = {() => {onModal(modal.confirmModal.accept)}} + onClick = {() => {onModalAccept(modal.confirmModal.accept, journeyPatterns)}} > - Valider + Enregistrer </button> </div> </div> @@ -34,7 +34,8 @@ const ConfirmModal = ({modal, onModal}) => ( ConfirmModal.propTypes = { modal: PropTypes.object.isRequired, - onModal: PropTypes.func.isRequired + onModalAccept: PropTypes.func.isRequired, + onModalCancel: PropTypes.func.isRequired } module.exports = ConfirmModal diff --git a/app/assets/javascripts/es6_browserified/journey_patterns/containers/ConfirmModal.js b/app/assets/javascripts/es6_browserified/journey_patterns/containers/ConfirmModal.js index 37f39176f..b78a4f49d 100644 --- a/app/assets/javascripts/es6_browserified/journey_patterns/containers/ConfirmModal.js +++ b/app/assets/javascripts/es6_browserified/journey_patterns/containers/ConfirmModal.js @@ -4,14 +4,18 @@ var ConfirmModal = require('../components/ConfirmModal') const mapStateToProps = (state) => { return { - modal: state.modal + modal: state.modal, + journeyPatterns: state.journeyPatterns } } const mapDispatchToProps = (dispatch) => { return { - onModal: (action) =>{ - dispatch(action) + onModalAccept: (next, state) =>{ + actions.submitJourneyPattern(dispatch, state, next) + }, + onModalCancel: (next) =>{ + dispatch(next) }, onModalClose: () =>{ dispatch(actions.closeModal()) diff --git a/app/assets/javascripts/es6_browserified/journey_patterns/reducers/journeyPatterns.js b/app/assets/javascripts/es6_browserified/journey_patterns/reducers/journeyPatterns.js index 96f6bd8c2..ba1a2cdc5 100644 --- a/app/assets/javascripts/es6_browserified/journey_patterns/reducers/journeyPatterns.js +++ b/app/assets/javascripts/es6_browserified/journey_patterns/reducers/journeyPatterns.js @@ -3,7 +3,8 @@ var actions = require("../actions") const journeyPattern = (state = {}, action) => { switch (action.type) { case 'ADD_JOURNEYPATTERN': - const stop_points = state[0].stop_points.map((s)=>{ + let stopPoints = JSON.parse(JSON.stringify(state[0].stop_points)) + stopPoints.map((s)=>{ s.checked = false return s }) @@ -11,7 +12,7 @@ const journeyPattern = (state = {}, action) => { name: action.data.name.value, published_name: action.data.published_name.value, registration_number: action.data.registration_number.value, - stop_points: stop_points, + stop_points: stopPoints, deletable: false } case 'UPDATE_CHECKBOX_VALUE': diff --git a/app/controllers/journey_patterns_collections_controller.rb b/app/controllers/journey_patterns_collections_controller.rb index 39355932e..cc01c1580 100644 --- a/app/controllers/journey_patterns_collections_controller.rb +++ b/app/controllers/journey_patterns_collections_controller.rb @@ -10,42 +10,71 @@ class JourneyPatternsCollectionsController < ChouetteController alias_method :route, :parent def show - journey_patterns_state + jps_state end def update state = JSON.parse request.raw_post state.each do |item| - journey_pattern = journey_pattern_by_objectid(item['object_id']) - journey_pattern_update_stop_points(journey_pattern, item['stop_points']) if journey_pattern + jp = jp_by_objectid(item['object_id']) || create_jp(item) + if item['deletable'] + state.delete(item) if jp.destroy + next + end + update_journey_pattern(jp, item) end + errors = state.any? {|item| item['errors']} - journey_patterns_state + respond_to do |format| + format.json { render json: state, status: errors ? :unprocessable_entity : :ok } + end end protected + def update_jp jp, item + jp.update_attributes(fetch_jp_attributes(item)) + end + + def fetch_jp_attributes item + { + name: item['name'], + published_name: item['published_name'], + registration_number: item['registration_number'] + } + end - def journey_pattern_update_stop_points journey_pattern, stop_points - stop_points.each do |sp| - stop_id = sp['id'] - exist = journey_pattern.stop_area_ids.include?(stop_id) + def create_jp item + jp = route.journey_patterns.create(fetch_jp_attributes(item)) + if jp.persisted? + item['object_id'] = jp.objectid + else + item['errors'] = jp.errors + end + jp + end + + def update_journey_pattern jp, item + item['stop_points'].each do |sp| + exist = jp.stop_area_ids.include?(sp['id']) next if exist && sp['checked'] - stop_point = route.stop_points.find_by(stop_area_id: stop_id) - if sp['checked'] && !exist - journey_pattern.stop_points << stop_point - else - journey_pattern.stop_points.delete(stop_point) + stop_point = route.stop_points.find_by(stop_area_id: sp['id']) + if !exist && sp['checked'] + jp.stop_points << stop_point + end + if exist && !sp['checked'] + jp.stop_points.delete(stop_point) end end + update_jp(jp, item) end - def journey_patterns_state + def jps_state @q = route.journey_patterns.includes(:stop_points) @journey_patterns ||= @q.paginate(:page => params[:page]).order(:name) end - def journey_pattern_by_objectid objectid + def jp_by_objectid objectid Chouette::JourneyPattern.find_by(objectid: objectid) end end diff --git a/app/models/chouette/journey_pattern.rb b/app/models/chouette/journey_pattern.rb index 75e1a4a14..945ee4f70 100644 --- a/app/models/chouette/journey_pattern.rb +++ b/app/models/chouette/journey_pattern.rb @@ -12,6 +12,7 @@ class Chouette::JourneyPattern < Chouette::TridentActiveRecord has_many :route_sections, through: :journey_pattern_sections, dependent: :destroy validates_presence_of :route + validates_presence_of :name enum section_status: { todo: 0, completed: 1, control: 2 } diff --git a/app/views/api/v1/journey_patterns/show.rabl b/app/views/api/v1/journey_patterns/show.rabl index 882660360..808808462 100644 --- a/app/views/api/v1/journey_patterns/show.rabl +++ b/app/views/api/v1/journey_patterns/show.rabl @@ -15,7 +15,7 @@ end unless root_object.vehicle_journeys.empty? child :stop_points => :stop_area_short_descriptions do |stop_points| node do |stop_point| - partial("api/v1/stop_areas/short_description", :object => stop_point.stop_area) - end -end unless root_object.stop_points.empty? + partial("api/v1/stop_areas/short_description", :object => stop_point.stop_area) + end +end diff --git a/spec/javascripts/journey_patterns/reducers/modal_spec.js b/spec/javascripts/journey_patterns/reducers/modal_spec.js index ee83cb251..46ab2d905 100644 --- a/spec/javascripts/journey_patterns/reducers/modal_spec.js +++ b/spec/javascripts/journey_patterns/reducers/modal_spec.js @@ -11,7 +11,8 @@ let fakeJourneyPattern = { deletable: false } -const accept = cancel = function(){} +const accept = function(){} +const cancel = function(){} describe('modal reducer', () => { beforeEach(() => { |
