Here's the updated code with the "type" field added to each node as a root field:
filterTree: DropdownTreeNode<Filter>[] = [
{
title: 'Presents',
type: 'presents',
children: [
{
title: t('manage.answered'),
type: 'answered',
value: {
key: 'surveyAnswers.answered',
filter: {commentsCount: {$gt: 0}}
},
checked: (node) => checked(node.value),
onClick: (node) => toggleFilter(node.value)
},
{
title: t('manage.unanswered'),
type: 'unanswered',
value: {
key: 'surveyAnswers.unanswered',
filter: {
$or: [{commentsCount: 0}, {commentsCount: {$exists: false}}],
email: {$exists: true}
}
},
checked: (node) => checked(node.value),
onClick: (node) => toggleFilter(node.value)
}
]
},
surveyQuestions.length && {
title: 'Survey',
type: 'survey',
children: surveyQuestions.map(({_id, name, type, ratingType, options}) => {
if (type === 'rating') {
const ratings: DropdownTreeNode<Filter>[] = rating(ratingType).map((ratingNum) => ({
title: ratingNum.toString(),
type: 'rating-option',
value: {
key: `surveyAnswers.${_id}.rating.${ratingNum}`,
filter: {[`surveyAnswers.${_id}.rating`]: ratingNum}
},
checked: (node) => checked(node.value),
onClick: (node) => toggleFilter(node.value)
}));
return {
title: name,
type: 'rating',
children: [createToggleAllNode(ratings), ...ratings]
};
}
if (type === 'multipleChoice') {
const choices = options.map(({id, value}) => ({
title: value,
type: 'multiple-choice-option',
value: {
key: `surveyAnswers.${_id}.${id}`,
filter: {[`surveyAnswers.${_id}.choiceIds`]: id}
},
checked: (node) => checked(node.value),
onClick: (node) => toggleFilter(node.value)
}));
return {
title: name,
type: 'multipleChoice',
children: [createToggleAllNode(choices), ...choices]
};
}
if (type === 'singleChoice') {
const choices = options.map(({id, value}) => ({
title: value,
type: 'single-choice-option',
value: {
key: `surveyQuestions.${_id}.${id}`,
filter: {[`surveyAnswers.${_id}.choiceId`]: id}
},
checked: (node) => checked(node.value),
onClick: (node) => toggleFilter(node.value)
}));
return {
title: name,
type: 'singleChoice',
children: [createToggleAllNode(choices), ...choices]
};
}
return {
title: name,
type: 'default',
value: {
key: `surveyQuestions.${_id}`,
filter: {[`surveyAnswers.${_id}`]: {$exists: true}}
},
checked: (node) => checked(node.value),
onClick: (node) => toggleFilter(node.value)
};
})
},
highlightTypes.length && {
title: 'Highlights',
type: 'highlights',
children: highlightTypes.map(({type, color}) => ({
title: type,
type: 'highlight-type',
color,
value: {
key: `highlights.type.${type}`,
filter: {'highlights.type': type}
},
checked: (node) => checked(node.value),
onClick: (node) => toggleFilter(node.value)
}))
}
];