Created
April 19, 2016 16:34
-
-
Save thomasgwatson/51e5a502c00967361da75f4e434de533 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const NEW_MESSAGES = 'vehicleMessages/NEW_MESSAGES' | |
const initialState = { | |
vehicles: {}, | |
traces: {}, | |
updateCounter: 1, | |
vehicleMessagesLoading: false, | |
} | |
export default function reducer (state = initialState, action = {}) { | |
switch (action.type) { | |
case NEW_MESSAGES: | |
return reduceVehicleMessagesToVehicles(state, action.payload) | |
case LOADING_MESSAGES: | |
return { | |
...state, | |
messagesLoading: true, | |
} | |
default: | |
return state | |
} | |
} | |
export function newMessages (messages) { | |
return {type: NEW_MESSAGES, payload: messages} | |
} | |
export function reduceVehicleMessagesToVehicles (state, vehicleMessages) { | |
if (vehicleMessages.length < 1) return state | |
let updatedTraces = {...state.traces} | |
let updatedVehicles = {...state.vehicles} | |
// Have also tried: | |
// let updatedTraces = Object.assign({}, state.traces) | |
// let updatedVehicles = Object.assign({}, state.vehicles) | |
for (let message of vehicleMessages) { | |
const source = message._source | |
const vehicleId = source.rig_id | |
const coords = [source.geo_pt.lon, source.geo_pt.lat] | |
const oldTrace = updatedTraces[vehicleId] | |
let updatedVehicle = { | |
rig_id: vehicleId, | |
type: 'vehicle', | |
coords: coords, | |
} | |
updatedVehicles[vehicleId] = updatedVehicle | |
let updatedTrace= { | |
rig_id: vehicleId, | |
type: 'trace', | |
} | |
updatedTrace.trace = oldTrace ? oldTrace.trace.concat([coords]) : [coords] | |
updatedTraces[vehicleId] = updatedTrace | |
} | |
let messagesLoading = true | |
return { | |
vehicles: updatedVehicles, | |
traces: updatedTraces, | |
updateCounter: state.updateCounter + 1, | |
vehicleMessagesLoading: messagesLoading, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment