Some amazing article goes here!
function mergeActions(newActions, stateActions) {
const stateLookup = stateActions.records.reduce((lookup, action) => {
lookup[action.changeset] = action // eslint-disable-line no-param-reassign
return lookup
}, {})
const mergedActions = []
// Track if any actions have been merged
let hasChanged = false
newActions.records.forEach(na => {
const subchangesetIds = na.change && na.change.subchangeset_ids
// Tracks if the current actions has been replaced with a more detailed
// version from the state
let hasMerged = false
if (subchangesetIds && subchangesetIds.length > 0) {
subchangesetIds.forEach(cid => {
if (stateLookup[cid]) {
mergedActions.push(stateLookup[cid])
hasMerged = true
hasChanged = true
}
})
}
if (!hasMerged) mergedActions.push(na)
})
if (hasChanged) return { records: mergedActions }
return newActions
}