2021-11-14 21:49:52 +01:00
|
|
|
import {createNewIndexer} from '../indexes'
|
|
|
|
|
|
|
|
const {search} = createNewIndexer('labels', ['title', 'description'])
|
|
|
|
|
|
|
|
export interface label {
|
2021-10-17 13:20:51 +02:00
|
|
|
id: number,
|
|
|
|
title: string,
|
|
|
|
}
|
|
|
|
|
|
|
|
interface labelState {
|
2021-11-14 21:49:52 +01:00
|
|
|
labels: {
|
|
|
|
[k: number]: label,
|
|
|
|
},
|
2021-10-17 13:20:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2021-11-14 21:49:52 +01:00
|
|
|
const labelIdsToHide: number[] = labelsToHide.map(({id}) => id)
|
2021-10-17 13:20:51 +02:00
|
|
|
|
2021-11-14 21:49:52 +01:00
|
|
|
return search(query)
|
|
|
|
?.filter(value => !labelIdsToHide.includes(value))
|
|
|
|
.map(id => state.labels[id])
|
|
|
|
|| []
|
2021-10-17 13:20:51 +02:00
|
|
|
}
|
2021-10-17 16:06:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the labels by id if found
|
|
|
|
* @param {Object} state
|
|
|
|
* @param {Array} ids
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
|
|
|
export function getLabelsByIds(state: labelState, ids: number[]) {
|
|
|
|
return Object.values(state.labels).filter(({id}) => ids.includes(id))
|
|
|
|
}
|