aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/ducks/utils/view.js
blob: 6bf0a63ea15bca70c1d2740409e5952884a86f48 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import _ from 'lodash'

export const UPDATE_FILTER = 'VIEW_UPDATE_FILTER'
export const UPDATE_SORT = 'VIEW_UPDATE_SORT'
export const ADD = 'VIEW_ADD'
export const UPDATE = 'VIEW_UPDATE'
export const REMOVE = 'VIEW_REMOVE'
export const RECEIVE = 'VIEW_RECEIVE'

const defaultState = {
    data: [],
    indexOf: {},
}

export default function reduce(state = defaultState, action) {
    switch (action.type) {

        case UPDATE_FILTER:
        {
            const data = action.list.filter(action.filter).sort(action.sort)
            return {
                ...state,
                data,
                indexOf: _.fromPairs(data.map((item, index) => [item.id, index])),
            }
        }

        case UPDATE_SORT:
        {
            const data = [...state.data].sort(action.sort)
            return {
                ...state,
                data,
                indexOf: _.fromPairs(data.map((item, index) => [item.id, index])),
            }
        }

        case ADD:
            if (state.indexOf[action.item.id] != null || !action.filter(action.item)) {
                return state
            }
            return {
                ...state,
                ...sortedInsert(state, action.item, action.sort),
            }

        case REMOVE:
            if (state.indexOf[action.id] == null) {
                return state
            }
            return {
                ...state,
                ...sortedRemove(state, action.id),
            }

        case UPDATE:
            let hasOldItem = state.indexOf[action.item.id] !== null && state.indexOf[action.item.id] !== undefined
            let hasNewItem = action.filter(action.item)
            if (!hasNewItem && !hasOldItem) {
                return state
            }
            if (hasNewItem && !hasOldItem) {
                return {
                    ...state,
                    ...sortedInsert(state, action.item, action.sort)
                }
            }
            if (!hasNewItem && hasOldItem) {
                return {
                    ...state,
                    ...sortedRemove(state, action.item.id)
                }
            }
            if (hasNewItem && hasOldItem) {
                return {
                    ...state,
                    ...sortedUpdate(state, action.item, action.sort),
                }
            }
        case RECEIVE:
        {
            const data = action.list.filter(action.filter).sort(action.sort)
            return {
                ...state,
                data,
                indexOf: _.fromPairs(data.map((item, index) => [item.id, index])),
            }
        }

        default:
            return state
    }
}

export function updateFilter(list, filter = defaultFilter, sort = defaultSort) {
    return { type: UPDATE_FILTER, list, filter, sort }
}

export function updateSort(sort = defaultSort) {
    return { type: UPDATE_SORT, sort }
}

export function add(item, filter = defaultFilter, sort = defaultSort) {
    return { type: ADD, item, filter, sort }
}

export function update(item, filter = defaultFilter, sort = defaultSort) {
    return { type: UPDATE, item, filter, sort }
}

export function remove(id) {
    return { type: REMOVE, id }
}

export function receive(list, filter = defaultFilter, sort = defaultSort) {
    return { type: RECEIVE, list, filter, sort }
}

function sortedInsert(state, item, sort) {
    const index = sortedIndex(state.data, item, sort)
    const data = [ ...state.data ]
    const indexOf = { ...state.indexOf }

    data.splice(index, 0, item)
    for (let i = data.length - 1; i >= index; i--) {
        indexOf[data[i].id] = i
    }

    return { data, indexOf }
}

function sortedRemove(state, id) {
    const index = state.indexOf[id]
    const data = [...state.data]
    const indexOf = { ...state.indexOf, [id]: null }

    data.splice(index, 1)
    for (let i = data.length - 1; i >= index; i--) {
        indexOf[data[i].id] = i
    }

    return { data, indexOf }
}

function sortedUpdate(state, item, sort) {
    let data = [ ...state.data ]
    let indexOf = { ...state.indexOf }
    let index = indexOf[item.id]
    data[index] = item
    while (index + 1 < data.length && sort(data[index], data[index + 1]) > 0) {
        data[index] = data[index + 1]
        data[index + 1] = item
        indexOf[item.id] = index + 1
        indexOf[data[index].id] = index
        ++index
    }
    while (index > 0 && sort(data[index], data[index - 1]) < 0) {
        data[index] = data[index - 1]
        data[index - 1] = item
        indexOf[item.id] = index - 1
        indexOf[data[index].id] = index
        --index
    }
    return { data, indexOf }
}

function sortedIndex(list, item, sort) {
    let low = 0
    let high = list.length

    while (low < high) {
        const middle = (low + high) >>> 1
        if (sort(item, list[middle]) >= 0) {
            low = middle + 1
        } else {
            high = middle
        }
    }

    return low
}

function defaultFilter() {
    return true
}

function defaultSort(a, b) {
    return 0
}