-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
85 lines (73 loc) · 2.47 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { forEach, map } from 'lodash'
import qs from 'query-string'
import { addNewPatterns } from '../../actions/app'
import { getLists } from '../../data/trello'
import Config from '../../../config/config'
import { regexStringifier } from '../../utils/regex-stringify'
import history from '../../utils/history'
const actions = {
REQUEST: 'REQUEST_LIST',
RECEIVE: 'RECEIVE_LIST',
}
const startRequestList = boardId => ({ type: actions.REQUEST, boardId })
const receiveLists = (boardId, data, error = null) => ({
type: actions.RECEIVE,
boardId,
payload: data,
error,
})
const requestLists = (board, config) => async dispatch => {
dispatch(startRequestList(board.id))
// TODO: optimise and write tests for history.*
// eg. url: http://localhost:2222/#/?pattern=#sprint1,#sprint2,#sprint3
let newRegexSearch = []
let newRegexHash = []
if (history.location.search) {
const parsedSearch = qs.parse(history.location.search)
const parsedPatternSearch = (parsedSearch.pattern && parsedSearch.pattern.split(',')) || []
newRegexSearch = map(parsedPatternSearch, pattern => new RegExp(pattern))
}
if (history.location.hash) {
const parsedPatternHash = history.location.hash.split(',') || []
newRegexHash = map(parsedPatternHash, pattern => new RegExp(pattern))
}
const regexRules =
(newRegexSearch.length > 0 && newRegexSearch) ||
(newRegexHash.length > 0 && newRegexHash) ||
Config.lists ||
[]
// if either one of those is correct, we must add it to the redux store as well
if (regexRules.length > 0) {
dispatch(addNewPatterns(regexRules))
}
try {
const result = await getLists(board.id) // will get all lists of this board
const lists = []
const patterns = []
forEach(result, list => {
let pattern = ''
if (
// some and rx.test inspired by https://stackoverflow.com/a/38075457/1238150
regexRules.some(rx => {
if (rx.test(list.name)) {
pattern = regexStringifier('regex', rx) // convert to string for better re-usage
patterns.push(pattern)
return true
}
return false
})
) {
const listObj = {
list,
config,
pattern, // will allow us to only show lists of a certain pattern
}
lists.push(listObj)
}
})
dispatch(receiveLists(board.id, lists))
} catch (errorMsg) {
dispatch(receiveLists(board.id, null, errorMsg))
}
}
export { actions, requestLists }