From 02e1fb37c042de2bc1176af534d294585f1504ad Mon Sep 17 00:00:00 2001 From: Maksym Mykhailenko Date: Thu, 7 Nov 2019 13:25:10 +0800 Subject: [PATCH] fix: set consistent default questions values If we don't set default question value it would be an empty string "" by default. This may cause errors while indexing in ES in case later empty string value is changed to other data type like "array", "object" or maybe even "number". Also, potentially, code which relies on the questions values may expect consistent data type, though I don't know any issues so far. At the moment this fix is only for "add-ons" question type. --- .../detail/components/SpecQuestions.jsx | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/projects/detail/components/SpecQuestions.jsx b/src/projects/detail/components/SpecQuestions.jsx index 3c86b8b57..2b961d59f 100644 --- a/src/projects/detail/components/SpecQuestions.jsx +++ b/src/projects/detail/components/SpecQuestions.jsx @@ -71,6 +71,31 @@ const buildAddonsOptions = (q, productTemplates, productCategories) => { ) } +/** + * Format the default value for question if it doesn't have the value. + * + * WARNING. This is quite important method and should be updated with caution. + * What is the default value could means a lot as other code may rely on it. + * + * So far I only added default value other than empty string for the `add-ons` as I tested + * it thoroughly and it needs it for proper work. + * + * Most likely it would be good to update default value for other question types which have + * a value type other than string, like `number`, `array`, `object` and so on. + * But before update any of them we have to test it extensively that nothing got broken because of that. + * + * @param {Object} question question + * + * @returns {Any} + */ +const formatDefaultQuestionValue = (question) => { + if (_.includes(['add-ons'], question.type)) { + return [] + } else { + return '' + } +} + // { isRequired, represents the overall questions section's compulsion, is also available} class SpecQuestions extends React.Component { constructor(props) { @@ -104,7 +129,7 @@ class SpecQuestions extends React.Component { const elemProps = { name: q.fieldName, label: q.label, - value: _.get(project, q.fieldName, ''), + value: _.get(project, q.fieldName, formatDefaultQuestionValue(q)), required: q.required, validations: q.required ? 'isRequired' : null, validationError: q.validationError,