Add topic filter

This commit is contained in:
Thomas Nordquist
2019-01-21 15:07:53 +01:00
parent 4d21c63da9
commit 4c438bd00b
16 changed files with 286 additions and 53 deletions

View File

@@ -1,4 +1,8 @@
import { Action, ActionTypes, TopicOrder } from '../reducers/Settings'
import { ActionTypes as TreeActionTypes, Action as TreeAction } from '../reducers/Tree'
import { Dispatch } from 'redux'
import { AppState } from '../reducers'
import * as q from '../../../backend/src/Model'
export const setAutoExpandLimit = (autoExpandLimit: number = 0): Action => {
return {
@@ -20,9 +24,44 @@ export const setTopicOrder = (topicOrder: TopicOrder = TopicOrder.none): Action
}
}
export const filterTopics = (topicFilter: string): Action => {
return {
export const filterTopics = (filterStr: string) => (dispatch: Dispatch<any>, getState: () => AppState) => {
const topicFilter = filterStr.toLowerCase()
dispatch({
topicFilter,
type: ActionTypes.SETTINGS_FILTER_TOPICS,
})
const { tree } = getState().connection
if (!tree) {
return
}
if (!topicFilter) {
dispatch({
tree,
filter: '',
type: TreeActionTypes.TREE_SHOW_TREE,
})
return
}
const resultTree = tree.leafes()
.filter(leaf => leaf.path().toLowerCase().indexOf(topicFilter) !== -1)
.map((node) => {
const clone = node.unconnectedClone()
q.TreeNodeFactory.insertNodeAtPosition(node.path().split('/'), clone)
return clone.firstNode()
})
.reduce((a: q.TreeNode, b: q.TreeNode) => {
a.updateWithNode(b)
return a
}, new q.Tree())
dispatch({
tree: resultTree,
filter: topicFilter,
type: TreeActionTypes.TREE_SHOW_TREE,
})
}