fix: label search in tasks not working

This commit is contained in:
kolaente 2021-10-17 13:20:51 +02:00
parent 7e29dde717
commit 529b3d2890
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
3 changed files with 73 additions and 20 deletions

View file

@ -0,0 +1,40 @@
import {filterLabelsByQuery} from './labels'
describe('filter labels', () => {
const state = {
labels: [
{id: 1, title: 'label1'},
{id: 2, title: 'label2'},
{id: 3, title: 'label3'},
{id: 4, title: 'label4'},
{id: 5, title: 'label5'},
{id: 6, title: 'label6'},
{id: 7, title: 'label7'},
{id: 8, title: 'label8'},
{id: 9, title: 'label9'},
],
}
it('should return an empty array for an empty query', () => {
const labels = filterLabelsByQuery(state, [], '')
expect(labels).toHaveLength(0)
})
it('should return labels for a query', () => {
const labels = filterLabelsByQuery(state, [], 'label2')
expect(labels).toHaveLength(1)
expect(labels[0].title).toBe('label2')
})
it('should not return found but hidden labels', () => {
interface label {
id: number,
title: string,
}
const labelsToHide: label[] = [{id: 1, title: 'label1'}]
const labels = filterLabelsByQuery(state, labelsToHide, 'label1')
expect(labels).toHaveLength(0)
})
})

29
src/helpers/labels.ts Normal file
View file

@ -0,0 +1,29 @@
interface label {
id: number,
title: string,
}
interface labelState {
labels: label[],
}
/**
* Checks if a list of labels is available in the store and filters them then query
* @param {Object} state
* @param {Array} labelsToHide
* @param {String} query
* @returns {Array}
*/
export function filterLabelsByQuery(state: labelState, labelsToHide: label[], query: string) {
if (query === '') {
return []
}
const labelQuery = query.toLowerCase()
const labelIds = labelsToHide.map(({id}) => id)
return Object
.values(state.labels)
.filter(({id, title}) => {
return !labelIds.includes(id) && title.toLowerCase().includes(labelQuery)
})
}

View file

@ -1,6 +1,7 @@
import LabelService from '@/services/label' import LabelService from '@/services/label'
import Vue from 'vue' import Vue from 'vue'
import {setLoading} from '@/store/helper' import {setLoading} from '@/store/helper'
import {filterLabelsByQuery} from '@/helpers/labels'
/** /**
* Returns the labels by id if found * Returns the labels by id if found
@ -12,23 +13,6 @@ function getLabelsByIds(state, ids) {
return Object.values(state.labels).filter(({id}) => ids.includes(id)) return Object.values(state.labels).filter(({id}) => ids.includes(id))
} }
/**
* Checks if a list of labels is available in the store and filters them then query
* @param {Object} state
* @param {Array} labels
* @param {String} query
* @returns {Array}
*/
function filterLabelsByQuery(state, labels, query) {
const labelIds = labels.map(({id}) => id)
const foundLabels = getLabelsByIds(state, labelIds)
const labelQuery = query.toLowerCase()
return foundLabels.filter(({title}) => {
return !title.toLowerCase().includes(labelQuery)
})
}
export default { export default {
namespaced: true, namespaced: true,
state: () => ({ state: () => ({
@ -57,7 +41,7 @@ export default {
return (ids) => getLabelsByIds(state, ids) return (ids) => getLabelsByIds(state, ids)
}, },
filterLabelsByQuery(state) { filterLabelsByQuery(state) {
return (...arr) => filterLabelsByQuery(state, ...arr) return (labelsToHide, query) => filterLabelsByQuery(state, labelsToHide, query)
}, },
}, },
actions: { actions: {