diff options
| author | cedricnjanga | 2017-10-06 10:17:17 +0200 | 
|---|---|---|
| committer | cedricnjanga | 2017-10-06 10:17:17 +0200 | 
| commit | b6f08e58fae35d5dd8a610af31c2950b37746695 (patch) | |
| tree | 989843dd674c41ff73eb75bd630ce4cc91fff91b /app/javascript/journey_patterns/reducers | |
| parent | 08517c27551a2dd8b227f6662f4c41574a36d81e (diff) | |
| download | chouette-core-b6f08e58fae35d5dd8a610af31c2950b37746695.tar.bz2 | |
Add webpacker gem and migrate the React apps
Diffstat (limited to 'app/javascript/journey_patterns/reducers')
7 files changed, 221 insertions, 0 deletions
diff --git a/app/javascript/journey_patterns/reducers/editMode.js b/app/javascript/journey_patterns/reducers/editMode.js new file mode 100644 index 000000000..bff976804 --- /dev/null +++ b/app/javascript/journey_patterns/reducers/editMode.js @@ -0,0 +1,10 @@ +export default function editMode(state = {}, action ) { +  switch (action.type) { +    case "ENTER_EDIT_MODE": +      return true +    case "EXIT_EDIT_MODE": +      return false +    default: +      return state +  } +}
\ No newline at end of file diff --git a/app/javascript/journey_patterns/reducers/index.js b/app/javascript/journey_patterns/reducers/index.js new file mode 100644 index 000000000..2ffaf86d4 --- /dev/null +++ b/app/javascript/journey_patterns/reducers/index.js @@ -0,0 +1,18 @@ +import { combineReducers } from 'redux' +import editMode from './editMode' +import status from './status' +import journeyPatterns from './journeyPatterns' +import pagination from './pagination' +import modal from './modal' +import stopPointsList from './stopPointsList' + +const journeyPatternsApp = combineReducers({ +  editMode, +  status, +  journeyPatterns, +  pagination, +  stopPointsList, +  modal +}) + +export default journeyPatternsApp diff --git a/app/javascript/journey_patterns/reducers/journeyPatterns.js b/app/javascript/journey_patterns/reducers/journeyPatterns.js new file mode 100644 index 000000000..7702e21bc --- /dev/null +++ b/app/javascript/journey_patterns/reducers/journeyPatterns.js @@ -0,0 +1,90 @@ +import _ from 'lodash' +import actions from "../actions" + +export default function journeyPattern(state = {}, action) { +  switch (action.type) { +    case 'ADD_JOURNEYPATTERN': +      let stopPoints = window.stopPoints + +      if(stopPoints != undefined) { +        stopPoints.map((s)=>{ +          s.checked = false +          return s +        }) +      } +      return { +        name: action.data.name.value, +        published_name: action.data.published_name.value, +        registration_number: action.data.registration_number.value, +        stop_points: stopPoints, +        deletable: false +      } +    case 'UPDATE_CHECKBOX_VALUE': +      var updatedStopPoints = state.stop_points.map((s) => { +        if (String(s.id) == action.id) { +          return _.assign({}, s, {checked : !s.checked}) +        }else { +          return s +        } +      }) +      return _.assign({}, state, {stop_points: updatedStopPoints}) +    default: +      return state +  } +} + +const journeyPatterns = (state = [], action) => { +  switch (action.type) { +    case 'RECEIVE_JOURNEY_PATTERNS': +      return [...action.json] +    case 'RECEIVE_ERRORS': +      return [...action.json] +    case 'GO_TO_PREVIOUS_PAGE': +      $('#ConfirmModal').modal('hide') +      if(action.pagination.page > 1){ +        actions.fetchJourneyPatterns(action.dispatch, action.pagination.page, action.nextPage) +      } +      return state +    case 'GO_TO_NEXT_PAGE': +      $('#ConfirmModal').modal('hide') +      if (action.pagination.totalCount - (action.pagination.page * action.pagination.perPage) > 0){ +        actions.fetchJourneyPatterns(action.dispatch, action.pagination.page, action.nextPage) +      } +      return state +    case 'UPDATE_CHECKBOX_VALUE': +      return state.map((j, i) =>{ +        if(i == action.index) { +          return journeyPattern(j, action) +        } else { +          return j +        } +      }) +    case 'DELETE_JOURNEYPATTERN': +      return state.map((j, i) =>{ +        if(i == action.index) { +          return _.assign({}, j, {deletable: true}) +        } else { +          return j +        } +      }) +    case 'ADD_JOURNEYPATTERN': +      return [ +        journeyPattern(state, action), +        ...state +      ] +    case 'SAVE_MODAL': +      return state.map((j, i) =>{ +        if(i == action.index) { +          return _.assign({}, j, { +            name: action.data.name.value, +            published_name: action.data.published_name.value, +            registration_number: action.data.registration_number.value +          }) +        } else { +          return j +        } +      }) +    default: +      return state +  } +}
\ No newline at end of file diff --git a/app/javascript/journey_patterns/reducers/modal.js b/app/javascript/journey_patterns/reducers/modal.js new file mode 100644 index 000000000..0a96f1679 --- /dev/null +++ b/app/javascript/journey_patterns/reducers/modal.js @@ -0,0 +1,41 @@ +import _ from 'lodash' + +export default function modal(state = {}, action) { +  switch (action.type) { +    case 'OPEN_CONFIRM_MODAL': +      $('#ConfirmModal').modal('show') +      return _.assign({}, state, { +        type: 'confirm', +        confirmModal: { +          callback: action.callback, +        } +      }) +    case 'EDIT_JOURNEYPATTERN_MODAL': +      return { +        type: 'edit', +        modalProps: { +          index: action.index, +          journeyPattern: action.journeyPattern +        }, +        confirmModal: {} +      } +    case 'CREATE_JOURNEYPATTERN_MODAL': +      return { +        type: 'create', +        modalProps: {}, +        confirmModal: {} +      } +    case 'DELETE_JOURNEYPATTERN': +      return _.assign({}, state, { type: '' }) +    case 'SAVE_MODAL': +      return _.assign({}, state, { type: '' }) +    case 'CLOSE_MODAL': +      return { +        type: '', +        modalProps: {}, +        confirmModal: {} +      } +    default: +      return state +  } +}
\ No newline at end of file diff --git a/app/javascript/journey_patterns/reducers/pagination.js b/app/javascript/journey_patterns/reducers/pagination.js new file mode 100644 index 000000000..01fdf21d4 --- /dev/null +++ b/app/javascript/journey_patterns/reducers/pagination.js @@ -0,0 +1,35 @@ +import _ from 'lodash' + +export default function pagination (state = {}, action) { +  switch (action.type) { +    case 'RECEIVE_JOURNEY_PATTERNS': +      return _.assign({}, state, {stateChanged: false}) +    case 'GO_TO_PREVIOUS_PAGE': +      if (action.pagination.page > 1){ +        toggleOnConfirmModal() +        return _.assign({}, state, {page : action.pagination.page - 1, stateChanged: false}) +      } +      return state +    case 'GO_TO_NEXT_PAGE': +      if (state.totalCount - (action.pagination.page * action.pagination.perPage) > 0){ +        toggleOnConfirmModal() +        return _.assign({}, state, {page : action.pagination.page + 1, stateChanged: false}) +      } +      return state +    case 'UPDATE_CHECKBOX_VALUE': +    case 'ADD_JOURNEYPATTERN': +    case 'SAVE_MODAL': +      toggleOnConfirmModal('modal') +      return _.assign({}, state, {stateChanged: true}) +    case 'UPDATE_TOTAL_COUNT': +      return _.assign({}, state, {totalCount : state.totalCount - action.diff }) +    default: +      return state +  } +} + +const toggleOnConfirmModal = (arg = '') =>{ +  $('.confirm').each(function(){ +    $(this).data('toggle','') +  }) +}
\ No newline at end of file diff --git a/app/javascript/journey_patterns/reducers/status.js b/app/javascript/journey_patterns/reducers/status.js new file mode 100644 index 000000000..88c75966d --- /dev/null +++ b/app/javascript/journey_patterns/reducers/status.js @@ -0,0 +1,21 @@ +import _ from 'lodash' +import actions from '../actions' + +export default function status (state = {}, action) { +  switch (action.type) { +    case 'UNAVAILABLE_SERVER': +      return _.assign({}, state, {fetchSuccess: false}) +    case 'FETCH_API': +      return _.assign({}, state, {isFetching: true}) +    case 'RECEIVE_JOURNEY_PATTERNS': +      return _.assign({}, state, {fetchSuccess: true, isFetching: false}) +    case 'RECEIVE_ERRORS': +      return _.assign({}, state, {isFetching: false}) +    case 'ENTER_EDIT_MODE': +      return _.assign({}, state, {editMode: true}) +    case 'EXIT_EDIT_MODE': +      return _.assign({}, state, {editMode: false}) +    default: +      return state +  } +}
\ No newline at end of file diff --git a/app/javascript/journey_patterns/reducers/stopPointsList.js b/app/javascript/journey_patterns/reducers/stopPointsList.js new file mode 100644 index 000000000..ee5eb1a80 --- /dev/null +++ b/app/javascript/journey_patterns/reducers/stopPointsList.js @@ -0,0 +1,6 @@ +export default function stopPointsList (state = [], action) { +  switch (action.type) { +    default: +      return state +  } +}
\ No newline at end of file  | 
