Skip to content

Instantly share code, notes, and snippets.

@thomasgwatson
Created April 19, 2016 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasgwatson/51e5a502c00967361da75f4e434de533 to your computer and use it in GitHub Desktop.
Save thomasgwatson/51e5a502c00967361da75f4e434de533 to your computer and use it in GitHub Desktop.
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