Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 386x 40x 386x 262x 262x 262x 262x 262x 262x 262x 262x 346x 84x 84x 99x 99x 99x 99x 99x 99x 101x 101x 101x 49x 49x 52x 52x 52x 101x 52x 101x 99x 99x 99x 99x 99x 99x 491x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 2x 12x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 5x 5x 14x 14x 14x 10x 10x 14x 14x 14x 14x 491x 99x 99x 99x 99x 99x 37x 37x 37x 37x 99x 99x 99x 99x 99x 37x 37x 37x 37x 99x 99x 99x 99x 99x 99x 37x 37x 37x 37x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 982x 982x 982x 982x 982x 982x 37x 37x 37x 982x 982x 982x 982x 982x 982x 982x 982x 982x 982x 982x 982x 982x 982x 982x 982x 982x 982x 982x 982x 982x 982x 982x 982x 982x 982x 982x 982x 982x 982x 99x 99x 99x 99x 99x 99x 58x 58x 56x 56x 56x 56x 56x 54x 56x 54x 54x 56x 54x 56x 43x 41x 11x 11x 11x 5x 5x 5x 5x 5x 58x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 41x 41x 50x 9x 9x 9x 3x 9x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 58x 99x 67x 67x 67x 59x 59x 59x 67x 59x 59x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 27x 27x 24x 27x 3x 3x 3x 3x 3x 3x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 59x 67x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x 116x 116x 116x 116x 116x 116x 116x 99x 99x 1230x 1230x 1230x 1230x 1230x 99x 116x 116x 99x 99x 116x 130x 99x 99x 99x | import {
getOperatorConfig,
getWaitForResponseOperators,
operatorsToSelectOptions
} from '../operators';
import { generateDefaultCategoryName } from '../../utils';
import { isSystemCategory } from '../categoryUtils';
import { FormData } from '../types';
import { zustand } from '../../store/AppState';
/**
* Shared helper function to get operator value from various formats.
* Handles string, object, and array formats that can come from the form system.
*/
export const getOperatorValue = (operator: any): string => {
if (typeof operator === 'string') {
return operator.trim();
} else if (Array.isArray(operator) && operator.length > 0) {
const firstOperator = operator[0];
if (
firstOperator &&
typeof firstOperator === 'object' &&
firstOperator.value
) {
return firstOperator.value.trim();
}
} else if (operator && typeof operator === 'object' && operator.value) {
return operator.value.trim();
}
return '';
};
/**
* Shared isEmptyItem function for rules array.
* Determines if a rule item is considered empty and should be filtered out.
*/
export const isEmptyRuleItem = (item: any): boolean => {
// Check if operator and category are provided
const operatorValue = getOperatorValue(item.operator);
if (!operatorValue || !item.category || item.category.trim() === '') {
return true;
}
// Check if value is required based on operator configuration
const operatorConfig = getOperatorConfig(operatorValue);
if (operatorConfig && operatorConfig.operands === 1) {
return !item.value1 || item.value1.trim() === '';
} else if (operatorConfig && operatorConfig.operands === 2) {
return (
!item.value1 ||
item.value1.trim() === '' ||
!item.value2 ||
item.value2.trim() === ''
);
}
return false;
};
/**
* Shared onItemChange function for rules array.
* Handles auto-updating category names based on operator and value changes.
*/
export const createRuleItemChangeHandler = () => {
return (itemIndex: number, field: string, value: any, allItems: any[]) => {
const updatedItems = [...allItems];
const item = { ...updatedItems[itemIndex] };
// Update the changed field
item[field] = value;
// Get operator values (before and after the change)
const oldItem = allItems[itemIndex] || {};
const oldOperatorValue =
field === 'operator'
? getOperatorValue(oldItem.operator)
: getOperatorValue(item.operator);
const newOperatorValue = getOperatorValue(item.operator);
// Calculate what the default category name should be before the change
const oldDefaultCategory = generateDefaultCategoryName(
oldOperatorValue,
getOperatorConfig,
field === 'value1' ? oldItem.value1 : item.value1,
field === 'value2' ? oldItem.value2 : item.value2
);
// Calculate what the new default category name should be after the change
const newDefaultCategory = generateDefaultCategoryName(
newOperatorValue,
getOperatorConfig,
item.value1,
item.value2
);
// Determine if we should auto-update the category
const shouldUpdateCategory =
!item.category ||
item.category.trim() === '' ||
item.category === oldDefaultCategory;
// Auto-populate or update category if conditions are met
if (shouldUpdateCategory && newDefaultCategory) {
item.category = newDefaultCategory;
}
updatedItems[itemIndex] = item;
return updatedItems;
};
};
/**
* Shared visibility condition for value1 field.
*/
export const value1VisibilityCondition = (formData: Record<string, any>) => {
const operatorValue = getOperatorValue(formData.operator);
const operatorConfig = getOperatorConfig(operatorValue);
return operatorConfig ? operatorConfig.operands >= 1 : true;
};
/**
* Shared visibility condition for value2 field.
*/
export const value2VisibilityCondition = (formData: Record<string, any>) => {
const operatorValue = getOperatorValue(formData.operator);
const operatorConfig = getOperatorConfig(operatorValue);
return operatorConfig ? operatorConfig.operands === 2 : false;
};
/**
* Returns a placeholder for value1 based on the selected operator.
* Location operators use "State" as the first operand.
*/
const value1Placeholder = (formData: Record<string, any>): string => {
const operatorValue = getOperatorValue(formData.operator);
if (operatorValue === 'has_district' || operatorValue === 'has_ward') {
return 'State';
}
return '';
};
/**
* Returns a placeholder for value2 based on the selected operator.
* has_ward uses "District" as the second operand.
*/
const value2Placeholder = (formData: Record<string, any>): string => {
const operatorValue = getOperatorValue(formData.operator);
if (operatorValue === 'has_ward') {
return 'District';
}
return '';
};
/**
* Shared item configuration for rules array.
* This defines the operator, value1, value2, and category fields.
*/
export const createRulesItemConfig = () => ({
operator: {
type: 'select' as const,
required: true,
multi: false,
options: [], // Will be set by the caller
getDynamicOptions: () => {
const features = zustand.getState().features;
return operatorsToSelectOptions(getWaitForResponseOperators(features));
},
flavor: 'xsmall' as const,
width: '220px'
},
value1: {
type: 'text' as const,
placeholder: value1Placeholder,
evaluated: true,
flavor: 'xsmall' as const,
conditions: {
visible: value1VisibilityCondition
}
},
value2: {
type: 'text' as const,
placeholder: value2Placeholder,
evaluated: true,
flavor: 'xsmall' as const,
conditions: {
visible: value2VisibilityCondition
}
},
category: {
type: 'text' as const,
placeholder: 'Category',
required: true,
maxLength: 36,
maxWidth: '120px',
flavor: 'xsmall' as const
}
});
/**
* Shared function to extract rules from form data.
* Filters and transforms form rules into the format expected by createRulesRouter.
*/
export const extractUserRules = (formData: FormData) => {
return (formData.rules || [])
.filter((rule: any) => {
const operatorValue = getOperatorValue(rule?.operator);
if (
!operatorValue ||
!rule?.category ||
operatorValue === '' ||
rule.category.trim() === ''
) {
return false;
}
const operatorConfig = getOperatorConfig(operatorValue);
if (operatorConfig && operatorConfig.operands === 1) {
return rule?.value1 && rule.value1.trim() !== '';
} else if (operatorConfig && operatorConfig.operands === 2) {
return (
rule?.value1 &&
rule.value1.trim() !== '' &&
rule?.value2 &&
rule.value2.trim() !== ''
);
}
return true;
})
.map((rule: any) => {
const operatorValue = getOperatorValue(rule.operator);
const operatorConfig = getOperatorConfig(operatorValue);
const value1 = rule.value1 ? rule.value1.trim() : '';
const value2 = rule.value2 ? rule.value2.trim() : '';
let value = '';
if (operatorConfig && operatorConfig.operands === 1) {
value = value1;
} else if (operatorConfig && operatorConfig.operands === 2) {
value = '';
} else {
value = '';
}
return {
operator: operatorValue,
value: value,
value1,
value2,
category: rule.category.trim()
};
});
};
/**
* Shared function to transform router cases to form rules.
* Converts node router cases back into the form data structure.
*/
export const casesToFormRules = (node: any) => {
const rules = [];
if (node.router?.cases && node.router?.categories) {
node.router.cases.forEach((case_: any) => {
// Find the category for this case
const category = node.router!.categories.find(
(cat: any) => cat.uuid === case_.category_uuid
);
// Skip system categories
if (category && !isSystemCategory(category.name)) {
// Handle different operator types
const operatorConfig = getOperatorConfig(case_.type);
const operatorDisplayName = operatorConfig
? operatorConfig.name
: case_.type;
let value1 = '';
let value2 = '';
if (operatorConfig && operatorConfig.operands === 0) {
value1 = '';
value2 = '';
} else if (operatorConfig && operatorConfig.operands === 1) {
value1 = case_.arguments.join(' ');
value2 = '';
} else if (operatorConfig && operatorConfig.operands === 2) {
value1 = case_.arguments[0] || '';
value2 = case_.arguments[1] || '';
} else {
value1 = case_.arguments.join(' ');
value2 = '';
}
rules.push({
operator: { value: case_.type, name: operatorDisplayName },
value1: value1,
value2: value2,
category: category.name
});
}
});
}
return rules;
};
/**
* Creates a complete rules array configuration for forms.
* This is the shared configuration used by both wait_for_response and split_by_expression.
*
* @param operatorOptions - The operator options to use (from operatorsToSelectOptions)
* @param helpText - The help text to display for the rules array
* @returns A complete array field configuration object
*/
export const createRulesArrayConfig = (
operatorOptions: any[],
helpText: string = 'Define rules to categorize responses'
) => ({
type: 'array' as const,
helpText,
itemLabel: 'Rule',
minItems: 0,
maxItems: 100,
sortable: true,
maintainEmptyItem: true,
isEmptyItem: isEmptyRuleItem,
onItemChange: createRuleItemChangeHandler(),
createEmptyItem: (items: any[]) => {
// Get current operator options dynamically (includes location operators if enabled)
const features = zustand.getState().features;
const currentOptions = operatorsToSelectOptions(
getWaitForResponseOperators(features)
);
// Default to the last rule's non-location operator that has at least one operand,
// falling back to the first non-location operator option
const lastWithOperand = [...items].reverse().find((item) => {
const opValue = getOperatorValue(item.operator);
const config = opValue ? getOperatorConfig(opValue) : undefined;
return config && config.operands >= 1 && config.filter !== 'locations';
});
const nonLocationOptions = currentOptions.filter((o: any) => {
const config = getOperatorConfig(o.value);
return !config || config.filter !== 'locations';
});
const opValue = lastWithOperand
? getOperatorValue(lastWithOperand.operator)
: null;
const option = opValue
? currentOptions.find((o: any) => o.value === opValue)
: nonLocationOptions[0];
return option ? { operator: [{ ...option }] } : {};
},
itemConfig: {
...createRulesItemConfig(),
operator: {
...createRulesItemConfig().operator,
options: operatorOptions
}
}
});
|