mod-build 4.0.100-beta.2 → 4.0.100-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/package.json +1 -1
- package/tasks/get-default-trade-questions.js +112 -160
package/CHANGELOG.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
## 4.0.100
|
|
4
4
|
|
|
5
|
-
-
|
|
5
|
+
- Refactored `get-default-trade-questions.js` to reduce duplicated logic in `lead-form` and enable reuse across instances.
|
|
6
6
|
|
|
7
7
|
## 4.0.99
|
|
8
8
|
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import merge from 'lodash.merge';
|
|
3
3
|
import { defaultSettings } from '../src/data/config.js';
|
|
4
4
|
|
|
5
|
-
function resolveRefs(value, root, refChain = new Set()) {
|
|
5
|
+
export function resolveRefs(value, root = value, refChain = new Set()) {
|
|
6
6
|
if (value === null || typeof value !== 'object') {
|
|
7
7
|
return value;
|
|
8
8
|
}
|
|
@@ -33,187 +33,139 @@ function resolveRefs(value, root, refChain = new Set()) {
|
|
|
33
33
|
return resolved;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
function
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
36
|
+
function mergeQuestionFields(fields, defaultFields, {
|
|
37
|
+
legendAttributes,
|
|
38
|
+
optionFieldTypes,
|
|
39
|
+
errorMessages
|
|
40
|
+
}) {
|
|
41
|
+
let defaultOptions = [];
|
|
42
|
+
let defaultLegend = {};
|
|
43
|
+
defaultFields.forEach((defaultField) => {
|
|
44
|
+
defaultOptions = defaultField.options;
|
|
45
|
+
defaultLegend = defaultField.legend;
|
|
46
|
+
|
|
47
|
+
if (defaultLegend && legendAttributes) {
|
|
48
|
+
defaultLegend.attributes = {
|
|
49
|
+
...defaultLegend.attributes,
|
|
50
|
+
...legendAttributes
|
|
51
|
+
};
|
|
51
52
|
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function mergeDefaultTradeQuestionsConfig(config, steps) {
|
|
56
|
-
if (!config || !config.steps || !config.steps.items || !Array.isArray(config.steps.items)) {
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
53
|
+
});
|
|
59
54
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
55
|
+
fields.forEach((field) => {
|
|
56
|
+
if (field.legend) {
|
|
57
|
+
field.legend = merge(defaultLegend, field.legend);
|
|
58
|
+
} else {
|
|
59
|
+
field.legend = defaultLegend;
|
|
60
|
+
}
|
|
66
61
|
|
|
67
|
-
|
|
68
|
-
|
|
62
|
+
if (field.remove) {
|
|
63
|
+
if (field.remove.includes('all')) {
|
|
64
|
+
defaultOptions = [];
|
|
65
|
+
} else {
|
|
66
|
+
defaultOptions = defaultOptions.filter((_, index) => !field.remove.includes(index));
|
|
69
67
|
}
|
|
70
|
-
|
|
71
|
-
let defaultStepOptions = [];
|
|
72
|
-
let defaultQuestionLegend = {};
|
|
73
|
-
defaultStepFields.forEach((defaultField) => {
|
|
74
|
-
defaultStepOptions = defaultField.options;
|
|
75
|
-
defaultQuestionLegend = defaultField.legend;
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
item.stepContent.fields.forEach((field) => {
|
|
79
|
-
if (field.legend) {
|
|
80
|
-
field.legend = merge(defaultQuestionLegend, field.legend);
|
|
81
|
-
} else {
|
|
82
|
-
field.legend = defaultQuestionLegend;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (field && field.remove) {
|
|
86
|
-
if (field.remove.includes('all')) {
|
|
87
|
-
defaultStepOptions = [];
|
|
88
|
-
} else {
|
|
89
|
-
defaultStepOptions = defaultStepOptions.filter((_, i) => !field.remove.includes(i));
|
|
90
|
-
}
|
|
91
|
-
}
|
|
68
|
+
}
|
|
92
69
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
field.move.forEach((move) => {
|
|
97
|
-
const fromIndex = move.from;
|
|
98
|
-
const toIndex = move.to;
|
|
99
|
-
const fromOption = defaultStepOptions.splice(fromIndex, 1);
|
|
100
|
-
defaultStepOptions.splice(toIndex, 0, fromOption[0]);
|
|
101
|
-
console.log(JSON.parse(JSON.stringify(defaultStepOptions)));
|
|
102
|
-
});
|
|
103
|
-
}
|
|
70
|
+
if (!optionFieldTypes.includes(field.fieldType)) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
104
73
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
}
|
|
74
|
+
if (!field.errorMessage && errorMessages[field.fieldType]) {
|
|
75
|
+
field.errorMessage = errorMessages[field.fieldType];
|
|
76
|
+
}
|
|
109
77
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
}
|
|
117
|
-
merge(defaultStepOptions[option.update], option);
|
|
118
|
-
} else {
|
|
119
|
-
if (typeof option.insertAt === 'number') {
|
|
120
|
-
defaultStepOptions.splice(option.insertAt, 0, option);
|
|
121
|
-
} else {
|
|
122
|
-
defaultStepOptions.push(option);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
});
|
|
78
|
+
if (Array.isArray(field.options)) {
|
|
79
|
+
field.options.forEach((option) => {
|
|
80
|
+
if (typeof option.update === 'number') {
|
|
81
|
+
if (option.attributes?.checked === false) {
|
|
82
|
+
delete option.attributes.checked;
|
|
83
|
+
delete defaultOptions[option.update]?.attributes?.checked;
|
|
126
84
|
}
|
|
127
|
-
|
|
85
|
+
merge(defaultOptions[option.update], option);
|
|
86
|
+
} else if (typeof option.insertAt === 'number') {
|
|
87
|
+
defaultOptions.splice(option.insertAt, 0, option);
|
|
128
88
|
} else {
|
|
129
|
-
|
|
130
|
-
if (typeof field.update === 'number') {
|
|
131
|
-
merge(defaultStepFields[field.update], field);
|
|
132
|
-
} else {
|
|
133
|
-
if (typeof field.insertAt === 'number') {
|
|
134
|
-
defaultStepFields.splice(field.insertAt, 0, field);
|
|
135
|
-
} else {
|
|
136
|
-
defaultStepFields.push(field);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
field = defaultStepFields;
|
|
89
|
+
defaultOptions.push(option);
|
|
142
90
|
}
|
|
143
91
|
});
|
|
92
|
+
}
|
|
93
|
+
field.options = defaultOptions;
|
|
94
|
+
});
|
|
95
|
+
}
|
|
144
96
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
97
|
+
export function mergeDefaultTradeQuestionsIntoItems(items, steps, options = {}) {
|
|
98
|
+
if (!Array.isArray(items) || !steps || typeof steps !== 'object') {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
150
101
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
102
|
+
const mergeOptions = {
|
|
103
|
+
legendAttributes: options.legendAttributes,
|
|
104
|
+
optionFieldTypes: options.optionFieldTypes || ['radio', 'checkbox-group'],
|
|
105
|
+
errorMessages: {
|
|
106
|
+
radio: 'Professionals need this info to generate a quote.',
|
|
107
|
+
'checkbox-group': 'Please select at least one option.',
|
|
108
|
+
...options.errorMessages
|
|
109
|
+
}
|
|
110
|
+
};
|
|
159
111
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
112
|
+
items.forEach((item) => {
|
|
113
|
+
const stepName = item.attributes?.data?.['step-name'];
|
|
114
|
+
const defaultStep = steps[stepName];
|
|
115
|
+
if (!defaultStep) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
165
118
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
}
|
|
119
|
+
if (!item.stepContent) {
|
|
120
|
+
item.stepContent = {};
|
|
121
|
+
}
|
|
122
|
+
if (!item.stepContent.fields) {
|
|
123
|
+
item.stepContent.fields = [];
|
|
124
|
+
}
|
|
173
125
|
|
|
174
|
-
|
|
175
|
-
if (!field.errorMessage) {
|
|
176
|
-
field.errorMessage = 'Professionals need this info to generate a quote.';
|
|
177
|
-
}
|
|
126
|
+
mergeQuestionFields(item.stepContent.fields, defaultStep.fields, mergeOptions);
|
|
178
127
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
delete option.attributes.checked;
|
|
184
|
-
delete defaultStepOptions[option.update].attributes.checked;
|
|
185
|
-
}
|
|
186
|
-
merge(defaultExtraQuestionOptions[option.update], option);
|
|
187
|
-
} else {
|
|
188
|
-
if (typeof option.insertAt === 'number') {
|
|
189
|
-
defaultExtraQuestionOptions.splice(option.insertAt, 0, option);
|
|
190
|
-
} else {
|
|
191
|
-
defaultExtraQuestionOptions.push(option);
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
field.options = defaultExtraQuestionOptions;
|
|
197
|
-
} else {
|
|
198
|
-
if (field && Array.isArray(field) && field.length) {
|
|
199
|
-
if (typeof field.update === 'number') {
|
|
200
|
-
merge(defaultExtraQuestionFields[field.update], field);
|
|
201
|
-
} else {
|
|
202
|
-
if (typeof field.insertAt === 'number') {
|
|
203
|
-
defaultExtraQuestionFields.splice(field.insertAt, 0, field);
|
|
204
|
-
} else {
|
|
205
|
-
defaultExtraQuestionFields.push(field);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
field = defaultExtraQuestionFields;
|
|
210
|
-
}
|
|
211
|
-
});
|
|
128
|
+
const extraQuestionBlock = item.stepContent.extraQuestionBlock;
|
|
129
|
+
if (extraQuestionBlock) {
|
|
130
|
+
if (!extraQuestionBlock.fields) {
|
|
131
|
+
extraQuestionBlock.fields = [];
|
|
212
132
|
}
|
|
133
|
+
mergeQuestionFields(
|
|
134
|
+
extraQuestionBlock.fields,
|
|
135
|
+
defaultStep.extraQuestionBlock.fields,
|
|
136
|
+
mergeOptions
|
|
137
|
+
);
|
|
213
138
|
}
|
|
214
139
|
});
|
|
215
140
|
}
|
|
216
141
|
|
|
142
|
+
export function mergeDefaultTradeQuestionsConfigOnSteps(config, steps, options) {
|
|
143
|
+
if (!config || typeof config !== 'object') return;
|
|
144
|
+
|
|
145
|
+
if (config.steps && typeof config.steps === 'object' && config.steps.items) {
|
|
146
|
+
// cloning steps to keep multiple steps: {} objects independent
|
|
147
|
+
const clonedSteps = JSON.parse(JSON.stringify(steps));
|
|
148
|
+
mergeDefaultTradeQuestionsConfig(config, clonedSteps, options);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
for (const key in config) {
|
|
152
|
+
if (config.hasOwnProperty(key)) {
|
|
153
|
+
const value = config[key];
|
|
154
|
+
if (typeof value === 'object' && value !== null) {
|
|
155
|
+
mergeDefaultTradeQuestionsConfigOnSteps(value, steps, options);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function mergeDefaultTradeQuestionsConfig(config, steps, options) {
|
|
162
|
+
if (!config || !config.steps || !config.steps.items || !Array.isArray(config.steps.items)) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
mergeDefaultTradeQuestionsIntoItems(config.steps.items, steps, options);
|
|
167
|
+
}
|
|
168
|
+
|
|
217
169
|
export default async function(config) {
|
|
218
170
|
if (config.useStepsConfig) {
|
|
219
171
|
console.log('Starting get-default-trade-questions...');
|