aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/ducks/flows.js
blob: b877d3e458af1df40320907aa482fa80b2a8592f (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import makeList from "./utils/list"
import Filt from "../filt/filt"
import {updateViewFilter, updateViewList, updateViewSort} from "./utils/view"
import {reverseString} from "../utils.js";
import * as columns from "../components/FlowTable/FlowColumns";

export const UPDATE_FLOWS = "UPDATE_FLOWS"
export const SET_FILTER = "SET_FLOW_FILTER"
export const SET_HIGHLIGHT = "SET_FLOW_HIGHLIGHT"
export const SET_SORT = "SET_FLOW_SORT"
export const SELECT_FLOW = "SELECT_FLOW"

const {
    reduceList,
    updateList,
    fetchList,
} = makeList(UPDATE_FLOWS, "/flows")


const defaultState = {
    all: reduceList(),
    selected: [],
    view: [],
    filter: undefined,
    highlight: undefined,
    sort: {sortColumn: undefined, sortDesc: false},
}

function makeFilterFn(filter) {
    return filter ? Filt.parse(filter) : () => true;
}


function makeSortFn(sort){
    let column = columns[sort.sortColumn];
    if (!column) return;

    let sortKeyFun = column.sortKeyFun;
    if (sort.sortDesc) {
        sortKeyFun = sortKeyFun && function (flow) {
            const k = column.sortKeyFun(flow);
            return _.isString(k) ? reverseString("" + k) : -k;
        };
    }
    return sortKeyFun;
}

export default function reducer(state = defaultState, action) {
    switch (action.type) {
        case UPDATE_FLOWS:
            let all = reduceList(state.all, action)
            return {
                ...state,
                all,
                view: updateViewList(state.view, state.all, all, action, makeFilterFn(action.filter), makeSortFn(state.sort))
            }
        case SET_FILTER:
            return {
                ...state,
                filter: action.filter,
                view: updateViewFilter(state.all, makeFilterFn(action.filter), makeSortFn(state.sort))
            }
        case SET_HIGHLIGHT:
            return {
                ...state,
                highlight: action.highlight
            }
        case SET_SORT:
            return {
                ...state,
                sort: action.sort,
                view: updateViewSort(state.view, makeSortFn(action.sort))
            }
        case SELECT_FLOW:
            return {
                ...state,
                selected: [action.flowId]
            }
        default:
            return state
    }
}


export function setFilter(filter) {
    return {
        type: SET_FILTER,
        filter
    }
}
export function setHighlight(highlight) {
    return {
        type: SET_HIGHLIGHT,
        highlight
    }
}
export  function setSort(sort){
    return {
        type: SET_SORT,
        sort
    }
}
export function selectFlow(flowId) {
    return (dispatch, getState) => {
        dispatch({
            type: SELECT_FLOW,
            currentSelection: getState().flows.selected[0],
            flowId
            })
    }
}


export {updateList as updateFlows, fetchList as fetchFlows}