UNPKG

@securecall/client-component

Version:

SecureCall Core Web Component

61 lines (60 loc) 1.91 kB
import { mergeDeep } from "../utils/utils"; export class ResponseFields { _defaults; _result; _fieldNames; constructor(defaults) { this._defaults = JSON.parse(JSON.stringify(defaults)); this._fieldNames = Object.keys(defaults); this._result = {}; } get defaults() { return this._defaults; } toJSON() { return this.calculatedFields(); } processResponse(data) { if (data === undefined || data === null) { return; } this._fieldNames.forEach(k => { const mapping = this._defaults[k].mapping || `response.${k}`; let maps = mapping.split('.'); const val = maps.reduce((obj, key) => obj?.[key], data); if (val !== undefined) { this._result[k] = { value: val }; } }); } get fieldNames() { return this._fieldNames; } asComponentChildren(success) { return Object.entries(this.calculatedFields()) .filter(([_k, v]) => { return (success) ? v.showOnSuccess : v.showOnFailure; }) .filter(([_k, v]) => { return (!(v.ignoreIfEmpty && (v.value === null || v.value === undefined || v.value === ''))); }) .sort(([_ak, av], [_bk, bv]) => { const orderA = av.order === undefined ? 1000 : av.order; const orderB = bv.order === undefined ? 1000 : bv.order; return orderA - orderB; }) .map(([k, v]) => { return { name: k, label: v.label, value: v.value, action: v.action, component: v.component || 'field', }; }); } calculatedFields() { return mergeDeep(this._defaults, this._result); } } //# sourceMappingURL=response_fields.js.map