This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
removeMultivalueTag.ts
186 lines (183 loc) · 7.95 KB
/
removeMultivalueTag.ts
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
/*
* TESLER-UI
* Copyright (C) 2018-2020 Tesler Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Observable } from 'rxjs'
import { Store } from 'redux'
import { Epic, types, $do, AnyAction, ActionsMap } from '../../actions/actions'
import { PopupWidgetTypes } from '../../interfaces/widget'
import { TreeAssociatedRecord } from '../../interfaces/tree'
import { assignTreeLinks, getDescendants } from '../../utils/tree'
import { Store as CoreStore } from '../../interfaces/store'
/**
* For full hierarchies it fires `changeDataItem` action to remove value from source record
*
* With `hierarchyGroupDeselection` widget option, parent removal also remove children; removing
* last child will also remove the parent.
*
* With `hierarchyTraverse` widget option, descendants are used instead of children.
*
* When parent should be removed with both option, `removeMultivalueTag` with updated `removedItem` will
* fire instead of `changeDataItem`.
*
* For non-full hierarchies two `changeDataItem` actions will fire, first to drop `_associate` flag
* of remove item and second to update value of source record.
* Widget options are not tested for non-full hierarchies.
*
* @param action removeMultivalueTag
* @param store Store instance
*/
export const removeMultivalueTag: Epic = (action$, store) =>
action$.ofType(types.removeMultivalueTag).mergeMap(action => {
return removeMultivalueTagImpl(action, store)
})
/**
* Default implementation for `removeMultivalueTag` epic
*
* For full hierarchies it fires `changeDataItem` action to remove value from source record
*
* With `hierarchyGroupDeselection` widget option, parent removal also remove children; removing
* last child will also remove the parent.
*
* With `hierarchyTraverse` widget option, descendants are used instead of children.
*
* When parent should be removed with both option, `removeMultivalueTag` with updated `removedItem` will
* fire instead of `changeDataItem`.
*
* For non-full hierarchies two `changeDataItem` actions will fire, first to drop `_associate` flag
* of remove item and second to update value of soure record.
* Widget options are not tested for non-full hierarchies.
*
* @param action removeMultivalueTag
* @param store Store instance
* @category Epics
*/
export function removeMultivalueTagImpl(
action: ActionsMap['removeMultivalueTag'],
store: Store<CoreStore, AnyAction>
): Observable<AnyAction> {
const state = store.getState()
const { bcName, cursor, popupBcName, associateFieldKey } = action.payload
const widget = state.view.widgets.find(item => item.bcName === popupBcName && PopupWidgetTypes.includes(item.type))
const storeData = ((state?.data[popupBcName] || []) as unknown) as TreeAssociatedRecord[]
// Merge store data with pending changes
let data: TreeAssociatedRecord[] = storeData.map(item => {
const pendingChanges = state.view.pendingDataChanges[popupBcName]?.[item.id]
return { ...item, ...pendingChanges }
})
const removedItem = data.find(item => item.id === action.payload.removedItem.id)
/**
* It seems `_associate` is always false for full hierarchies
* so we rely on source record value instead
*/
const associated = action.payload.dataItem.map(item => item.id)
let removedNodes: string[] = []
if (widget.options?.hierarchyGroupDeselection) {
// Builds a tree to simplify searching of descendants
if (widget.options?.hierarchyTraverse) {
data = assignTreeLinks(data)
}
const removedItemChildren = data.filter(item => item.parentId === removedItem.id)
removedNodes = [removedItem.id, ...removedItemChildren.filter(item => associated.includes(item.id)).map(item => item.id)]
if (widget.options?.hierarchyTraverse) {
getDescendants(removedItemChildren, removedNodes)
removedNodes = data.filter(item => removedNodes.includes(item.id) && associated.includes(item.id)).map(item => item.id)
}
const parent = data.find(item => item.id === removedItem.parentId)
const siblings = data.filter(item => item.parentId === parent?.id)
const parentEmpty = siblings.every(child => removedNodes.includes(child.id) || !associated.includes(child.id))
// If last child/descendant removed, parent also should be
if (parent && parentEmpty) {
// Last descendant
if (widget.options?.hierarchyTraverse) {
const parentDescendants: string[] = []
getDescendants(siblings, parentDescendants)
const parentDeepEmpty = parentDescendants.every(descendant => {
return removedNodes.includes(descendant) || !associated.includes(descendant)
})
if (parentDeepEmpty) {
return Observable.concat(
Observable.of(
$do.removeMultivalueTag({
...action.payload,
removedItem: { id: parent.id, value: null }
})
)
)
}
} else {
// Last child
removedNodes.push(parent.id)
}
}
}
// Full hierarchies just filter out selected records
if (widget.options?.hierarchyFull) {
return Observable.of(
$do.changeDataItem({
bcName,
cursor,
dataItem: { [associateFieldKey]: action.payload.dataItem.filter(item => !removedNodes.includes(item.id)) }
})
) as Observable<AnyAction>
}
// Non-full hierarchies drops removed item's `_associate` flag`
// And also updates source record value
if (widget.options?.hierarchy) {
return Observable.concat(
Observable.of(
$do.changeDataItem({
/**
* This is incorrect and will break if different BC has records with
* identical ids.
*
* TODO: Record `level` should be mapped to hierarchyData index instead
*/
bcName:
widget.options?.hierarchy?.find(hierarchyData => {
return state.view.pendingDataChanges[hierarchyData.bcName]?.[action.payload.removedItem.id]
})?.bcName ?? bcName,
cursor: action.payload.removedItem.id,
dataItem: { ...(action.payload.removedItem as any), _associate: false }
})
),
Observable.of(
$do.changeDataItem({
bcName,
cursor,
dataItem: { [associateFieldKey]: action.payload.dataItem }
})
)
) as Observable<AnyAction>
}
// Non hierarchies drops removed item's `_associate` flag` from popup BC
// And also updates source record value
return Observable.concat(
Observable.of(
$do.changeDataItem({
bcName: popupBcName,
cursor: action.payload.removedItem.id,
dataItem: { ...(action.payload.removedItem as any), _associate: false }
})
),
Observable.of(
$do.changeDataItem({
bcName,
cursor,
dataItem: { [associateFieldKey]: action.payload.dataItem }
})
)
)
}