Created mapper methods
This commit is contained in:
parent
9db1fe98df
commit
3cbc071bb4
1 changed files with 66 additions and 4 deletions
|
|
@ -9,17 +9,79 @@ export function fetchCmsContentForPage(fmgPage) {
|
|||
const pageDataFromCms = {};
|
||||
|
||||
response.data.Result.forEach((widget) => {
|
||||
if (Object.values(widgetNames).includes(widget.Type)) {
|
||||
|
||||
let widgetWithReplacements = findAndReplaceGlobalStateValues(widget.Model, widget.Type);
|
||||
|
||||
if (Object.values(widgetNames).includes(widgetWithReplacements.Type)) {
|
||||
// If we already have this widget, push it on the collection
|
||||
if (widget.Type in pageDataFromCms) {
|
||||
pageDataFromCms[widget.Type].push(widget.Model);
|
||||
if (widgetWithReplacements.Type in pageDataFromCms) {
|
||||
pageDataFromCms[widgetWithReplacements.Type].push(widgetWithReplacements.Model);
|
||||
return;
|
||||
}
|
||||
|
||||
pageDataFromCms[widget.Type] = [widget.Model];
|
||||
pageDataFromCms[widgetWithReplacements.Type] = [widgetWithReplacements.Model];
|
||||
}
|
||||
});
|
||||
|
||||
return pageDataFromCms;
|
||||
});
|
||||
}
|
||||
|
||||
function mapStringToState(str) {
|
||||
|
||||
// Pull the state string out of our dynamic string from the CMS
|
||||
const regexExp = new RegExp('{(.*?):(.*?)}', 'g');
|
||||
const matches = [...str.matchAll(regexExp)];
|
||||
const stateString = matches.map(m => m[2]).toString();
|
||||
|
||||
// Convert the state string to a state object
|
||||
let storeState = store.state;
|
||||
|
||||
for (const s of stateString.split('.')) {
|
||||
if (storeState[s] != undefined) {
|
||||
storeState = storeState[s];
|
||||
} else {
|
||||
return false; // If we can't find our state variable, break our loop. Don't break our code.
|
||||
}
|
||||
}
|
||||
|
||||
return storeState;
|
||||
}
|
||||
|
||||
function findAndReplaceGlobalStateValues(widgetModel, widgetType) {
|
||||
|
||||
const objWithReplacements = {
|
||||
Type: widgetType,
|
||||
Model: {}
|
||||
};
|
||||
|
||||
Object.keys(widgetModel).forEach(key => {
|
||||
|
||||
let modelWithReplacements = processWidgetItemForReplacement(widgetModel, key);
|
||||
|
||||
objWithReplacements.Model[key] = modelWithReplacements;
|
||||
});
|
||||
|
||||
return objWithReplacements;
|
||||
}
|
||||
|
||||
function processWidgetItemForReplacement(widgetModel, key) {
|
||||
// If we have a string, and it needs to be replaced.
|
||||
if (typeof widgetModel[key] === 'string') {
|
||||
if (widgetModel[key].includes('{globalState:')) {
|
||||
widgetModel[key] = mapStringToState(widgetModel[key]);
|
||||
}
|
||||
return widgetModel[key];
|
||||
}
|
||||
|
||||
// If we have an object. array, etc
|
||||
if (typeof widgetModel[key] === 'object' && Object.keys(widgetModel[key]).length) {
|
||||
Object.keys(widgetModel[key]).forEach(item => {
|
||||
processWidgetItemForReplacement(widgetModel[key], item);
|
||||
});
|
||||
|
||||
return widgetModel[key];
|
||||
}
|
||||
|
||||
return widgetModel[key];
|
||||
}
|
||||
Loading…
Reference in a new issue