Filter by custom fields

This commit is contained in:
Gordon 2021-06-08 13:59:54 +01:00
parent 0a7918732e
commit 58cf1eb003

View File

@ -74,8 +74,17 @@ const filterProperties = [
]; ];
// Filter tasks according to the filter string // Filter tasks according to the filter string
const filterTask = (task: KanbnTask, taskFilter: string) => { const filterTask = (
task: KanbnTask,
taskFilter: string,
customFields: { name: string, type: 'boolean' | 'date' | 'number' | 'string' }[]
) => {
let result = true; let result = true;
const customFieldMap = Object.fromEntries(customFields.map(customField => [
customField.name.toLowerCase(),
customField,
]));
const customFieldNames = Object.keys(customFieldMap);
taskFilter.split(' ').forEach(f => { taskFilter.split(' ').forEach(f => {
const parts = f.split(':').map(p => p.toLowerCase()); const parts = f.split(':').map(p => p.toLowerCase());
@ -90,6 +99,17 @@ const filterTask = (task: KanbnTask, taskFilter: string) => {
return; return;
} }
// Filter boolean custom fields
if (customFieldNames.includes(parts[0]) && customFieldMap[parts[0]].type === 'boolean') {
if (
!(customFieldMap[parts[0]].name in task.metadata) ||
!task.metadata[customFieldMap[parts[0]].name]
) {
result = false;
}
return;
}
// Filter task id or name // Filter task id or name
if ( if (
!task.id.toLowerCase().includes(parts[0]) && !task.id.toLowerCase().includes(parts[0]) &&
@ -101,9 +121,14 @@ const filterTask = (task: KanbnTask, taskFilter: string) => {
} }
// If this filter section contains a property name and value, check the value against the property // If this filter section contains a property name and value, check the value against the property
if (parts.length === 2 && filterProperties.indexOf(parts[0]) !== -1) { if (
parts.length === 2 && (
filterProperties.includes(parts[0]) ||
customFieldNames.includes(parts[0])
)
) {
// Filter by property value // Fetch the value to filter by
let propertyValue = ''; let propertyValue = '';
switch (parts[0]) { switch (parts[0]) {
case 'description': case 'description':
@ -121,8 +146,24 @@ const filterTask = (task: KanbnTask, taskFilter: string) => {
case 'relation': case 'relation':
propertyValue = task.relations.map(relation => `${relation.type} ${relation.task}`).join(' '); propertyValue = task.relations.map(relation => `${relation.type} ${relation.task}`).join(' ');
break; break;
default: break; case 'subtask':
propertyValue = task.subTasks.map(subTask => `${subTask.text}`).join(' ');
break;
case 'comment':
propertyValue = task.comments.map(comment => `${comment.author} ${comment.text}`).join(' ');
break;
default:
if (
customFieldNames.includes(parts[0]) &&
customFieldMap[parts[0]].type !== 'boolean' &&
customFieldMap[parts[0]].name in task.metadata
) {
propertyValue = `${task.metadata[customFieldMap[parts[0]].name]}`;
}
break;
} }
// Check the search term against the value
if (!propertyValue.toLowerCase().includes(parts[1])) { if (!propertyValue.toLowerCase().includes(parts[1])) {
result = false; result = false;
} }
@ -326,14 +367,16 @@ const Board = ({
snapshot.isDraggingOver ? 'drag-over' : null snapshot.isDraggingOver ? 'drag-over' : null
].filter(i => i).join(' ')} ].filter(i => i).join(' ')}
> >
{column.filter(task => filterTask(task, taskFilter)).map((task, position) => <TaskItem {column
task={task} .filter(task => filterTask(task, taskFilter, customFields))
columnName={columnName} .map((task, position) => <TaskItem
customFields={customFields} task={task}
position={position} columnName={columnName}
dateFormat={dateFormat} customFields={customFields}
vscode={vscode} position={position}
/>)} dateFormat={dateFormat}
vscode={vscode}
/>)}
{provided.placeholder} {provided.placeholder}
</div> </div>
); );