ice.fo.utils
Version:
130 lines (110 loc) • 3.48 kB
JavaScript
import _set from 'lodash/set'
import { convertToQueryObject, formatStringValue } from 'ice.fo.utils/StringUtils'
import { get, getValue } from 'ice.fo.utils/GetterUtils'
import { makeSvcAPI } from 'ice.fo.utils/BuilderUtils'
import { parseDataMap } from 'ice.fo.utils/ParseUtils'
/**
* Map a list of items with new properties path changes.
*
* @example
* [{ name: 'nikochin', age: 10 }, { name: 'steve', age: 20 }]; // return [{ fullName: 'nikochin', legalAge: 10 }, { fullName: 'steve', legalAge: 20 }]
*
* @param {object} dataMapConfig
* @param {object[]} items
* @param {VueContext} context
* @returns
*/
export function mapData (dataMapConfig, items, context = { strict: false }) {
const parseValues = context
const mapPaths = (dataMapConfig.list || dataMapConfig.binding || []).filter(i => i.toField && (i.fromField || i.format))
const result = items.filter(i => i).map((i) => {
const resultItem = {
...(context.strict ? {} : i),
key: `${i.id || Math.random()}_${get(context, '$i18n.locale', '')}`,
ranKey: Math.random(),
id: i.id,
get item () {
return this
},
get data () {
const result = {}
for (const k in this) {
if (!['id', 'key', 'ranKey', 'item', 'data'].includes(k)) {
result[k] = this[k]
}
}
return result
},
}
const data = {
...resultItem,
...parseValues,
...i,
}
mapPaths.forEach(({ fromField, toField, format }) => {
fromField = (fromField || '').trim()
format = getValue(format).trim().split('>').pop()
_set(resultItem, toField, parseDataMap({ data, from: fromField, format }))
})
return resultItem
})
return result
}
/**
* Fetch items by API Binnding Connfig
*
* @param {object} dataMapConfig
* @param {object} params
* @param {{ $axios }} context
* @returns
*/
export async function fetchDataMap (dataMapConfig, params = {}, context = {}) {
const { $axios } = context
dataMapConfig = dataMapConfig && (dataMapConfig.item || dataMapConfig)
if (!dataMapConfig || !(dataMapConfig.api || dataMapConfig.baseApi)) {
return null
}
const parseValues = context
// call replated apis
if (dataMapConfig.relatedApis && dataMapConfig.relatedApis.length) {
for (const item of dataMapConfig.relatedApis) {
try {
const relatedResult = await fetchDataMap(item.relatedApi.item, params, parseValues)
_set(parseValues, item.name, relatedResult)
} catch (error) {
console.error(error)
}
}
}
const { data } = await $axios.request({
url: formatStringValue(parseValues, getValue(dataMapConfig.api) || makeSvcAPI(dataMapConfig.baseApi)),
method: 'get', // fetch data to display only support GET
params: {
...params,
...convertToQueryObject({ text: dataMapConfig.apiParams, data: parseValues }),
},
})
if (!data.currentPage) {
data.currentPage = params.page
data.pageSize = params.pageSize
}
if (!data.pageCount && data.totalCount != null) {
data.pageCount = Math.ceil(data.totalCount / data.pageSize)
}
const item = data.item
const items = data.items
const result = mapData(dataMapConfig, items || [item])
if (items) {
return {
...data,
values: result,
canFetchMore: data.currentPage < data.pageCount,
}
} else if (item) {
return {
...data,
value: result[0],
}
}
return null
}