Created
October 2, 2017 19:55
-
-
Save mrinterweb/34ad8c50332200e88905c35cf127c1d1 to your computer and use it in GitHub Desktop.
A basic jsonapi serializer
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
import _ from 'lodash'; | |
export default { | |
UPDATE_RATES({ commit }, results) { | |
if (results.data && results.data.length === 0) { | |
commit('UPDATE_RATES', []); | |
} else { | |
let mapAtributes = function(_val, key) { | |
return _.camelCase(key); | |
}; | |
let rates = results.data.map((d) => { | |
let rate = _.mapKeys(d.attributes, mapAtributes); | |
rate.id = d.id; | |
rate.serviceProviderId = d.relationships['service-provider'].data.id; | |
let serviceProvider = _.find(results.included, { type: 'service-providers', id: rate.serviceProviderId }); | |
if (serviceProvider) { | |
rate = _.merge(rate, _.mapKeys(serviceProvider.attributes, mapAtributes)); | |
} | |
return rate; | |
}); | |
commit('UPDATE_RATES', rates); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment