Add topic filter
This commit is contained in:
@@ -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,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user