Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LPS-144391 search-experiences-web: Return full element JSON when clicking on 'View Element JSON' #379

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -24,7 +24,7 @@ import {DEFAULT_SXP_ELEMENT_ICON} from '../../utils/data';
import {INPUT_TYPES} from '../../utils/inputTypes';
import {
cleanUIConfiguration,
getConfigurationEntry,
getSXPElementJSON,
isDefined,
} from '../../utils/utils';
import {PreviewModalWithCopyDownload} from '../PreviewModal';
Expand Down Expand Up @@ -355,10 +355,10 @@ function SXPElement({
fileName="sxpElement.json"
size="lg"
text={JSON.stringify(
getConfigurationEntry({
getSXPElementJSON(
sxpElement,
uiConfigurationValues,
}),
uiConfigurationValues
),
null,
'\t'
)}
Expand Down
Expand Up @@ -252,97 +252,6 @@ export function cleanUIConfiguration(uiConfiguration = {}) {
return {fieldSets};
}

/**
* Function for retrieving a valid default value from one element
* configuration entry. Returns the proper empty value for invalid values.
*
* Examples:
* getDefaultValue({
* defaultValue: 10,
* label: 'Title Boost',
* name: 'boost',
* type: 'slider',
* })
* => 10
*
* getDefaultValue({
* label: 'Enabled',
* name: 'enabled',
* type: 'select',
* typeOptions: {
* options: [
* {
* label: 'True',
* value: true,
* },
* {
* label: 'False',
* value: false,
* },
* ],
* },
* })
* => true
*
* @param {object} item Configuration with label, name, type, defaultValue
* @return {(string|Array|number)}
*/
export function getDefaultValue(item) {
const itemValue = item.defaultValue;

switch (item.type) {
case INPUT_TYPES.DATE:
return typeof itemValue === 'number'
? itemValue
: moment(itemValue, ['MM-DD-YYYY', 'YYYY-MM-DD']).isValid()
? moment(itemValue, ['MM-DD-YYYY', 'YYYY-MM-DD']).unix()
: '';
case INPUT_TYPES.FIELD_MAPPING:
return typeof itemValue === 'object' && itemValue.field
? itemValue
: {
field: '',
locale: '',
};
case INPUT_TYPES.FIELD_MAPPING_LIST:
return Array.isArray(itemValue)
? itemValue.filter(({field}) => !!field) // Remove empty fields
: [];
case INPUT_TYPES.ITEM_SELECTOR:
return Array.isArray(itemValue)
? itemValue.filter((item) => item.label && item.value)
: [];
case INPUT_TYPES.JSON:
return typeof itemValue === 'object'
? JSON.stringify(itemValue, null, '\t')
: '{}';
case INPUT_TYPES.MULTISELECT:
return Array.isArray(itemValue)
? itemValue.filter((item) => item.label && item.value)
: [];
case INPUT_TYPES.NUMBER:
return typeof itemValue === 'number'
? itemValue
: typeof toNumber(itemValue) === 'number'
? toNumber(itemValue)
: '';
case INPUT_TYPES.SELECT:
return typeof itemValue === 'string'
? itemValue
: typeof item.typeOptions?.options?.[0]?.value === 'string'
? item.typeOptions.options[0].value
: '';
case INPUT_TYPES.SLIDER:
return typeof itemValue === 'number'
? itemValue
: typeof toNumber(itemValue) === 'number'
? toNumber(itemValue)
: '';
default:
return typeof itemValue === 'string' ? itemValue : '';
}
}

/**
* Function for replacing the ${variable_name} with actual value.
*
Expand Down Expand Up @@ -546,25 +455,124 @@ export function getConfigurationEntry({sxpElement, uiConfigurationValues}) {
}

/**
* Function for parsing custom json element text into sxpElement
* Function for retrieving a valid default value from one element
* configuration entry. Returns the proper empty value for invalid values.
*
* @param {object} sxpElement Original sxpElement (default)
* @param {object} uiConfigurationValues Contains custom JSON for sxpElement
* @return {object}
* Examples:
* getDefaultValue({
* defaultValue: 10,
* label: 'Title Boost',
* name: 'boost',
* type: 'slider',
* })
* => 10
*
* getDefaultValue({
* label: 'Enabled',
* name: 'enabled',
* type: 'select',
* typeOptions: {
* options: [
* {
* label: 'True',
* value: true,
* },
* {
* label: 'False',
* value: false,
* },
* ],
* },
* })
* => true
*
* @param {object} item Configuration with label, name, type, defaultValue
* @return {(string|Array|number)}
*/
export function parseCustomSXPElement(sxpElement, uiConfigurationValues) {
try {
if (isDefined(uiConfigurationValues.sxpElement)) {
return JSON.parse(uiConfigurationValues.sxpElement);
}
export function getDefaultValue(item) {
const itemValue = item.defaultValue;

return sxpElement;
}
catch {
return sxpElement;
switch (item.type) {
case INPUT_TYPES.DATE:
return typeof itemValue === 'number'
? itemValue
: moment(itemValue, ['MM-DD-YYYY', 'YYYY-MM-DD']).isValid()
? moment(itemValue, ['MM-DD-YYYY', 'YYYY-MM-DD']).unix()
: '';
case INPUT_TYPES.FIELD_MAPPING:
return typeof itemValue === 'object' && itemValue.field
? itemValue
: {
field: '',
locale: '',
};
case INPUT_TYPES.FIELD_MAPPING_LIST:
return Array.isArray(itemValue)
? itemValue.filter(({field}) => !!field) // Remove empty fields
: [];
case INPUT_TYPES.ITEM_SELECTOR:
return Array.isArray(itemValue)
? itemValue.filter((item) => item.label && item.value)
: [];
case INPUT_TYPES.JSON:
return typeof itemValue === 'object'
? JSON.stringify(itemValue, null, '\t')
: '{}';
case INPUT_TYPES.MULTISELECT:
return Array.isArray(itemValue)
? itemValue.filter((item) => item.label && item.value)
: [];
case INPUT_TYPES.NUMBER:
return typeof itemValue === 'number'
? itemValue
: typeof toNumber(itemValue) === 'number'
? toNumber(itemValue)
: '';
case INPUT_TYPES.SELECT:
return typeof itemValue === 'string'
? itemValue
: typeof item.typeOptions?.options?.[0]?.value === 'string'
? item.typeOptions.options[0].value
: '';
case INPUT_TYPES.SLIDER:
return typeof itemValue === 'number'
? itemValue
: typeof toNumber(itemValue) === 'number'
? toNumber(itemValue)
: '';
default:
return typeof itemValue === 'string' ? itemValue : '';
}
}

/**
* Function that provides the element JSON, with title, description, and elementDefinition.
* The elementDefinition's configuration is updated to have its variables replaced with
* values from uiConfigurationValues.
*
* @param {object} sxpElement SXP Element with title, description, elementDefinition
* @param {object} uiConfigurationValues Values that will replace the keys in uiConfiguration
* @return {object}
*/
export function getSXPElementJSON(sxpElement, uiConfigurationValues) {
const {description_i18n, elementDefinition, title_i18n} = sxpElement;

const {category, icon} = elementDefinition;

return {
description_i18n,
elementDefinition: {
category,
configuration: getConfigurationEntry({
sxpElement,
uiConfigurationValues,
}),
icon,
},
title_i18n,
};
}

/**
* Function for getting all the default values from an SXPElement. For non-custom
* json elements, returns the configuration values after looping over all fieldSets.
Expand Down Expand Up @@ -612,6 +620,26 @@ export function isCustomJSONSXPElement(uiConfigurationValues) {
return isDefined(uiConfigurationValues.sxpElement);
}

/**
* Function for parsing custom json element text into sxpElement
*
* @param {object} sxpElement Original sxpElement (default)
* @param {object} uiConfigurationValues Contains custom JSON for sxpElement
* @return {object}
*/
export function parseCustomSXPElement(sxpElement, uiConfigurationValues) {
try {
if (isDefined(uiConfigurationValues.sxpElement)) {
return JSON.parse(uiConfigurationValues.sxpElement);
}

return sxpElement;
}
catch {
return sxpElement;
}
}

/**
* Converts the attributes list to the format expected by the
* `searchContextAttributes` property.
Expand Down