Created
November 7, 2017 15:51
-
-
Save brunokunace/e1f2352753b57b47bd30ba84ca588e05 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
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