Skip to content

Instantly share code, notes, and snippets.

@brunokunace
Created November 7, 2017 15:51
Show Gist options
  • Save brunokunace/e1f2352753b57b47bd30ba84ca588e05 to your computer and use it in GitHub Desktop.
Save brunokunace/e1f2352753b57b47bd30ba84ca588e05 to your computer and use it in GitHub Desktop.
import { get } from 'lodash'
/**
* @param schema
* @param data
* @returns {*}
*/
export const dotNotation = (schema, data) => {
const reduce = (accumulate, key) => {
accumulate[schema[key].field] = get(data, schema[key].field, undefined)
return accumulate
}
return Object.keys(schema).reduce(reduce, {})
}
/**
* @param {AppCrudForm} $component
* @param {AxiosResponse} response
* @param {Function} callback
*/
export default ($component, response, callback = null) => {
let body = response.data
if (!Array.isArray(body)) {
$component.data = dotNotation($component.fields, body)
return
}
$component.data = dotNotation($component.fields, body[0])
if (typeof callback === 'function') {
callback()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment