aboutsummaryrefslogtreecommitdiffstats
path: root/app/javascript/routes/reducers/stopPoints.js
blob: ba183d002ad39093543fe1e328860d4be64b7f85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import _ from 'lodash'
import formHelper from '../form_helper'

const stopPoint = (state = {}, action, length) => {
  switch (action.type) {
    case 'ADD_STOP':
      return {
        text: '',
        index: length,
        edit: true,
        for_boarding: '',
        for_alighting: '',
        olMap: {
          isOpened: false,
          json: {}
        }
      }
    default:
      return state
  }
}

const updateFormForDeletion = (stop) =>{
  if (stop.stoppoint_id !== undefined){
    let now = Date.now()
    formHelper.addInput('id', stop.stoppoint_id, now)
    formHelper.addInput('_destroy', 'true', now)
  }
}

const stopPoints = (state = [], action) => {
  switch (action.type) {
    case 'ADD_STOP':
      return [
        ...state,
        stopPoint(undefined, action, state.length)
      ]
    case 'MOVE_STOP_UP':
      return [
        ...state.slice(0, action.index - 1),
        _.assign({}, state[action.index], { index: action.index - 1 }),
        _.assign({}, state[action.index - 1], { index: action.index }),
        ...state.slice(action.index + 1)
      ]
    case 'MOVE_STOP_DOWN':
      return [
        ...state.slice(0, action.index),
        _.assign({}, state[action.index + 1], { index: action.index }),
        _.assign({}, state[action.index], { index: action.index + 1 }),
        ...state.slice(action.index + 2)
      ]
    case 'DELETE_STOP':
      updateFormForDeletion(state[action.index])
      return [
        ...state.slice(0, action.index),
        ...state.slice(action.index + 1).map((stopPoint)=>{
          stopPoint.index--
          return stopPoint
        })
      ]
    case 'UPDATE_INPUT_VALUE':
      return state.map( (t, i) => {
        if (i === action.index) {
          return _.assign(
            {},
            t,
            {
              stoppoint_id: t.stoppoint_id,
              text: action.text.text,
              stoparea_id: action.text.stoparea_id,
              stoparea_kind: action.text.stoparea_kind,
              user_objectid: action.text.user_objectid,
              latitude: action.text.latitude,
              longitude: action.text.longitude,
              name: action.text.name,
              short_name: action.text.short_name,
              area_type: action.text.area_type,
              city_name: action.text.city_name,
              comment: action.text.comment,
              registration_number: action.text.registration_number
            }
          )
        } else {
          return t
        }
      })
    case 'UPDATE_SELECT_VALUE':
      return state.map( (t, i) => {
        if (i === action.index) {
          let stopState = _.assign({}, t)
          stopState[action.select_id] = action.select_value
          return stopState
        } else {
          return t
        }
      })
    case 'TOGGLE_EDIT':
      return state.map((t, i) => {
        if (i === action.index){
          return _.assign({}, t, {edit: !t.edit})
        } else {
          return t
        }
      })
    case 'TOGGLE_MAP':
      return state.map( (t, i) => {
        if (i === action.index){
          let val = !t.olMap.isOpened
          let jsonData = val ? _.assign({}, t, {olMap: undefined}) : {}
          let stateMap = _.assign({}, t.olMap, {isOpened: val, json: jsonData})
          return _.assign({}, t, {olMap: stateMap})
        }else {
          let emptyMap = _.assign({}, t.olMap, {isOpened: false, json : {}})
          return _.assign({}, t, {olMap: emptyMap})
        }
      })
    case 'SELECT_MARKER':
      return state.map((t, i) => {
        if (i === action.index){
          let stateMap = _.assign({}, t.olMap, {json: action.data})
          return _.assign({}, t, {olMap: stateMap})
        } else {
          return t
        }
      })
    case 'UNSELECT_MARKER':
      return state.map((t, i) => {
        if (i === action.index){
          let stateMap = _.assign({}, t.olMap, {json: {}})
          return _.assign({}, t, {olMap: stateMap})
        } else {
          return t
        }
      })
    case 'CLOSE_MAP':
      return state.map( (t, i) => {
        let emptyMap = _.assign({}, t.olMap, {isOpened: false, json: {}})
        return _.assign({}, t, {olMap: emptyMap})
      })
    default:
      return state
  }
}

export default stopPoints